Search code examples
windows-8wndproc

CreateWindow never triggers WM_CREATE


I have the following bit of code;

#include <windows.h>

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){
    printf("%d\n", message);

    return 0;
}

int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){

    WNDCLASSEX wc = {0};

    wc.cbSize = sizeof(WNDCLASSEX);

    wc.lpfnWndProc   = WndProc;
    wc.hInstance     = hInstance;
    wc.hbrBackground = (HBRUSH)(COLOR_BACKGROUND);
    wc.lpszClassName = "oglversionchecksample";
    wc.style = CS_OWNDC;

    if(!RegisterClassEx(&wc))
        return 1;

    CreateWindow(wc.lpszClassName, "openglversioncheck", WS_OVERLAPPED, 0, 0, 640, 480, 0, 0, hInstance, 0);

    return 0;
}

Calling CreateWindow() triggers message numbers 36 WM_GETMINMAXINFO, 129 WM_NCCREATE, and then 130 WM_NCDESTROY, but message number 1 WM_CREATE is never triggered like it's supposed to be.

What am I doing wrong that's causing WM_CREATE to not be triggered?


Solution

  • It doesn't trigger any message, because message loop is not called yet.

    You have to put this after CreateWindow

    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    

    Also, WndProc should not simply return zero. It should return the following:

    return DefWindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    

    Visual Studio or other IDE's can create C++ -> Win32 project basic project, it sets up all the code.