Win32 API窗口无法打开

时间:2013-02-26 02:05:11

标签: c++ windows winapi window

好吧,所以我已经花了很多时间来学习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;
}

3 个答案:

答案 0 :(得分:3)

使用WS_EX_OVERLAPPEDWINDOW作为CreateWindowEx功能的第一个参数(而不是WS_OVERLAPPEDWINDOW,这不是有效的扩展窗口样式。)

答案 1 :(得分:-1)

而不是使用WNDCLASSEX使用WNDCLASS

变化:

WNDCLASSEX WCE;WNDCLASS WCE;

删除行:

WCE.cbSize = sizeof(WNDCLASSEX);

变化:

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

答案 2 :(得分:-2)

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