0

好的,所以我已经花时间学习了与打开窗口有关的 Win32 API,而我最后想出的代码我认为可以工作,但没有。我注册了窗口类,做了我必须做的所有事情,但是当我运行它时,什么都没有发生......如果有人能指出我做错了什么/遗漏了什么,那将是一个很大的帮助。

#include <stdlib.h>
#include <iostream>
#include <Windows.h>
#pragma comment (lib, "wsock32.lib")
#define WNDCLASSNAME "wndclass"

bool quit = false;

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
        case WM_CLOSE:
            DestroyWindow(hwnd);
            break;

        case WM_DESTROY:
            PostQuitMessage(0);
            break;

        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}

int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprevinstance, LPSTR lpcmdline, int nshowcmd)
{
    WNDCLASSEX WCE;
    WCE.cbSize = sizeof(WNDCLASSEX);
    WCE.style = CS_HREDRAW|CS_VREDRAW|CS_OWNDC|CS_DBLCLKS;
    WCE.lpfnWndProc = WndProc;
    WCE.cbClsExtra = 0;
    WCE.cbWndExtra = 0;
    WCE.hInstance = hinstance;
    WCE.hIcon = NULL;//LoadImage()
    WCE.hCursor = NULL;//LoadCursor(NULL, IDC_CROSS);
    WCE.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
    WCE.lpszMenuName = NULL;
    WCE.lpszClassName = "KyleWindow";
    WCE.hIconSm = NULL;

    RegisterClassEx(&WCE);

    HWND WindowHandle;

    WindowHandle = CreateWindowEx(WS_OVERLAPPEDWINDOW, "KyleWindow", "Xerus", WS_OVERLAPPEDWINDOW, 0, 0, 500, 500, NULL, NULL, hinstance, NULL);

    ShowWindow(WindowHandle, SW_SHOWNORMAL);
    UpdateWindow(WindowHandle);

    std::cout<<"'Opened' Window"<<std::endl;

    MSG msg;

    while(!quit)
    {
        if(PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE))
        {
            if(msg.message == WM_QUIT)
                quit = true;
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }
    return msg.lParam;
}
4

3 回答 3

3

用作函数WS_EX_OVERLAPPEDWINDOW的第一个参数CreateWindowEx(而不是WS_OVERLAPPEDWINDOW,它不是有效的扩展窗口样式)。

于 2013-02-26T03:00:17.213 回答
-1

而不是使用WNDCLASSEX使用WNDCLASS

改变:

WNDCLASSEX WCE;WNDCLASS WCE;

删除行:

WCE.cbSize = sizeof(WNDCLASSEX);

改变:

RegisterClassEx(&WCE);RegisterClass(&WCE);

于 2013-07-26T08:44:16.370 回答
-2

函数 int WINAPI WinMain 必须在函数 LRESULT CALLBACK WndProc 之前。编译器按顺序读取。

于 2013-05-09T01:28:09.593 回答