windows programming, a message function in question to intercept messages

Windows windows for many years did not write a program, today encountered some problems when vulkan program based on self-control, part of the code is as follows:

LRESULT CALLBACK XWindow::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {

    switch (uMsg) {
    case WM_CLOSE:
        //PostQuitMessage(0);
        shouldClose = true;
        break;
    case WM_PAINT:
        //run(info);//return 0;
        break;
    default:
        break;
    }
    return (DefWindowProc(hWnd, uMsg, wParam, lParam));
}

int main()
{
    while (true) {//while1

        MSG msg;
        while (PeekMessage(&msg, hWnd, 0, 0, PM_REMOVE)) { //while2
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
     vulkan_render();
    }

Code above normal operation, when a message, windows processing messages, rendering is performed when there is no message function vulkan_render ().

If you change the code: the

    case WM_PAINT:
        break;

Read:

    case WM_PAINT:
        return 0;

Rendering function vulkan_render will never be executed, get out into circulation in while2.

For the following reasons:

The WM_PAINT message is generally not frequent, WINDOWS not send this message only when the window needs to be redrawn, such as the window is resized, the window is minimized and then restored, the outer window out of the screen and back again, note that the window is covered other windows does not result in redrawn happen again.

If we take this message directly return the handler, not to deal with the opportunity to DefWindowProc behind, then WINDOWS not know WM_PAINT been processed, so the next frame will send WM_PAINT message to the program. In an endless loop.

Therefore, the message handler should not return, unless you know exactly what you are doing

Guess you like

Origin www.cnblogs.com/timeObjserver/p/11334431.html