CreateWindow失败,错误50(不支持请求。)

时间:2017-04-20 09:31:32

标签: c windows winapi createwindow

我在这里做错了,但错误信息并没有给我任何线索。对CreateWindow的调用总是失败(返回NULL,GetLastError()返回50)。我想要的只是一个简单的空白窗口,但显然我的请求“不受支持”。

// gcc basic.c -o basic.exe -mwindows
#include <stdio.h>
#include <windows.h>

// Function prototypes.
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int);
BOOL InitApplication(HINSTANCE);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM);

// Application entry point.
int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    MSG msg;
    if (!InitApplication(hinstance))
        return FALSE;

    if (!InitInstance(hinstance, nCmdShow))
        return FALSE;

    BOOL fGotMessage;
    while ((fGotMessage = GetMessage(&msg, (HWND) NULL, 0, 0)) != 0 && fGotMessage != -1)
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return msg.wParam;
}

BOOL InitApplication(HINSTANCE hinstance)
{
    WNDCLASSEX wcx;
    wcx.cbSize = sizeof (wcx);          // size of structure
    wcx.style = CS_HREDRAW | CS_VREDRAW;
    wcx.lpfnWndProc = MainWndProc;     // points to window procedure
    wcx.cbClsExtra = 0;                // no extra class memory
    wcx.cbWndExtra = 0;                // no extra window memory
    wcx.hInstance = hinstance;         // handle to instance
    wcx.hIcon = NULL;
    wcx.hCursor = NULL;
    wcx.hbrBackground = GetStockObject(WHITE_BRUSH);                  // white background brush
    wcx.lpszMenuName =  NULL;    // name of menu resource
    wcx.lpszClassName = "MainWClass";  // name of window class
    wcx.hIconSm = NULL; // small class icon
    return RegisterClassEx(&wcx);
}

BOOL InitInstance(HINSTANCE hinstance, int nCmdShow)
{
    HWND hwnd;

    // Create the main window.
    hwnd = CreateWindow(
        "MainWClass",        // name of window class
        "Sample",            // title-bar string
        WS_OVERLAPPEDWINDOW, // top-level window
        CW_USEDEFAULT,       // default horizontal position
        CW_USEDEFAULT,       // default vertical position
        CW_USEDEFAULT,       // default width
        CW_USEDEFAULT,       // default height
        (HWND) NULL,         // no owner window
        (HMENU) NULL,
        hinstance,           // handle to application instance
        (LPVOID) NULL);      // no window-creation data

    if (!hwnd) {
        int error_code = GetLastError();
        char caption[256];
        snprintf(caption, sizeof caption, "CreateWindow: error %d", error_code);
        MessageBox(NULL, caption, "CreateWindow error", MB_OK);
        return FALSE;
    }

    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);
    return TRUE;
}

LRESULT CALLBACK
MainWndProc(HWND hwnd, UINT uMsg, WPARAM wparam, LPARAM lparam)
{
    switch (uMsg) {
    case WM_CLOSE:
        DestroyWindow(hwnd);
        return 1;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        DefWindowProc(hwnd, uMsg, wparam, lparam);
    }
    return 0;
}

1 个答案:

答案 0 :(得分:0)

您的窗口程序已损坏。而不是

DefWindowProc(hwnd, uMsg, wparam, lparam);

你必须写

return DefWindowProc(hwnd, uMsg, wparam, lparam);

此外,对于WM_CLOSE,文档说:

  

如果应用程序处理此消息,则应返回零。

您不遵守该规则。