我试图在 Visual Studio 的空 C++ 项目中创建一个窗口,但是当我运行它时,它没有显示任何窗口。但是,它也没有给我任何错误。
#include <Windows.h>
using namespace std;
int CALLBACK WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow )
{
const auto pClassName = "TextClass";
WNDCLASSEX wc = {0};
wc.cbSize = sizeof(wc);
wc.style = CS_OWNDC;
wc.lpfnWndProc = DefWindowProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = nullptr;
wc.hCursor = nullptr;
wc.hbrBackground = nullptr;
wc.lpszMenuName = pClassName;
wc.hIconSm = nullptr;
RegisterClassEx(&wc);
HWND hWnd = CreateWindowEx(
0,
pClassName,
"A sad Window",
WS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU,
200, 200, 640, 480,
nullptr, nullptr, hInstance, nullptr);
ShowWindow(hWnd, SW_SHOW);
while (true);
return 0;
}
最佳答案
你打错了:你正在设置
wc.lpszMenuName = pClassName;
而不是 wc.lpszClassName。
我相信只要修正这个问题,窗口就会出现在屏幕上,但可执行文件随后会随着它一起卡在屏幕上,因为没有任何消息。
一个最小的消息循环而不是
while (true) ...
会是
MSG msg;
while( GetMessage( &msg, NULL, 0, 0 ) > 0 )
DispatchMessage( &msg );
关于c++ - 我用C++创建的窗口不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57288641/