我知道这是一个无聊的问题,我之前已经做过一百万次了,但是我无法在我编写的 WinAPI 程序中显示一个简单的窗口。它后来使用 DirectX 在窗口上绘图,但我已经调试了程序,即使在所有 DirectX 内容开始执行之前它也无法显示窗口。我只在任务栏上看到程序图标,但没有窗口。
这是处理窗口创建的一段代码。
WNDCLASSEX wc;
ZeroMemory(&wc, sizeof(wc));
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC)WndProc;
wc.hInstance = hInstance;
wc.lpszClassName = title.c_str();
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hIcon = 0;
wc.hIconSm = 0;
wc.hbrBackground = 0;
wc.lpszMenuName = 0;
if(!RegisterClassEx(&wc))
{
debug << "Failed to register window class." << std::endl;
return 0;
}
DWORD dwStyle, dwExStyle;
dwStyle = WS_OVERLAPPEDWINDOW | WS_VISIBLE;
dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
AdjustWindowRectEx(&windowRect, dwStyle, FALSE, dwExStyle);
int wwidth = windowRect.right - windowRect.left;
int wheight = windowRect.bottom - windowRect.top;
debug << "Screensize: " << wwidth << "x" << wheight << std::endl;
debug << "Creating program window..." << std::endl;
HWND hWnd = CreateWindowExW(dwExStyle, wc.lpszClassName, title.c_str(), dwStyle | WS_CLIPCHILDREN | WS_CLIPSIBLINGS, 0, 0, wwidth, wheight, NULL, NULL, hInstance, 0);
if(!hWnd)
{
debug << "Error occured while trying to display the window.\n";
return false;
}
ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd);
我绝望了。我开始觉得我的编程环境、库等有问题。我使用的是 VS2012/Win7,以及 VS 附带的标准 c++ 编译器。
编辑 WindowRect 在这里
RECT windowRect;
windowRect.left = 0;
windowRect.right = g_engine->getScreenWidth(); //800
windowRect.top = 0;
windowRect.bottom = g_engine->getScreenHeight(); //600
这是 DefWindowProc
LRESULT CALLBACK WndProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
if(msg == WM_DESTROY)
return DefWindowProc(hWnd, msg, wParam, lParam);
}