我想把直线和矩形的绘制记录放在一个vector数组中,并且能够一直画直线或者矩形,但是在用当前形状为直线时,测试之后只能画出一条直线,猜测是无法获取鼠标左键再次按下的消息,所以在此请教
#include <iostream>
#include <easyx.h>
#include <vector>
using namespace std;
//定义形状类型枚举
enum ShapeType
{
ShapeLine, //线段
ShapeRect //矩形
};
int g_begx, g_begy;
bool isPress = false; //鼠标是否按下
int g_shapetype = ShapeLine; //当前形状
vector<void*> g_shapes; //存储所有形状
class Line
{
private:
int type; //形状
int x1;
int y1;
int x2;
int y2;
COLORREF color; //颜色
int line_Th; //线宽
vector<void*> g_shapes; //存储所有形状
public:
Line()
{
}
Line(int x1, int y1, int x2, int y2, COLORREF color, int line_Th)
{
this->type = ShapeLine;
this->x1 = x1;
this->y1 = y1;
this->x2 = x2;
this->y2 = y2;
this->color = color;
this->line_Th = line_Th;
}
~Line()
{
}
void line_draw()
{
setlinecolor(this->color);
setlinestyle(PS_SOLID, this->line_Th);
line(this->x1, this->y1, this->x2, this->y2);
}
int GetType()
{
return this->type;
}
void SetType(int t)
{
this->type = t;
}
int GetX1()
{
return this->x1;
}
void SetX1(int x)
{
this->x1 = x;
}
int GetY1()
{
return this->y1;
}
void SetY1(int y)
{
this->y1 = y;
}
int GetX2()
{
return this->x2;
}
void SetX2(int x)
{
this->x2 = x;
}
int GetY2()
{
return this->y2;
}
void SetY2(int y)
{
this->y2 = y;
}
COLORREF GetColor()
{
return this->color;
}
void SetColor(COLORREF c)
{
this->color = c;
}
int Getline_Th()
{
return this->line_Th;
}
void Setline_Th(int lt)
{
this->line_Th = lt;
}
};
class Rect :public Line
{
public:
Rect()
{
}
Rect(int x1, int y1, int x2, int y2, COLORREF color, int line_Th)
{
Rect r(int x1, int y1, int x2, int y2, COLORREF color, int line_Th);
this->SetType(ShapeRect);
}
~Rect()
{
}
void rect_draw()
{
setlinecolor(this->GetColor());
setlinestyle(PS_SOLID, this->Getline_Th());
rectangle(this->GetX1(), this->GetY1(), this->GetX2(), this->GetY2());
}
};
//消息处理函数
//鼠标左键按下处理
void mouseButtomDown(ExMessage* msg)
{
isPress = true;
//记录鼠标按下的坐标
g_begx = msg->x;
g_begy = msg->y;
}
//鼠标左键弹起处理
void mouseButtomUP(ExMessage* msg)
{
isPress = false;
//判断当前绘制的形状是什么
if (g_shapetype == ShapeLine)
{
Line line(g_begx, g_begy, msg->x, msg->y, RED, 2);
Line* l = new Line;
l = &line;
g_shapes.push_back(l);
}
else if (g_shapetype == ShapeRect)
{
Rect rect(g_begx, g_begy, msg->x, msg->y, RED, 2);
Line* r = new Rect;
r = ▭
g_shapes.push_back(r);
}
}
//鼠标移动处理
void mouseButtomMOVE(ExMessage* msg)
{
//鼠标左键按下并移动,就开始绘制
if (isPress)
{
cleardevice(); //清屏重绘
setlinecolor(RED);
switch (g_shapetype)
{
case ShapeLine:
//画直线
line(g_begx, g_begy, msg->x, msg->y);
break;
case ShapeRect:
//画矩形
rectangle(g_begx, g_begy, msg->x, msg->y);
break;
}
}
}
int main()
{
initgraph(640, 480, EX_SHOWCONSOLE);//创建窗口
setbkcolor(WHITE);//设置背景颜色为白色
cleardevice();//清屏
BeginBatchDraw(); //双缓冲,防止闪屏
while (true)
{
//绘制
for (int i = 0; i < g_shapes.size(); i++)
{
(*(Line*)g_shapes[i]).line_draw();
}
//消息处理 鼠标消息
static ExMessage msg;
if (peekmessage(&msg, EX_MOUSE))
{
//消息分发
switch (msg.message)
{
case WM_LBUTTONDOWN: //鼠标左键按下
mouseButtomDown(&msg);
break;
case WM_LBUTTONUP: //鼠标左键弹起
mouseButtomUP(&msg);
break;
case WM_MOUSEMOVE: //鼠标移动
mouseButtomMOVE(&msg);
break;
}
}
FlushBatchDraw();
}
EndBatchDraw();
getchar();
return 0;
}