Win32 API窗口将不会打开

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

Solutions Collecting From Web of "Win32 API窗口将不会打开"

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

而不是使用WNDCLASSEX使用WNDCLASS

更改:

WNDCLASSEX WCE; WNDCLASS WCE;

删除行:

 WCE.cbSize = sizeof(WNDCLASSEX); 

更改:

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

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