天天看點

Windows API一日一練(11)GetMessage函數

應用程式為了擷取源源不斷的消息,就需要調用函數 GetMessage 來實作,因為所有在視窗上的輸入消息,都會放到應用程式的消息隊列裡,然後再發送給視窗回調函數處理。 函數 GetMessage 聲明如下: WINUSERAPI BOOL WINAPI GetMessageA(     __out LPMSG lpMsg,     __in_opt HWND hWnd,     __in UINT wMsgFilterMin,     __in UINT wMsgFilterMax); WINUSERAPI BOOL WINAPI GetMessageW(     __out LPMSG lpMsg,     __in_opt HWND hWnd,     __in UINT wMsgFilterMin,     __in UINT wMsgFilterMax); #ifdef UNICODE #define GetMessage GetMessageW #else #define GetMessage GetMessageA #endif // !UNICODE lpMsg 是從線程消息隊列裡擷取到的消息指針。 hWnd 是想擷取那個視窗的消息,當設定為 NULL 時是擷取所有視窗的消息。 wMsgFilterMin 是擷取消息的 ID 編号最小值,如果小于這個值就不擷取回來。 wMsgFilterMax 是擷取消息的 ID 編号最大值,如果大于這個值就不擷取回來。 函數傳回值可能是 0 ,大于 0 ,或者等于 -1 。如果成功擷取一條非 WM_QUIT 消息時,就傳回大于 0 的值;如果擷取 WM_QUIT 消息時,就傳回值 0 值。如果出錯就傳回 -1 的值。   調用這個函數的例子如下: #001 // 主程式入口 #002 // #003 //  蔡軍生  2007/07/19 #004 // QQ: 9073204 #005 // #006 int APIENTRY _tWinMain(HINSTANCE hInstance, #007                       HINSTANCE hPrevInstance, #008                       LPTSTR    lpCmdLine, #009                       int       nCmdShow) #010 { #011  UNREFERENCED_PARAMETER(hPrevInstance); #012  UNREFERENCED_PARAMETER(lpCmdLine); #013  #014   // #015  MSG msg; #016  HACCEL hAccelTable; #017  #018  // 加載全局字元串。 #019  LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); #020  LoadString(hInstance, IDC_TESTWIN, szWindowClass, MAX_LOADSTRING); #021  MyRegisterClass(hInstance); #022  #023  // 應用程式初始化 : #024  if (!InitInstance (hInstance, nCmdShow)) #025  { #026         return FALSE; #027  } #028  #029  hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_TESTWIN)); #030  #031  // 消息循環 : #032  BOOL bRet; #033  while ( (bRet = GetMessage(&msg, NULL, 0, 0)) != 0) #034  { #035         if (bRet == -1) #036         { #037               // 處理出錯。 #038  #039         } #040         else if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) #041         { #042               TranslateMessage(&msg); #043               DispatchMessage(&msg); #044         } #045  } #046  #047  return (int) msg.wParam; #048 } #049    第 33 行就是擷取所有視窗的消息回來。  

轉載于:https://www.cnblogs.com/ajuanabc/archive/2007/07/20/2464333.html