What should be returned in the WM_CREATE message to indicate error?

1

I am sending the WM_SETFONT message to each control to set the source of the same and I am using the CreateFont function to create the source. The CreateFont function is called when the WM_CREATE message is received.

LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
    switch (uMsg) {
        case WM_CREATE: {
            int nHeight = -MulDiv(10, GetDeviceCaps(GetDC(hWnd), LOGPIXELSY), 72);

            SegoeUI = CreateFont(
                nHeight, 0,
                0, 0,
                FW_NORMAL,
                FALSE, FALSE, FALSE,
                DEFAULT_CHARSET,
                OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS,
                DEFAULT_QUALITY,
                DEFAULT_PITCH | FF_DONTCARE,
                _T("Segoe UI")
            );

            if (!SegoeUI) return -1;
        }
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        default:
            return DefWindowProc(hWnd, uMsg, wParam, lParam);
    }
    return 0;
}

If the CreateFont function fails it will return NULL and if the source is NULL and return -1 in WM_CREATE message. The problem is that I can not interrupt the creation of the window.

Microsoft says the following about the message WM_CREATE :

  

If an application processes this message, it should return zero to continue creation of the window. If the application returns -1, the window is destroyed and the CreateWindowEx or CreateWindow function returns a NULL handle.

The function CreateWindow should return NULL and I should be able to check if the window was created or not, but it looks like the code is stopped.

HWND hWnd = CreateWindow(
    szClassName,
    szTitle,
    dwStyle,
    CW_USEDEFAULT, CW_USEDEFAULT,
    rect.right - rect.left, rect.bottom - rect.top,
    NULL,
    NULL,
    hInstance,
    NULL
);

// daqui para baixo nada é executado

if (!hWnd) { // eu quero fazer essa verificação antes de encerrar
    MessageBox(
        NULL,
        _T("Não foi possível iniciar o programa!"),
        szTitle,
        MB_OK | MB_ICONERROR
    );

    return 0;
}
    
asked by anonymous 09.06.2016 / 02:27

1 answer

1

You are not able to check whether the window was created or not because, in WM_CREATE , after verifying that the source is NULL , should return -1 if it was OU 0 (or simply DefWindowProc(hWnd, uMsg, wParam, lParam) ). Since there is no break or return at the end, case WM_DESTROY is running, interrupting your code.

Correct as follows:

case WM_CREATE: {
            int nHeight = -MulDiv(10, GetDeviceCaps(GetDC(hWnd), LOGPIXELSY), 72);

            SegoeUI = CreateFont(
                nHeight, 0,
                0, 0,
                FW_NORMAL,
                FALSE, FALSE, FALSE,
                DEFAULT_CHARSET,
                OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS,
                DEFAULT_QUALITY,
                DEFAULT_PITCH | FF_DONTCARE,
                _T("Segoe UI")
            );

            if (!SegoeUI) return -1;
            else return DefWindowProc(hWnd, uMsg, wParam, lParam);

        }
    
29.04.2017 / 17:21