简体   繁体   English

我如何获得一个窗口来调整大小的C ++时保留绘制的对象

[英]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.

相关问题 如何在Windows C ++ CLR中跟踪成千上万的绘制形状 - How to keep track of thousands of painted shapes in Windows C++ CLR 带有附加窗口的C ++ Window应用程序打开。 如何摆脱第二个窗口? - C++ Window application opening with an additional window. How do I get rid of the second window? C ++ C#包装器相关。 如何从C#窗口获取hwnd并将其传递给C ++? - C++ C# wrapper related. How do I get a hwnd from C# window and pass it to C++? 当我用C ++杀死一个pThread时,堆栈上的对象的析构函数会被调用吗? - When I kill a pThread in C++, do destructors of objects on stacks get called? 在C ++中,当我通过值将对象传递给函数时,是否会将对象复制到堆栈中? - In C++, Do objects get copied to the stack when I pass them to a function by value? C ++如何在调用函数时防止编译器进行隐式转换? - C++ how do i keep compiler from making implicit conversions when calling functions? 如何跟踪C ++对象 - How to keep track of C++ objects 调整窗口大小时,公共控件未正确绘制 - Common controls are not properly painted when I resize window 当我编译CLR C ++应用程序的“发布”版本时,除了GUI外,我还弹出一个cmd窗口。 我该如何解决? - When I compile a “release” build of CLR C++ application I get a cmd window popping up in addition to the GUI. How do I resolve this? 使用C ++如何获取在桌面上打开的窗口的rgb值? - using c++ how do I get the rgb values of a window open on my desktop?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM