Win32 WM_PAINT和子窗口

时间:2009-10-13 20:33:27

标签: winapi paint onpaint

如何在子窗口内绘制?

我认为我应该使用CreateWindow(WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN)(不使用WndProc)创建主窗口WM_PAINT。在WM_CREATE上,我创建了另一个CreateWindow(WS_CHILD | WS_CLIPCHILDREN)窗口,其中WndProc2WM_PAINT作出反应。 但是,似乎另一个处理程序进入无限循环。我做错了什么?

拜托,你有没有建议或考试?

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);


1 个答案:

答案 0 :(得分:5)

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