我没有太多的Windows编程经验,但无法明确地看到这段代码有什么问题,但窗口没有打开。尽管有时并不总是,它会在任务管理器中打开,所以我猜测它是注册类并创建窗口,但是问题出在ShowWindow()函数上。但是,我不积极。Windows编程,窗口没有打开,没有生成错误
我的理解程序的流程是: 窗口创建与注册类。 显示窗口。 连续查找在Proc窗口中处理的消息。
我觉得我已经完成了所有这些事情,所以我的理解错误,或者是我的代码丢失了什么?
谢谢。
源代码:
#include <Windows.h>
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
if (uMsg == WM_DESTROY)
{
PostQuitMessage(0);
return 0;
}
DefWindowProc(hwnd, uMsg, wParam, lParam);
}
int WINAPI wWinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow)
{
//Variable for message loop.
MSG msg;
//Setting up the window class.
WNDCLASSEX windowClass;
windowClass.cbSize = sizeof(windowClass);
windowClass.style = CS_HREDRAW | CS_VREDRAW | CS_PARENTDC;
windowClass.lpfnWndProc = WindowProc;
windowClass.hInstance = hinstance;
windowClass.hbrBackground = (HBRUSH)COLOR_WINDOW;
windowClass.lpszClassName = "WindowClass";
RegisterClassEx(&windowClass);
HWND windowHandle = CreateWindowEx(WS_EX_OVERLAPPEDWINDOW, "WindowClass", "My Program", WS_OVERLAPPEDWINDOW, 500, 200, 800, 500, NULL, NULL, hinstance, 0);
if (!windowHandle)
return FALSE;
ShowWindow(windowHandle, nCmdShow);
// Start the message loop.
while (GetMessage(&msg, NULL, 0, 0) != 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
// Return the exit code to the system.
return msg.wParam;
}
检查API调用失败并查看GetLastError说什么。 –
我会使用WNDCLASSEX windowClass = {};'以确保结构的所有元素都被初始化为零。 –
谢谢。我这样做,它的工作。 – kazama