65,187
社区成员




#include "MathDrawPad.h"
HDC InitMathDrawPad()
{
hAllwnd=GetForegroundWindow();
hAllDC=GetDC(hAllwnd);
}
HDC InitMathDrawPad(HWND hwnd)
{
hAllwnd=hwnd;
hAllDC=GetDC(hAllwnd);
}
void SetDot(pos* p,COLORREF c)
{
RECT rc;
rc.left=p->x;
rc.top=p->y;
rc.right=p->x+1;
rc.right=p->y+1;
FillRect(hAllDC,&rc,(HBRUSH)&c);
}
void SetSomeDot(pos* p,COLORREF c,int len)
{
for(int i=1;i<=len;i++) SetDot(&p[i],c);
}
void DrawLine(pos* bp,pos* ep,COLORREF c)
{
int x=abs(bp->x-ep->x),y=abs(bp->y-ep->y);
if(x>=y)
{
for(double i=0,j=0;i++,j+=y/x;i++)
{
pos p={(bp->x>=ep->x?i:-i),(bp->y-ep->y?j:-j)};
SetDot(&p,c);
}
}
else
{
for(double i=0,j=0;i++,j+=x/y;i++)
{
pos p={(bp->x>=ep->x?i:-i),(bp->y-ep->y?j:-j)};
SetDot(&p,c);
}
}
}
void DrawCircle(pos* O,int r,COLORREF c)
{
for(int i=-r;i<=r;i++)
{
pos p={i,sqrt(r*r-i*i)};
SetDot(&p,c);
}
for(int i=-r;i<=r;i++)
{
pos p={i,-sqrt(r*r-i*i)};
SetDot(&p,c);
}
}
void ClosePad()
{
ReleaseDC(hAllwnd,hAllDC);
}
void EndPad(void (*EndProc)())
{
EndProc();
ClosePad();
}
h:
/*
* Name: MathDrawPad
* Copyright: He Zhexi Personal Development
* Author: He Zhexi
* Date: 10/04/20 10:44
* Description: This is the MathDrawPad's head file. MathDrawPad want to build an object to make easier to draw on the window.
* So please when you're include to windows.h or cmath, think about have you include MathDrawPad.h?
* If yes,please don't include windows.h or cmath. Because I have included the windows.h and cmath headfile.
* If you know windows.h and you can use some(or many)API. I aslo write some function to use something in windows.h.
* and You can find me LoadRe@163.com
* This is my first head of big object. So if some where can be better. Welcome to find me in e-mail and tell the Improvement Scheme.
*/
#include <windows.h>
#include <cmath>
//The color macros
//First order color
#define COLOR_RED RGB(255,0,0)
#define COLOR_GREEN RGB(0,255,0)
#define COLOR_BLUE RGB(0,0,255)
//Second order color
#define COLOR_YELLO COLOR_RED|COLOR_GREEN
#define COLOR_PURPLE COLOR_RED|COLOLR_BLUE
#define COLOR_LIGHTBLUE COLOR_GREEN|COLOR_BLUE
//The var proclamations district
HDC hAllDC;
HWND hAllwnd;
//Some struct
#ifndef POS_H
#define POS_H
struct pos
{
int x;
int y;
};
#endif
//Please init pad first. Else the pad won't work!
HDC InitMathDrawPad();
HDC InitMathDrawPad(HWND hwnd);
//Make init function easier
#define InitPad InitMathDrawPad
//The unit of the pad,dot.
//SetDot based on the FillRect function
void SetDot(pos* p,COLORREF c);
void SetSomeDot(pos* p,COLORREF c,int len); //This function will draw len dot.
//Lines based on the SetDot function.
void DrawLine(pos* bp,pos* ep,COLORREF c);
//Circle based on the SetDot,too.And it's also use function 'sqrt'
void DrawCircle(pos* O,int r,COLORREF c);
//Release the dc
void ClosePad();
void EndPad(void (*EndProc)());
test.cpp:
#include <MathDrawPad.h>
int main()
{
InitPad();
pos b={30,30},e={100,200};
DrawLine(&b,&e,RGB(255,255,255));
ClosePad();
return 0;
}
有谁能帮我们一下啊——