C++基础语言熟悉—简单画板绘制
*介绍
学一门语言最好的方法就是通过各种项目实战来巩固基础语法,使之烂熟于指尖!
小白第一次模仿创作,如有错误及更好的补充还请各位不吝赐教!
代码分析
1.创建窗口
2.画板操作(坐标获取 执行操作)
3.按钮操作(功能栏设计)
效果展示
/窗口设计/
class Window
{
private:
int _width;
int _height;
public:
Window(int w, int h, int flag = 0) :_width(w), _height(h)
{
initgraph(w, h, flag);
};
/*返回窗口状态*/
int exec()
{
return system("pause");
}
~Window()
{
closegraph();
}
public:
**/*设计窗口标题*/**
void setTitle(const char* title)
{
SetWindowText(GetHWnd(), title);
/*GetHwan为获得窗口句柄*/
}
/*获取窗口的高度以及宽度*/
static int GetWidth() { return getwidth(); }
static int GetHeight() { return getheight(); }
};
/坐标获取/
class GetCoordinate
{
public:
GetCoordinate() {};
GetCoordinate(int x, int y) :x(x), y(y) {};
int x;
int y;
static GetCoordinate ZERO;
};
GetCoordinate GetCoordinate::ZERO = GetCoordinate(0, 0);
/画笔操作/
class Paint
{
public:
static void drawRect(const GetCoordinate& p, int Back, int h, COLORREF color = getfillcolor())
{
setfillcolor(color);
solidrectangle(p.x, p.y, p.x + Back, p.y + h);
}
static void drawLine(const GetCoordinate& p1, const GetCoordinate& p2, COLORREF color = getlinecolor(), int lineSize = 1)
{
setlinestyle(PS_COSMETIC, lineSize);
setlinecolor(color);
line(p1.x, p1.y, p2.x, p2.y);
}
static void drawText(int x, int y, const char* text, COLORREF color = gettextcolor(), int textSize = 26)
{
setbkmode(TRANSPARENT);
settextcolor(color);
settextstyle(textSize, 0, "黑体");
outtextxy(x, y, text);
}
static void drawText(int x, int y, int text, COLORREF color = gettextcolor(), int textSize = 26)
{
char lineSizeStr[10] = "";
sprintf_s(lineSizeStr, "%d", text);
Paint::drawText(x, y, lineSizeStr, color, textSize);
}
static void drawCircle(const GetCoordinate& p, int Back, int h, COLORREF color = getfillcolor())
{
setfillcolor(color);
solidellipse(p.x, p.y, p.x + Back, p.y + h);
}
};
class Sketchpad :public Tools
{
public:
Sketchpad() {}
Sketchpad(int x, int y, int Back, int h)
:Tools(x, y, Back, h), color(BLACK), lineSize(1)
{
this->isDown = false;
this->begPos = GetCoordinate(0, 0);
}
bool ProcessMsg(MOUSEMSG& msg)
{
switch (msg.uMsg)
{
case WM_LBUTTONDOWN:
if (isIn(GetCoordinate(msg.x, msg.y)))
{
isDown = true;
begPos = GetCoordinate(msg.x, msg.y);
}
break;
case WM_LBUTTONUP:
isDown = false;
break;
case WM_MOUSEWHEEL: //滚轮调整字体大小
lineSize = msg.wheel > 0 ? lineSize + 1 : lineSize - 1;
if (lineSize <= 0)
{
lineSize = 1;
}
break;
case WM_MOUSEMOVE: //画线
if (isDown)
{
if (isIn(begPos) && isIn(GetCoordinate(msg.x, msg.y))) // 起始点和结束点都在区域内
{
Paint::drawLine(begPos, GetCoordinate(msg.x, msg.y), color, lineSize);
}
begPos = GetCoordinate(msg.x, msg.y);
}
break;
case WM_RBUTTONDOWN: //清屏
Paint::drawRect(GetCoordinate::ZERO, Window::getW() - 100, Window::getH(), WHITE); //绘图区域
break;
}
return false;
}
public:
int getLineSize()
{
return lineSize;
}
void setColor(COLORREF color)
{
this->color = color;
}
private:
int lineSize; //线条宽度
COLORREF color; //线条颜色
bool isDown; //鼠标是否按下
GetCoordinate begPos; //记录上一个点的坐标
};
/继承基础/
class Tools
{
public:
Tools() {};
Tools(int x, int y, int Back, int h)
:leftTop(x, y), width(Back), height(h)
{
}
//鼠标是否在控件上
bool isIn(const GetCoordinate& p)
{
if (p.x >= leftTop.x && p.x <= leftTop.x + width &&
p.y >= leftTop.y && p.y <= leftTop.y + height)
{
return true;
}
return false;
}
//处理鼠标消息,需要继承自己实现
bool ProcessMsg(MOUSEMSG& msg)
{
return 0;
}
GetCoordinate leftTop;
int width;
int height;
};
/功能区域设计/
class Button :public Tools
{
public:
enum ButtonStyle { CIRCLE, RECT };
Button() :style(ButtonStyle::CIRCLE) {}
Button(int x, int y, int Back, int h, ButtonStyle style = ButtonStyle::CIRCLE)
:Tools(x, y, Back, h), style(style)
{
isClick = false;
}
bool ProcessMsg(MOUSEMSG& msg, Sketchpad& Sketchpad)
{
switch (msg.uMsg)
{
case WM_LBUTTONDOWN:
if (isIn(GetCoordinate(msg.x, msg.y)))
{
printf("leftTop(%d,%d) %#X \n", leftTop.x, leftTop.y, color);
Sketchpad.setColor(color);
isClick = true;
}
break;
case WM_LBUTTONUP:
isClick = false;
break;
}
if (style == ButtonStyle::CIRCLE)
{
Paint::drawCircle(leftTop, width, height, color);
}
else if (style == ButtonStyle::RECT)
{
Paint::drawRect(leftTop, width, height, color);
}
return false;
}
public:
void setPos(const GetCoordinate& p)
{
this->leftTop = p;
}
void setSize(int Back, int h)
{
this->width = Back;
this->height = h;
}
void setColor(COLORREF color)
{
this->color = color;
}
void setStyle(ButtonStyle style)
{
this->style = style;
}
private:
bool isClick; //按钮是否被点击
COLORREF color; //按钮颜色
ButtonStyle style; //按钮样式
};
/主函数/
int main()
{
Window Back(1100, 768, EW_SHOWCONSOLE);
Back.setWindowTitle("c++ 绘图");
Paint::drawRect(GetCoordinate::ZERO, Back.getW(), Back.getH(), WHITE);//绘图区域 //绘图区域
Paint::drawRect(GetCoordinate(Back.getW() - 100, 0), Back.getW(), Back.getH(), 0xEFEFEF); //工具栏
Sketchpad Sketchpad(0, 0, Back.getW() - 100, Back.getH());
//颜色按钮
Button btns[9];
for (int i = 0; i < 9; i++)
{
btns[i].setSize(50, 50);
btns[i].setPos(GetCoordinate(Window::getW() - 100 + 26, 50 + i * 60));
btns[i].setColor(RGB(rand() % 256, rand() % 256, rand() % 256));
//btns[i].setStyle(Button::RECT);
}
```cpp
BeginBatchDraw();
while (true)
{
MOUSEMSG msg = GetMouseMsg();
Sketchpad.ProcessMsg(msg); //画布,处理消息
Paint::drawRect(GetCoordinate(1050, 700), 50, 50, 0xEFEFEF);
Paint::drawText(1000, 700, "字体大小:", BLACK, 15);
Paint::drawText(1080, 700, Sketchpad.getLineSize(), BLACK, 15);
for (int i = 0; i < 9; i++)
{
btns[i].ProcessMsg(msg, Sketchpad);
}
FlushBatchDraw();
}
return Back.exec(); //防止闪退
}
> 这里是引用
谢谢支持!