我试图为DirectX创建一个窗口,但是由于某种原因,当创建窗口时,在按X键后不能关闭它,它就变成了一个僵尸进程。我发现GetMessage循环在按X之后根本没有调用WndProc,我试图找到一个解决方案,但没有成功,所以我想问社区。请告诉我。
#include <Windows.h>
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int
nCmdShow)
{
//register windows class
const wchar_t* pClassName = L"Senko Interaction";
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 = nullptr;
wc.lpszClassName = pClassName;
wc.hIconSm = nullptr;
RegisterClassEx(&wc);
//create window instance
HWND hWnd = CreateWindowEx(
0,
pClassName,
L"Senko Interaction",
WS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU,
0, 0, 1280, 720,
nullptr, nullptr, hInstance, nullptr
);
ShowWindow(hWnd, SW_SHOW);
//create message loop
MSG msg;
BOOL gResult;
while ((gResult = GetMessage(&msg, nullptr, 0, 0)) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
if (gResult == -1)
{
return -1; //return -1
}
else
{
return msg.wParam; //return 0
}
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CLOSE:
PostQuitMessage(1);
break;
}
return DefWindowProc(hWnd, message, wParam, lParam); //return 1
}发布于 2022-03-10 20:23:28
您将窗口类设置为对DefWindowProc()使用WndProc()而不是对lpfnWndProc使用WndProc(),所以为什么从来不调用WndProc()是有意义的:
wc.lpfnWndProc = DefWindowProc; // WRONG这应该是:
wc.lpfnWndProc = WndProc; // CORRECThttps://stackoverflow.com/questions/71430133
复制相似问题