C++ win32项目双缓冲

发布于 2024-10-21 13:16:34 字数 1009 浏览 3 评论 0原文

我有一个 win32 应用程序,我想通过拖动鼠标来画一条线。我也使用双缓冲,但问题是它在鼠标路径中绘制多条线。这是我的绘图代码:

hdc = BeginPaint(hWnd, &ps);
hdcBack = CreateCompatibleDC(hdc); 
GetClientRect(hWnd, &windowRect); 
backBuffer = CreateCompatibleBitmap(hdc, windowRect.right, windowRect.bottom);
SelectObject(hdcBack, backBuffer); 
FloodFill(hdcBack, 0, 0, RGB(255, 255, 255)); 
BitBlt(hdcBack,0,0,windowRect.right,windowRect.bottom,hdc,0,0,SRCCOPY);
color = RGB(rand() % 255, rand() % 255, rand() % 255); 
hBrush = CreateSolidBrush(color); 
SelectObject (hdcBack, hBrush); 

MoveToEx(hdcBack,x1,y1,NULL); //x1,y1,x2,y2 are the initial click point and the current position of the mouse when keeping the left button down and dragging
LineTo(hdcBack,x2,y2);
BitBlt(hdc, 0, 0, windowRect.right, windowRect.bottom, hdcBack, 0, 0, SRCCOPY);

DeleteObject(hBrush); 
DeleteDC(hdcBack);
DeleteObject(backBuffer);
EndPaint(hWnd, &ps);

我也尝试在绘制线条之前不将背景复制到缓冲区中,并且它会正确绘制线条,但是当我绘制新线条时,之前绘制的线条会消失。 那么如何使用双缓冲绘制多条线并保留之前绘制的线呢?

I have a win32 application and i want to draw a line by dragging the mouse. I use double buffering as well but the problem is it draws multiple lines in the path of the mouse. Here is my drawing code:

hdc = BeginPaint(hWnd, &ps);
hdcBack = CreateCompatibleDC(hdc); 
GetClientRect(hWnd, &windowRect); 
backBuffer = CreateCompatibleBitmap(hdc, windowRect.right, windowRect.bottom);
SelectObject(hdcBack, backBuffer); 
FloodFill(hdcBack, 0, 0, RGB(255, 255, 255)); 
BitBlt(hdcBack,0,0,windowRect.right,windowRect.bottom,hdc,0,0,SRCCOPY);
color = RGB(rand() % 255, rand() % 255, rand() % 255); 
hBrush = CreateSolidBrush(color); 
SelectObject (hdcBack, hBrush); 

MoveToEx(hdcBack,x1,y1,NULL); //x1,y1,x2,y2 are the initial click point and the current position of the mouse when keeping the left button down and dragging
LineTo(hdcBack,x2,y2);
BitBlt(hdc, 0, 0, windowRect.right, windowRect.bottom, hdcBack, 0, 0, SRCCOPY);

DeleteObject(hBrush); 
DeleteDC(hdcBack);
DeleteObject(backBuffer);
EndPaint(hWnd, &ps);

I tried also not copying the background into the buffer before drawing the line and it draws the line correctly but when i draw a new line the previously drawn line disappears.
So how can i draw multiple lines with double buffering and keeping the previous drawn lines?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

美羊羊 2024-10-28 13:16:34

如果我正确理解了需求,那么问题是设计中固有的。

第一个 BitBlt()先前内容复制到缓冲区,然后绘制线条,然后将更改应用到屏幕,这会给出您想要的确切结果描述的。这样,您只是添加图形而不是替换图形,并且您会看到多行而不是一行。

如果你想显示一条被鼠标拖动的线,你需要首先用你想要的任何背景填充后台缓冲区(称之为常量数据),然后在上面绘画每次鼠标移动时相关图形(称之为变化数据)。不管怎样,我相信评论第一个 BitBlt() 应该可以解决问题。

另外,您在调用使用笔的线条函数之前要选择画笔。该调用不应该在调用 FloodFill() 之前进行吗?

编辑:

按照我对您的评论中的建议,使用第三个缓冲区来保存最新数据。在“鼠标松开”处理程序中,最后在缓冲区上绘制新线。
因此,当用户最终确定如何在鼠标移动处理程序和绘制处理程序中从该缓冲区中读取,并在鼠标释放处理程序中写入他想要画出自己的底线。

The problem's inherent in the design, if I understand the requirement correctly.

The first BitBlt() copies the previous contents on to the buffer, then you draw the line, and then you apply the changes to the screen, which gives the exact results you described. This way you're only adding graphics rather than replacing, and you see several lines instead of one.

If you want to display a line that's being dragged by the mouse, you need to first fill the back-buffer with whatever background you had in mind (call it the constant-data), and paint on it the relevant graphics (call it changing-data) every mouse move. Anyway, I believe that commenting that first BitBlt() should do the trick.

Also, you're selecting a brush before calling the line functions, which use a pen. Shouldn't that call come before calling FloodFill()?

EDIT:

Use a third buffer to hold the most recent data as suggested in my comment to yours. In your "mouse-up" handler, finally draw the new line on that buffer.
So you read from that buffer in your mouse-move handler and on-paint handler, and write to it in the mouse-up handler, when the user is finally sure how he wants his line drawn.

情深如许 2024-10-28 13:16:34

不使用后台缓冲区进行设计的一种方法是保留每次绘制的线条的动态列表。

您的消息过程可能如下所示:

  • On WM_LBUTTONDOWN ,以当前位置作为开始向列表添加新行。
  • WM_MOUSEMOVE 上(按下左键),将列表中最新行的结束位置更改为当前鼠标位置。

在每次重绘时,您只需迭代列表并绘制每条线。

One way to design this without using a backbuffer is to keep a dynamic list of lines that you draw each time.

Your message proc could look something like this:

  • On WM_LBUTTONDOWN, add a new line to the list with the current position as a start.
  • On WM_MOUSEMOVE (with left button down), change the end position of the latest line in the list to the current mouse pos.

On each redraw you simply iterate through the list and draw each line.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文