首页 »VB教程 » windowsapi函数:Windows API一日一练(11)GetMessage函数 »正文
windowsapi函数:Windows API一日一练(11)GetMessage函数
来源: 发布时间:星期一, 2009年9月28日 浏览:7次 评论:0
应用 为了获取源源不断 消息 就需要 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); #def UNICODE # GetMessage GetMessageW # # GetMessage GetMessageA #end // !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 APIENTRY _tWinMain(HINSTANCE hInstance, #007 HINSTANCE hPrevInstance, #008 LPTSTR lpCmdLine, #009 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 (!InitInstance (hInstance, nCmdShow)) #025 { #026 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 (bRet -1) #036 { #037 //处理出错 #038 #039 } #040 (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) #041 { #042 TranslateMessage(&msg); #043 DispatchMessage(&msg); #044 } #045 } #046 #047 () msg.wParam; #048 } #049 第33行就是获取所有窗口 消息回来
相关文章
读者评论
发表评论
|
|