如何在WinApi中创建窗口来解决故障?

时间:2019-05-19 17:52:47

标签: c++ winapi dev-c++

窗口说明成功注册,但窗口创建失败。

我已经尝试使用CreateWindowEx更改文本的类型,但没有任何改变。

似乎由于任何错误,变量hMainWindow的值都为null,但我无法确定代码中的错误。

#include <windows.h>

LRESULT CALLBACK WindowProcedure(HWND, UINT, WPARAM, LPARAM);
char szClassName[] = "TestWindow";

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCommandLine, int nCommandShow) {

    HWND hMainWindow;
    WNDCLASSEX wcex;
    wcex.hInstance = hInstance;
    wcex.lpszClassName = szClassName;
    wcex.lpfnWndProc = WindowProcedure;
    wcex.style = CS_HREDRAW | CS_VREDRAW;
    wcex.cbSize = sizeof(WNDCLASSEX);
    wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
    wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground = (HBRUSH)(COLOR_3DFACE + 1);
    wcex.lpszMenuName = NULL;
    wcex.cbClsExtra = NULL;
    wcex.cbWndExtra = NULL;

    if(!RegisterClassEx(&wcex)) {
        MessageBox(NULL, "Class registration failed", "Error", MB_ICONERROR);
        return NULL;
    }

    hMainWindow = CreateWindow(szClassName, "Hello, Window", WS_OVERLAPPEDWINDOW | WS_VSCROLL, CW_USEDEFAULT, NULL, 400, 300, NULL, NULL, hInstance, NULL);

    if(!hMainWindow) {
        MessageBox(NULL, "Window creation failed", "Error", MB_ICONERROR);
        return NULL;
    }

    ShowWindow(hMainWindow, nCommandShow);
    UpdateWindow(hMainWindow);

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

    return msg.wParam;
}

LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
    return NULL;
}

0 个答案:

没有答案