邪门的方法按你的要求修正版,并且把消息框底下的绘图加到刷新中,就不会丢失了:
增加邪门的方法和增加底层绘图的运行的效果视屏网站:https://www.bilibili.com/read/cv19753023?spm_id_from=333.999.0.0
1修改部分:
if (this->Get_message()->x >= 155 && this->Get_message()->x <= 205 && this->Get_message()->y >= 15 && this->Get_message()->y <= 45)
{
HelpUser();
HWND hwnd = GetHWnd();
POINT point;
BOOL af = true;
while (af)
{
GetCursorPos(&point); // 获取鼠标指针位置(屏幕坐标)
ScreenToClient(hwnd, &point);
while ((GetAsyncKeyState(VK_LBUTTON)))
{
if (point.x <= 155 || point.x >= 205 || point.y <= 15 || point.y >= 45)
{
setfillcolor(WHITE);
setlinecolor(WHITE);
clearrectangle(300, 200, 800, 450);
fillrectangle(300, 200, 800, 450);
af = false;
break;
}
}
}
}
}
2.修改部分2:
Canvas canvas;
canvas.painting();
3.邪门完整代码:
#include <iostream>
#include <graphics.h>
#include <conio.h>
#include<math.h>
using namespace std;
int PI = 3.14;
void paintstar(double x, double y, double r, double a)
{
int pt[10];
for (int n = 0; n < 5; ++n)
{
pt[n * 2] = (int)(-cos(PI * 4 / 5 * n + a) * r + x);
pt[n * 2 + 1] = (int)(sin(PI * 4 / 5 * n + a) * r + y);
}
fillpoly(5, pt);
}
class Canvas
{
private:
ExMessage msg;
public:
ExMessage* Get_message();
void LMouseButtonDown();
void HelpUser();
void painting();
};
ExMessage* Canvas::Get_message()
{
return &this->msg;
}
void Canvas::LMouseButtonDown()
{
if (this->Get_message()->x >= 155 && this->Get_message()->x <= 205 && this->Get_message()->y >= 15 && this->Get_message()->y <= 45)
{
HelpUser();
HWND hwnd = GetHWnd();
POINT point;
BOOL af = true;
while (af)
{
GetCursorPos(&point); // 获取鼠标指针位置(屏幕坐标)
ScreenToClient(hwnd, &point);
while ((GetAsyncKeyState(VK_LBUTTON)))
{
if (point.x <= 155 || point.x >= 205 || point.y <= 15 || point.y >= 45)
{
setfillcolor(WHITE);
setlinecolor(WHITE);
clearrectangle(300, 200, 800, 450);
fillrectangle(300, 200, 800, 450);
af = false;
break;
}
}
}
}
}
void Canvas::HelpUser()
{
setfillcolor(RGB(150, 150, 150));
fillrectangle(300, 200, 800, 450);
settextcolor(WHITE);
settextstyle(20, 0, _T("黑体"));
outtextxy(320, 220, _T("鼠标点击绘图区任意位置退出帮助"));
}
void Canvas::painting()
{
double r = 0;
while (true)
{
r += 0.02;
if (r > PI * 2) r -= PI * 2;
setcolor(RGB(0xff, 0xff, 0xff));
setfillcolor(RGB(0, 0, 0xff));
paintstar(400, 300, 100, r);
while (peekmessage(this->Get_message(), EX_MOUSE))
{
switch (this->Get_message()->message)
{
case WM_LBUTTONDOWN:
this-> LMouseButtonDown();
break;
}
}
}
}
//界面显示
void SetWindow()
{
initgraph(1080, 750); //创建窗口
//设置背景
setbkcolor(RGB(150, 150, 150));
cleardevice();
//绘制画图板
setfillcolor(WHITE);
fillrectangle(15, 70, 1060, 800);
//工具栏
setlinecolor(RGB(10, 10, 10));
setlinestyle(PS_SOLID, 1);
rectangle(155, 15, 205, 45);
settextstyle(18, 11, _T("黑体"));
outtextxy(160, 23, _T("帮助"));
outtextxy(160, 23, _T("帮助"));
}
int main()
{
SetWindow();
Canvas canvas;
canvas.painting();
_getch();
return 0;
}
你看看这是不是你要的效果,点击按钮弹出提示框,点击按钮消除提示框的内容。
运行的效果视屏网站:https://www.bilibili.com/read/cv19753023?spm_id_from=333.999.0.0
1.实例问题
你的点击按钮外区域就关闭提示的算法算法有问题。
正常的点击按钮算法判断是:
if (this->Get_message()->x >= 155 && this->Get_message()->x <= 205 && this->Get_message()->y >= 15 && this->Get_message()->y <= 45)
但是点击按钮区域外的算法是反过来的如下:
if (this->Get_message()->x <= 155 || this->Get_message()->x >= 205 || this->Get_message()->y <= 15 || this->Get_message()->y >= 45)
所以你的代码无效果
2.修改代码完整版:
第一修改区域为:
void Canvas::LMouseButtonDown()
{
if (this->Get_message()->x >= 155 && this->Get_message()->x <= 205 && this->Get_message()->y >= 15 && this->Get_message()->y <= 45)
{
HelpUser();
}
else if (this->Get_message()->x <= 155 || this->Get_message()->x >= 205 || this->Get_message()->y <= 15 || this->Get_message()->y >= 45)
{
setfillcolor(WHITE);
setlinecolor(WHITE);
clearrectangle(300, 200, 800, 450);
fillrectangle(300, 200, 800, 450);
}
}
第二修改区域为:
while (1)
{
Canvas canvas;
canvas.painting();
}
3.修改代码如下:
#include <iostream>
#include <graphics.h>
#include <conio.h>
using namespace std;
class Canvas
{
private:
ExMessage msg;
public:
ExMessage* Get_message();
void LMouseButtonDown();
void HelpUser();
void painting();
};
ExMessage* Canvas::Get_message()
{
return &this->msg;
}
void Canvas::LMouseButtonDown()
{
if (this->Get_message()->x >= 155 && this->Get_message()->x <= 205 && this->Get_message()->y >= 15 && this->Get_message()->y <= 45)
{
HelpUser();
}
else if (this->Get_message()->x <= 155 || this->Get_message()->x >= 205 || this->Get_message()->y <= 15 || this->Get_message()->y >= 45)
{
setfillcolor(WHITE);
setlinecolor(WHITE);
clearrectangle(300, 200, 800, 450);
fillrectangle(300, 200, 800, 450);
}
}
void Canvas::HelpUser()
{
setfillcolor(RGB(150, 150, 150));
fillrectangle(300, 200, 800, 450);
settextcolor(WHITE);
settextstyle(20, 0, _T("黑体"));
outtextxy(320, 220, _T("鼠标点击绘图区任意位置退出帮助"));
}
void Canvas::painting()
{
while (true)
{
while (peekmessage(this->Get_message(), EX_MOUSE))
{
switch (this->Get_message()->message)
{
case WM_LBUTTONDOWN:
this->LMouseButtonDown();
break;
}
}
}
}
//界面显示
void SetWindow()
{
initgraph(1080, 750); //创建窗口
//设置背景
setbkcolor(RGB(150, 150, 150));
cleardevice();
//绘制画图板
setfillcolor(WHITE);
fillrectangle(15, 70, 1060, 800);
//工具栏
setlinecolor(RGB(10, 10, 10));
setlinestyle(PS_SOLID, 1);
rectangle(155, 15, 205, 45);
settextstyle(18, 11, _T("黑体"));
outtextxy(160, 23, _T("帮助"));
}
int main()
{
SetWindow();
Canvas canvas;
canvas.painting();
_getch();
return 0;
}