[英]How do I get a Window to keep the painted objects when it is resized C++
My program creates random rectanges on the screen. 我的程序在屏幕上创建随机矩形。 Each time the left mouse button is clicked a new rectange of random size and colour is added to the window. 每次单击鼠标左键,都会在窗口中添加一个随机大小和颜色的新矩形。 However, when I resize the window, all of the rectanges are deleted - I don't want this to happen. 但是,当我调整窗口大小时,所有矩形均被删除-我不希望这种情况发生。 I don't understand what the program does when it is resized, is it calling the case WM_PAINT
? 我不知道该程序在调整大小时会执行什么操作,是否称为WM_PAINT
? If so, what do I need to include to ensure the current window state remains when resized. 如果是这样,我需要包括什么以确保在调整大小时仍保留当前窗口状态。
You need to save the state and redraw whenever you receive WM_PAINT. 您需要保存状态并在收到WM_PAINT时重绘。 This is simply the protocol Microsoft decided on. 这只是Microsoft决定的协议。 So make sure you keep the state of your window in memory. 因此,请确保将窗口的状态保留在内存中。
In the case of a resize you also get several sizing messages, first a series of WM_SIZING events while the size is changing and then WM_SIZE when the size is finally set. 在调整大小的情况下,您还会收到几条大小调整消息,首先是在大小更改时发生的一系列WM_SIZING事件,然后是在最终设置大小后发生的WM_SIZE事件。 You will get WM_PAINT messages in between and you are free to repaint the application upon any or all of those events. 您将在两者之间收到WM_PAINT消息,并且可以在发生任何或所有这些事件时随意重绘应用程序。
See my answer to a similar question here about painting using GDI. 在这里查看有关使用GDI绘画的类似问题的答案。
https://stackoverflow.com/a/12764607/682404 https://stackoverflow.com/a/12764607/682404
Basically, when your windows is resized, you should receive a WM_PAINT
message from Windows. 基本上,调整窗口大小时,您应该从Windows收到WM_PAINT
消息。 In your message handler, you need to repaint your form. 在您的消息处理程序中,您需要重新绘制表单。 The answer I linked in has some details about the painting process. 我链接的答案包含有关绘画过程的一些详细信息。 Your painting code would look rougly like this: 您的绘画代码看起来像这样糟糕:
PAINTSTRUCT stPaintStruct;
HDC hPaintDC = BeginPaint(hWnd, &stPaintStruct);
if (hPaintDC != HANDLE_NULL)
{
// establish clipping rect using stPaintStruct.rcPaint
if (!m_bRendering)
{
m_bRendering = TRUE;
// Render() knows the how to create the output.
Render ();
m_bRendering = FALSE;
}
EndPaint (hWnd, &stPaintStruct);
return (TRUE);
}
Every time you need to paint your window, you always need to paint from scratch, so you need to store somewhere the coordinates of your rectangles. 每次需要绘制窗口时,总是需要从头开始绘制,因此需要将矩形的坐标存储在某处。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.