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

來源:互聯網
上載者:User
 應用程式為了擷取源源不斷的訊息,就需要調用函數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行就是擷取所有視窗的訊息回來。
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.