下面这段代码,关闭窗口后,没有关闭程序,谁能帮我改改:
改的时候要求不要把PeekMessage改成GetMessage
----------------------------------------------------
#include<windows.h>LRESULT CALLBACK WinProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{
switch(message)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd,message,wParam,lParam);
}
//主函数
int WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpStr,int nCmdShow)
{
WNDCLASS wc;
wc.cbClsExtra=0;
wc.cbWndExtra=0;
wc.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
wc.hCursor=LoadCursor(NULL,IDC_ARROW);
wc.hIcon=0;
wc.hInstance=hInstance;
wc.lpfnWndProc=WinProc;
wc.lpszClassName="game";
wc.lpszMenuName=0;
wc.style=CS_HREDRAW|CS_VREDRAW; RegisterClass(&wc); HWND hWnd;
hWnd=CreateWindow("game","window",WS_OVERLAPPEDWINDOW,0,0,800,600,0,0,hInstance,0); ShowWindow(hWnd,nCmdShow);
UpdateWindow(hWnd); MSG msg; while(true)
{
if(PeekMessage(&msg,hWnd,0,0,PM_REMOVE))
{
if(msg.message==WM_QUIT)
{
break;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
}
}
}

解决方案 »

  1.   

    if(msg.message==WM_QUIT)
    {
    break;
    }
      

  2.   

    去掉hWnd在PeekMessage中,
    while(true)
    {
    if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
    {
    if(msg.message==WM_QUIT)
    {
    break;
    }
    TranslateMessage(&msg);
    DispatchMessage(&msg);
    }
    else
    {
    }
    }
      

  3.   

    if(PeekMessage(&msg,hWnd,0,0,PM_REMOVE))这个好像孙鑫的书上有解释
      

  4.   

    谢谢notation的回答,按你所说改了下代码,关闭窗口后确实 退出了程序,但是我想知道,为什么把PeekMessage的第2个参数hwnd改成NULL,程序才能退出.
      

  5.   

    PeekMessage(&msg,hWnd,0,0,PM_REMOVE))
    =========
    hWnd改成NULL,因为如果第二个参数为hWnd的话,表示只接收与hWnd相关的消息,而WM_QUIT消息和任何窗体都没有关系,这样就收不到WM_QUIT消息,所以循环就没办法退出。
    初学者容易出这个错误,因为总觉得消息队列是属于窗口的,其实消息队列是属于线程的。
      

  6.   

      BOOL PeekMessage(
      LPMSG lpMsg,         // message information
      HWND hWnd,           // handle to window
      UINT wMsgFilterMin,  // first message
      UINT wMsgFilterMax,  // last message
      UINT wRemoveMsg    // removal options指定消息获取的方式,如果设为PM_NOREMOVE,那么消息将不会从消息队列中被移除;如果设为PM_REMOVE,那么消息将从消息队列中被移除(和GetMessage函数行为一致)
    );  BOOL GetMessage(
      LPMSG lpMsg,         // message information
      HWND hWnd,           // handle to window
      UINT wMsgFilterMin,  // first message
      UINT wMsgFilterMax   // last message
    );
      

  7.   

    谢谢wltg2001回复,下面是我的理解
    我用MSDN查了下WM_QUIT和WM_DESTROY:
    -------------------------------------------
    The WM_QUIT message indicates a request to terminate an application and is generated when the application calls the PostQuitMessage function. It causes the GetMessage function to return zero. A window receives this message through its WindowProc function. 
    ---------------------------------------------------
    The WM_DESTROY message is sent when a window is being destroyed. It is sent to the window procedure of the window being destroyed after the window is removed from the screen. This message is sent first to the window being destroyed and then to the child windows (if any) as they are destroyed. During the processing of the message, it can be assumed that all child windows still exist. A window receives this message through its WindowProc function.
    ------------------------------------------------------
    我理解是 当我们点击窗口上的关闭按钮时,产生了WM_DESTROY消息,hWnd 所指向的窗口被销毁;
    接着
    case WM_DESTROY:
    PostQuitMessage(0);
    return 0;
    于是产生了WM_QUIT消息;
    接着 if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
    {
    if(msg.message==WM_QUIT)
    {
    break;
    }
    TranslateMessage(&msg);
    DispatchMessage(&msg);
    }
    退出where循环!程序执行结束.
      

  8.   

    if(PeekMessage(&msg,hWnd,0,0,PM_REMOVE))这个好像孙鑫的书上有解释
      

  9.   

    我理解是 当我们点击窗口上的关闭按钮时,产生了WM_DESTROY消息,hWnd 所指向的窗口被销毁;
    接着
    case WM_DESTROY:
    PostQuitMessage(0);
    return 0;
    于是产生了WM_QUIT消息;
    ============
    差不多就是这样,不过点击窗口上的关闭按钮时,产生的是WM_CLOSE消息,这个消息一般由默认的DefWindowProc处理,在处理过程中产生WM_DESTROY消息。下面的过程和楼主说的一样。
      

  10.   

    问题终于结束了,最后谢谢大家的帮助.第一次用csdn问问题,非常感谢大家!