Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C PostQuitMessage()无法关闭我的应用程序?_C_Winapi - Fatal编程技术网

C PostQuitMessage()无法关闭我的应用程序?

C PostQuitMessage()无法关闭我的应用程序?,c,winapi,C,Winapi,我试图在win32中编写hello world,但当我关闭主窗口时,应用程序继续运行 我的窗口程序: LRESULT CALLBACK MainWndProc(HWND hWnd, UINT msg, WPARAM wparam, LPARAM lparam) { switch (msg) { case WM_DESTROY: PostQuitMessage(0); return 0; } return DefWindowP

我试图在win32中编写hello world,但当我关闭主窗口时,应用程序继续运行

我的窗口程序:

LRESULT CALLBACK MainWndProc(HWND hWnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
    switch (msg)
    {
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    }
    return DefWindowProc(hWnd, msg, wparam, lparam);
}
事件循环:

while ((bret = GetMessage(&msg, hWndMain, 0, 0) != 0)
{
    if (bret == -1)
    {
        DWORD error = GetLastError();
        return 1;
    }
    TranslateMessage(&msg);
    DispatchMessage(&msg);
}
现在,我通过点击右上角x得到WM_DESTROY,而不是GetMessage()返回0表示WM_退出,而是返回-1,GetLastError()抱怨错误1400,这是“无效的窗口句柄”
…我很困惑。

通常您会将NULL而不是窗口句柄传递给
GetMessage()
,这可以解释为什么您会得到
错误\u无效\u窗口\u句柄
,因为在
WM\u DESTROY
和friends完成后,窗口将不再存在。
PostQuitMessage()
发布的WM_QUIT是一条线程消息,因此带有句柄的GetMessage永远不会接收它


这是一个实现细节,但PostQuitMessage甚至没有发布真正的消息,它只是线程存储的一个标志,GetMessage将在需要时自动生成消息,请参阅以获取更多细节。

通常,您会将NULL而不是窗口句柄传递给
GetMessage()
,这可以解释为什么您会得到
ERROR\u INVALID\u WINDOW\u HANDLE
,因为在
WM\u DESTROY
和friends完成后,窗口将不再存在。
PostQuitMessage()
发布的WM_QUIT是一条线程消息,因此带有句柄的GetMessage永远不会接收它

这是一个实现细节,但PostQuitMessage甚至没有发布真正的消息,它只是线程存储的一个标志,GetMessage将在需要时自动生成消息,有关更多细节,请参阅。

我个人这样做:

if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
   if(msg.message == WM_QUIT)
   {
      break;
   }

   TranslateMessage(&msg);
   DispatchMessage(&msg);
}
我个人是这样做的:

if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
   if(msg.message == WM_QUIT)
   {
      break;
   }

   TranslateMessage(&msg);
   DispatchMessage(&msg);
}