What should be returned in WM_CREATE message to indicate error?

Asked

Viewed 38 times

1

I’m sending the message WM_SETFONT for each control to define the source of the same and I am using the function CreateFont to create the source. The function CreateFont is called when the message WM_CREATE 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 function CreateFont fail she will return NULL and if the source is NULL I return -1 in the message WM_CREATE. The problem is I’m having trouble interrupting the window creation.

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 verify whether the window was created or not, but it looks like the code is broken.

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

1 answer

1

You are not able to verify whether the window was created or not because, in WM_CREATE, after checking if the source is NULL, should return -1 if it were OR 0 (or simply DefWindowProc(hWnd, uMsg, wParam, lParam)). As there is no breakor return at the end, is being executed the case WM_DESTROY, 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);

        }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.