1. 程式人生 > >討論:GetMessage()的第二個引數使用

討論:GetMessage()的第二個引數使用

在Windows程式設計中,關於訊息處理中的GetMessage,MSDN中有如下描述:

The GetMessage function retrieves a message from the calling thread's message queue and places it in the specified structure. This function can retrieve both messages associated with a specified window and thread messages posted via the PostThreadMessagefunction. The function retrieves messages that lie within a specified range of message values. GetMessage

does not retrieve messages for windows that belong to other threads or applications.

程式通過GetMesage從呼叫執行緒的訊息佇列中取出一條一條的訊息。

BOOL GetMessage(
  LPMSG
lpMsg,         // address of structure with message
  HWND hWnd,           // handle of window
  UINT wMsgFilterMin// first message
  UINT wMsgFilterMax   // last message
);


其中第二個引數是視窗控制代碼。 hWnd Handle to the window whose messages are to be retrieved. One value has a special meaning:
Value Meaning
NULL GetMessage retrieves messages for any window that belongs to the calling thread and thread messages posted to the calling thread via PostThreadMessage
.

在具體嘗試中,出現瞭如下情形:

情形1(以下是部分關於訊息處理的

...........

MSG msg;
 while(GetMessage(&msg,NULL,0,0))
 {
  TranslateMessage(&msg);
  DispatchMessage(&msg);
 }

 ..........

 case WM_CLOSE:
  if( IDYES==MessageBox(hwnd,"就這樣結束?","Wave-p",MB_YESNO) )
  {
   DestroyWindow(hwnd);
  }  
  break;
 case WM_DESTROY:
  PostQuitMessage(0);
  break;
 default:
  return DefWindowProc(hwnd,uMsg,wParam,lParam);

............

情形2:

將情形1中的 while(GetMessage(&msg,NULL,0,0))
變成     while(GetMessage(&msg,hwnd0,0))          

執行程式過程中,生成可執行檔案test.exe對比出現問題:

當關閉視窗時,彈出對話方塊“就這樣結束?”選擇“是”,視窗關閉。

在情形1下,開啟工作管理員(task manager)的程序(process)會發現,關閉之前test.exe的CPU佔用率是0%,記憶體使用是2840k,關閉之後test.exe會自動結束(end process)。

在情形2下,關閉之前test.exe的CPU佔用率是0%,記憶體使用是2868k,關閉之後test.exe依然存在,沒有自動結束,而且CPU 使用率是98%,記憶體佔用是2980K。

請問:這是什麼原因?