现阶段只能调颜色,后续慢慢加入新功能
先写出线的结构和按键结构
创造按键和在图形板上画出
画线
改变线颜色函数
#include<stdio.h>
#include<easyx.h>
#include<windows.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>
#include<assert.h>
#define High 800
#define wide 800
struct Point
{
int x;
int y;
};
struct Line_my
{
int size;
COLORREF color;
Point start;
bool isdorw;
};
struct Line_my line_tool = { 10,RED,{0,0},false};
typedef struct Button
{
int x;
int y;
int w;
int h;
COLORREF color;
char* text;
}button, * LPbutton;
LPbutton createbutton(int x, int y, int w, int h, COLORREF color, const char* p)
{
LPbutton b = (LPbutton)malloc(sizeof(Button));
assert(b);
b->x = x;
b->y = y;
b->w = w;
b->h = h;
b->color = color;
b->text = (char*)malloc(sizeof(char) * (strlen(p) + 1));
assert(b->text);
strcpy(b->text, p);
return b;
}
void drawbutton(LPbutton b)
{
int a, c, xx, yy;
setbkmode(TRANSPARENT);
setfillcolor(b->color);
settextstyle(15, 0, "楷体");
solidrectangle(b->x, b->y, b->x + b->w, b->y + b->h);
a = textwidth(b->text);
c = textheight(b->text);
xx = b->x + (b->w - a) / 2;
yy = b->y + (b->h - c) / 2;
outtextxy(xx, yy, b->text);
settextcolor(RGB(42, 45, 65));
}
void darw_line(ExMessage a)//Exmessage鼠标消息相当于int,char之类
{
if (a.message == WM_LBUTTONDOWN)
{
line_tool.start.x=a.x;
line_tool.start.y=a.y;
line_tool.isdorw = true;
}
if (a.message == WM_LBUTTONUP)
{
line_tool.isdorw = false;
}
if (a.message == WM_MOUSEMOVE && line_tool.isdorw == true)
{
setlinecolor(line_tool.color);
setlinestyle(PS_SOLID,line_tool.size);
line(line_tool.start.x,line_tool.start.y,a.x,a.y);
}
line_tool.start.x = a.x;
line_tool.start.y = a.y;
if (a.message == WM_RBUTTONDOWN)
{
cleardevice();
}
}
void setcolor(ExMessage l, LPbutton q, LPbutton e, LPbutton i, LPbutton m)
{
if (l.message == WM_LBUTTONDOWN && l.x >= q->x && l.x < e->x && l.y >= 0 && l.y <= 100)
{
q->color = RGB(182, 255, 255);
line_tool.color = BLUE;
}
else
{
q->color = BLUE;
}
if (l.message == WM_LBUTTONDOWN && l.x >= e->x && l.x < i->x && l.y >= 0 && l.y <= 100)
{
e->color = RGB(182, 255, 255);
line_tool.color = GREEN;
}
else
{
e->color = GREEN;
}
if (l.message == WM_LBUTTONDOWN && l.x >= i->x && l.x < m->x && l.y >= 0 && l.y <= 100)
{
i->color = RGB(182, 255, 255);
line_tool.color = WHITE;
}
else
{
i->color = WHITE;
}
if (l.message == WM_LBUTTONDOWN && l.x >= m->x && l.x <= wide && l.y >= 0 && l.y <= 100)
{
m->color = RGB(182, 255, 255);
line_tool.color = RED;
}
else
{
m->color = RED;
}
}
int main(void)
{
initgraph(wide, High);
LPbutton a = createbutton(0,0,50,50,BLUE,"BLUE");
LPbutton b = createbutton(50,0,50,50,GREEN,"GREEN");
LPbutton c = createbutton(100, 0, 50, 50,WHITE, "WHITE");
LPbutton d = createbutton(150, 0, 50, 50,RED, "RED");
while (1)
{
BeginBatchDraw();
ExMessage k;//鼠标控制变量;
peekmessage(&k, EM_MOUSE);//接受鼠标消息
darw_line(k);
drawbutton(a);
drawbutton(b);
drawbutton(c);
drawbutton(d);
setcolor(k, a, b,c, d);
if (_kbhit())//按键控制并保持图片用saveimage
{
if (_getch() == '\r')
{
saveimage("1.bmp");
cleardevice();
//break;
}
}
EndBatchDraw();
}
closegraph();
return 0;
}