Qt处理Windows消息的方法
一、用到的头文件和函数
首先,要想在Qt工程中处理Windows消息,你必须包含对应的Windows头文件<windows.h>。如果还需要用到一些扩展的组件,则还要包含<windowsx.h>。
此外,你需要实现继承自QWidget类的虚函数nativeEvent。这个函数名也很好记,即本地事件。注意,这是一个保护类型成员。
该函数的声明为
virtual bool
nativeEvent(const QByteArray &eventType, void *message, long *result)
注意,在Windows平台上,事件类型标识为 "windows_generic_MSG",消息类型为MSG*,返回值类型为LRESULT。
二、示例
首先,我们在类中声明nativeEvent函数。
接着,我们对参数类型进行转换。
然后,就可以像往常一样通过switch来处理Windows消息了。
在此需要提醒大家注意的是,如果我们处理完相应的消息后,不再想让Qt执行默认的事件处理,就返回true,表示消息已处理;否则,就返回false,执行完指定的 *** 作后再执行默认处理。
1. bool QApplication::winEventFilter ( MSG * ) 消息程序在每次接受到消息时调用这个函数。如果你想处理Qt不处理的窗口消息msg,请重新实现这个函数。 1. bool MainWindow::winEvent(MSG* pMsg) 2. { 3. if ( pMsg->message == WM_COPYDATA ) 4. { 5. COPYDATASTRUCT* pCopyDataStruct6. POSTERS_REC_STRUCT* pRec7. unsigned char* odapMsgPtr[MAX_POSTERS_SIZE]8. QString str9. pCopyDataStruct = (COPYDATASTRUCT*) pMsg->lParam10. switch (pCopyDataStruct->dwData) 11. { 12. case VALID_REC1 : 13. case VALID_REC2 : 14. { 15. (void)memcpy(odapMsgPtr, pCopyDataStruct->lpData, pCopyDataStruct->cbData)16. if (odapMsgPtr != NULL) 17. { 18. pRec = (POSTERS_REC_STRUCT *)odapMsgPtr19. class_data1 = pRec->var120. class_data2 = pRec->var221. } 22. } 23. } 24. return true25. } 26. else 27. return false28. } 29. Some Code on the web gives another example 30. #ifdef HAVE_WIN32_API 31. virtual bool winEventFilter(MSG * msg) { 32. SPW_InputEvent sbEvent33. if (SPW_TranslateEventWin32(msg, &sbEvent)) { 34. QWidget * focus = this->focusWidget()35. if (!focus) focus = this->activeWindow()36. if (focus) { 37. QCustomEvent qevent((QEvent::Type)SoQtInternal::SPACEBALL_EVENT, 38. (void *)&sbEvent)39. QApplication::sendEvent(focus, &qevent)40. } 41. } 42. #if (QT_VERSION >= 0x040000) 43. long result = 044. return QApplication::winEventFilter(msg, &result)45. #else 46. return QApplication::winEventFilter(msg)47. #endif The QSystemTrayIcon class provides an icon for an application in the system tray.Modern operating systems usually provide a special area on the desktop, called the system tray or notification area, where long-running applications can display icons and short messages. 1. /* translates a Win32 event to a SPW_InputEvent. */ 2. int SPW_TranslateEventWin32(MSG * msg, SPW_InputEvent * sbEvent) 3. { 4. SiSpwEvent spwEvent5. SiGetEventData eventdata6. if (Spw_DeviceHandle != SI_NO_HANDLE) { 7. SiGetEventWinInit (&eventdata, msg->message, msg->wParam, msg->lParam)8. if (SiGetEvent (Spw_DeviceHandle, 0, &eventdata, &spwEvent) == SI_IS_EVENT) { 9. int i10. switch(spwEvent.type) { 11. case SI_MOTION_EVENT: 12. sbEvent->type = SPW_InputMotionEvent13. for(i=0i<6i++) { 14. sbEvent->sData[i] = (short)spwEvent.u.spwData.mData[i]15. } 16. break17. case SI_BUTTON_EVENT: 18. sbEvent->type = SPW_InputButtonPressEvent19. sbEvent->buttonState.pressed = (SiButtonPressed(&spwEvent) != SI_NO_BUTTON)20. sbEvent->buttonState.released = (SiButtonReleased(&spwEvent) != SI_NO_BUTTON)21. break22. } 23. return TRUE24. } 25. } 26. return FALSE27. } 小结:QT源码解析之Qt处理Windows消息的内容介绍完了,希望本文对你有帮助。来源:网络
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)