【发布时间】:2014-03-15 19:59:27
【问题描述】:
我想让 WndProc 成为一个类成员函数,我找到了this article,所以我尝试将它应用到最简单的 Win32 程序中,它只是创建一个空白窗口,这是 Win32 的第一步。
int Myclass::Start(HINSTANCE hInstance, int nCmdShow)
{
if (FAILED(InitWindow(hInstance, nCmdShow)))
return 0;
MSG msg = { 0 };
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;;
}
LRESULT Myclass::StaticWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
Myclass* pThis = nullptr;
if (message == WM_NCCREATE) {
LPCREATESTRUCT lpcs = reinterpret_cast<LPCREATESTRUCT>(lParam);
pThis = static_cast<Myclass*>(lpcs->lpCreateParams);
SetWindowLongPtr(hWnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(pThis));
}
else {
pThis = reinterpret_cast<Myclass*>(GetWindowLongPtr(hWnd, GWLP_USERDATA));
}
if(pThis)
return pThis->RealWndProc(hWnd, message, wParam, lParam);
return DefWindowProc(hWnd, message, wParam, lParam);
}
LRESULT Myclass::RealWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
它运行良好,但是当我关闭窗口时,程序停留在消息循环中并且不会退出。
我发现WM_DESTROY没有传递给RealWndProc(),所以没有调用PostQuitMessage()。
如果我插入 if(WM_DESTROY == message) { PostQuitMessage(0);返回0; } 在 StaticWndProc 的最后一行之前,然后程序退出。但我不确定这是否是一个好方法。
如何让 RealWndProc() 使用 WM_DESTROY?
【问题讨论】: