清屏刷新问题

0

想实现的功能:在绘图窗口的白色区域实现连续画单条直线,其余灰色区域一直存在,不会被清屏

遇到的问题:若在void Canvas::mouseMove(ExMessage* msg)函数体内写上cleardevice(),可以实现每次鼠标移动画出单条直线,但是绘图窗口中的灰色区域会被清屏;

若不写cleardevice(),绘图窗口的灰色区域不会被清屏,但是每次鼠标移动画线时画出的是一系列直线

环境:vs2022,win10

#include <iostream>
#include <graphics.h>
#include <conio.h>
#include <vector>
using namespace std;

class Line
{
private:
	int x1;
	int y1;
	int x2;
	int y2;
	COLORREF color;  //颜色
	int line_Th;  //线宽

public:
	Line();
	Line(int x1, int y1, int x2, int y2, COLORREF color, int line_Th);
	~Line();

	void line_draw();
};

Line::Line()
{
}

Line::Line(int x1, int y1, int x2, int y2, COLORREF color, int line_Th)
{
	this->x1 = x1;
	this->y1 = y1;
	this->x2 = x2;
	this->y2 = y2;
	this->color = color;
	this->line_Th = line_Th;
}

Line::~Line()
{
}

void Line::line_draw()
{
	setlinecolor(this->color);
	setlinestyle(PS_SOLID, this->line_Th);
	line(this->x1, this->y1, this->x2, this->y2);
}



class Canvas
{
private:
	Line l;

	ExMessage msg;

	int begx;  //鼠标按下时x坐标
	int begy;  //鼠标按下时y坐标

	bool isPress = false;  //鼠标是否按下

	vector<Line*> Shapes;

public:
	Canvas();
	~Canvas();

	ExMessage* Get_message();

	void LMouseButtonDown(ExMessage* msg);
	void LMouseButtonUp(ExMessage* msg);
	void mouseMove(ExMessage* msg);

	void SetVector(Line* l);
	Line* GetVector(int i);

	void painting();
};

Canvas::Canvas()
{
}

Canvas::~Canvas()
{
}

ExMessage* Canvas::Get_message()
{
	return &this->msg;
}

void Canvas::LMouseButtonDown(ExMessage* msg)
{
	this->isPress = true;

	//记录鼠标按下时的坐标
	this->begx = msg->x;
	this->begy = msg->y;
}

void Canvas::LMouseButtonUp(ExMessage* msg)
{
	this->isPress = false;

	Line* l = new Line(this->begx, this->begy, msg->x, msg->y, BLUE, 2);
	this->SetVector(l);
}

void Canvas::mouseMove(ExMessage* msg)
{
	//若鼠标左键按下并移动,就开始绘制
	if (this->isPress)
	{
		cleardevice();

		//测试直线
		setlinecolor(BLUE);
		line(this->begx, this->begy, msg->x, msg->y);
	}
}

void Canvas::SetVector(Line* l)
{
	this->Shapes.push_back(l);
}

Line* Canvas::GetVector(int i)
{
	return this->Shapes[i];
}

void Canvas::painting()
{
	while (true)
	{
		//绘制
		for (int i = 0; i < this->Shapes.size(); i++)
		{
			this->GetVector(i)->line_draw();
		}

		while (peekmessage(this->Get_message(), EX_MOUSE))
		{
			switch (this->Get_message()->message)
			{
			case WM_LBUTTONDOWN:
				this->LMouseButtonDown(this->Get_message());
				break;
			case WM_LBUTTONUP:
				this->LMouseButtonUp(this->Get_message());
				break;
			case WM_MOUSEMOVE:
				this->mouseMove(this->Get_message());
				break;
			}
		}
	}
}

//界面显示
void SetWindow()
{
	initgraph(1080, 750);  //创建窗口
	//设置背景
	setbkcolor(RGB(150, 150, 150));
	cleardevice();
	//绘制画图板
	setfillcolor(WHITE);
	fillrectangle(0, 70, 1060, 750);
	//绘制颜色选择区域
	setfillcolor(RGB(195, 195, 195));
	fillrectangle(980, 70, 1072, 750);
}

int main()
{
	SetWindow();

	Canvas canvas;
	canvas.painting();

	_getch();
	return 0;
}
ava
kk

2022-11-20

0

解决方法:加上写入缓冲区。

缓冲区函数:

	BeginBatchDraw();
	FlushBatchDraw();

修改部位:在这里加上你的程序灰色绘图部分就可以完美解决,

去掉 cleardevice(); 改成 

BeginBatchDraw();
FlushBatchDraw();

setlinecolor(WHITE);
setfillcolor(WHITE);
fillrectangle(0, 70, 1060, 750);
//绘制颜色选择区域
setfillcolor(RGB(195, 195, 195));
fillrectangle(980, 70, 1072, 750);


 


//测试直线
setlinecolor(BLUE);
line(this->begx, this->begy, msg->x, msg->y);

就是让灰色清除,绘制直线,而不是用背景色来清除直线,然后就会变成你那样

加入 缓冲 函数 就不会绘制直线而闪屏了

void Canvas::mouseMove(ExMessage* msg)
{
	//若鼠标左键按下并移动,就开始绘制
	if (this->isPress)
	{
		BeginBatchDraw();
		FlushBatchDraw();

		setlinecolor(WHITE);
		setfillcolor(WHITE);
		fillrectangle(0, 70, 1060, 750);
		//绘制颜色选择区域
		setfillcolor(RGB(195, 195, 195));
		fillrectangle(980, 70, 1072, 750);

		//测试直线
		setlinecolor(BLUE);
		line(this->begx, this->begy, msg->x, msg->y);
	}
}

修改代码:

#include <iostream>
#include <graphics.h>
#include <conio.h>
#include <vector>
using namespace std;

//界面显示
void SetWindow()
{
	initgraph(1080, 750);  //创建窗口
	//设置背景
	setbkcolor(RGB(150, 150, 150));
	cleardevice();
	//绘制画图板
	setfillcolor(WHITE);
	fillrectangle(0, 70, 1060, 750);
	//绘制颜色选择区域
	setfillcolor(RGB(195, 195, 195));
	fillrectangle(980, 70, 1072, 750);
}

class Line
{
private:
	int x1;
	int y1;
	int x2;
	int y2;
	COLORREF color;  //颜色
	int line_Th;  //线宽

public:
	Line();
	Line(int x1, int y1, int x2, int y2, COLORREF color, int line_Th);
	~Line();

	void line_draw();
};

Line::Line()
{
}

Line::Line(int x1, int y1, int x2, int y2, COLORREF color, int line_Th)
{
	this->x1 = x1;
	this->y1 = y1;
	this->x2 = x2;
	this->y2 = y2;
	this->color = color;
	this->line_Th = line_Th;
}

Line::~Line()
{
}

void Line::line_draw()
{
	setlinecolor(this->color);
	setlinestyle(PS_SOLID, this->line_Th);
	line(this->x1, this->y1, this->x2, this->y2);
}



class Canvas
{
private:
	Line l;

	ExMessage msg;

	int begx;  //鼠标按下时x坐标
	int begy;  //鼠标按下时y坐标

	bool isPress = false;  //鼠标是否按下

	vector<Line*> Shapes;

public:
	Canvas();
	~Canvas();

	ExMessage* Get_message();

	void LMouseButtonDown(ExMessage* msg);
	void LMouseButtonUp(ExMessage* msg);
	void mouseMove(ExMessage* msg);

	void SetVector(Line* l);
	Line* GetVector(int i);

	void painting();
};

Canvas::Canvas()
{
}

Canvas::~Canvas()
{
}

ExMessage* Canvas::Get_message()
{
	return &this->msg;
}

void Canvas::LMouseButtonDown(ExMessage* msg)
{
	this->isPress = true;

	//记录鼠标按下时的坐标
	this->begx = msg->x;
	this->begy = msg->y;
}

void hcq()
{
	
}
void Canvas::LMouseButtonUp(ExMessage* msg)
{
	this->isPress = false;

	Line* l = new Line(this->begx, this->begy, msg->x, msg->y, BLUE, 2);
	this->SetVector(l);
}

void Canvas::mouseMove(ExMessage* msg)
{

	
	//若鼠标左键按下并移动,就开始绘制
	if (this->isPress)
	{
	
		BeginBatchDraw();
		FlushBatchDraw();

		setlinecolor(WHITE);
		setfillcolor(WHITE);
		fillrectangle(0, 70, 1060, 750);
		//绘制颜色选择区域
		setfillcolor(RGB(195, 195, 195));
		fillrectangle(980, 70, 1072, 750);

		

		//测试直线
		setlinecolor(BLUE);
		line(this->begx, this->begy, msg->x, msg->y);
		
	
		
	
	}

}

void Canvas::SetVector(Line* l)
{
	this->Shapes.push_back(l);
}

Line* Canvas::GetVector(int i)
{
	return this->Shapes[i];
}

void Canvas::painting()
{

	
	
	while (true)
	{
		//绘制
		for (int i = 0; i < this->Shapes.size(); i++)
		{
			
			this->GetVector(i)->line_draw();
			
		}

		while (peekmessage(this->Get_message(), EX_MOUSE))
		{
			
			
			switch (this->Get_message()->message)
			{
			case WM_LBUTTONDOWN:
				this->LMouseButtonDown(this->Get_message());
				break;
			case WM_LBUTTONUP:
				this->LMouseButtonUp(this->Get_message());
				break;
			case WM_MOUSEMOVE:
				this->mouseMove(this->Get_message());
				break;
			}
		
		}
	}
	
}


int main()
{
	SetWindow();

	Canvas canvas;
	canvas.painting();
	
	

	EndBatchDraw();
	closegraph();
	
	_getch();
	return 0;
}
ava
舞乐耶

2022-11-21

技术讨论社区
相关问题