Win32 WM_PAINT 和子窗口

发布于 2024-08-07 20:51:27 字数 1862 浏览 3 评论 0原文

如何在子窗口内绘图?

我认为我应该使用一些 WndProc (没有 WM_PAINT)创建主窗口 CreateWindow(WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN) 。在其 WM_CREATE 上,我使用另一个对 WM_PAINT 做出反应的 WndProc2 创建另一个窗口 CreateWindow(WS_CHILD | WS_CLIPCHILDREN)。 然而,另一个处理程序似乎进入了无限循环。我做错了什么?

请问,你没有建议或例子吗?

PS: WS_CLIPCHILDREN 似乎不会影响这一点,并且 WndProc 默认为 DefWindowProc

代码:


LRESULT CALLBACK Proc2(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
   switch(msg)
    {
        case WM_CREATE:
            printf("-------\n");
            return 0;
        case WM_PAINT:
            printf("-");
            return 0;

        default:
            return DefWindowProc(hWnd, msg, wParam, lParam);
    }
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
...
case WM_CREATE:
    CreateWindowClass(hInstance, Proc2, "Window2");
    w2 = CreateWindowEx(WS_EX_STATICEDGE, "Window2", "Win", WS_CHILD | WS_CLIPCHILDREN, 0, 0, 100, 100, hWnd, NULL, hInstance, NULL);


void createWindowClass(HINSTANCE hInstance, WNDPROC WndProc, LPCSTR lpszClassName)
{
    WNDCLASSEX wc;
    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = 0;
    wc.lpfnWndProc = WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    wc.lpszMenuName  = "test";
    wc.lpszClassName = lpszClassName;
    RegisterClassEx(&wc);
}

int WINAPI WinMain
...
createWindowClass(hInstance, WndProc, "MainWindow");
w = CreateWindow("MainWindow", "Main", WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);


How to draw inside a child window?

I thought I should create the main window CreateWindow(WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN) with some WndProc (without WM_PAINT). On its WM_CREATE I create another window CreateWindow(WS_CHILD | WS_CLIPCHILDREN) with another WndProc2 which reacts to WM_PAINT.
However, it seems that the another handler enters an infinite loop. What am I doind wrong?

Please, don't you have an advice or examle?

PS: WS_CLIPCHILDREN doesn't seem to effect this, and both WndProc default to DefWindowProc

The code:


LRESULT CALLBACK Proc2(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
   switch(msg)
    {
        case WM_CREATE:
            printf("-------\n");
            return 0;
        case WM_PAINT:
            printf("-");
            return 0;

        default:
            return DefWindowProc(hWnd, msg, wParam, lParam);
    }
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
...
case WM_CREATE:
    CreateWindowClass(hInstance, Proc2, "Window2");
    w2 = CreateWindowEx(WS_EX_STATICEDGE, "Window2", "Win", WS_CHILD | WS_CLIPCHILDREN, 0, 0, 100, 100, hWnd, NULL, hInstance, NULL);


void createWindowClass(HINSTANCE hInstance, WNDPROC WndProc, LPCSTR lpszClassName)
{
    WNDCLASSEX wc;
    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = 0;
    wc.lpfnWndProc = WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    wc.lpszMenuName  = "test";
    wc.lpszClassName = lpszClassName;
    RegisterClassEx(&wc);
}

int WINAPI WinMain
...
createWindowClass(hInstance, WndProc, "MainWindow");
w = CreateWindow("MainWindow", "Main", WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);


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

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

发布评论

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

评论(1

帅气尐潴 2024-08-14 20:51:27

您应该调用 BeginPaint 和 EndPaint 来响应 WM_PAINT 消息来验证窗口。否则,系统认为您的窗口尚未绘制,因此它将再次(一次又一次)发送绘制消息。请参阅 Microsoft 文档。

You should be calling BeginPaint and EndPaint in response to the WM_PAINT message to validate the window. Otherwise, the system thinks that your window has not been painted and so it will send the paint message again (and again). See the Microsoft documentation.

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