Qt-捕获Windows消息
Qt4版本的實現
方法1:
通過繼承QWidget的類中重新實現winEvent接口,以接收在消息參數中傳遞的本機Windows事件。
bool QWidget::winEvent(MSG *message, long *result)
1
方法2:
通過繼承QCoreApplication的類中重新實現winEventFilter接口,以接收在消息參數中傳遞的本機Windows事件。
bool QCoreApplication::winEventFilter(MSG *msg, long *result)
1
Qt5版本實現
方法1:
通過繼承QWidget的類中重新實現winEvent接口,以接收在消息參數中傳遞的eventType標識的本機平臺事件。
bool QWidget::nativeEvent(const QByteArray &eventType, void *message, long *result)
1
方法2:
通過繼承QAbstractNativeEventFilter的類中重新實現nativeEventFilter接口:
bool QAbstractNativeEventFilter::nativeEventFilter(const QByteArray &eventType, void *message, long *result)
1
并安裝到中:
void QCoreApplication::installNativeEventFilter(QAbstractNativeEventFilter *filterObj)
1
或安裝到:
void QAbstractEventDispatcher::installNativeEventFilter(QAbstractNativeEventFilter *filterObj)
1
特別地:不同平臺對應的eventType類型有:
平臺? ? ? ? ? ? ? ? 事件類型(eventType)? ? ? ? ? ? 消息類型(message)? ? ? ? ?結果類型(result)
Windows? ? ? ? “windows_generic_MSG”? ? ? MSG *? ? ? ? ? ? ? ? ? ? ? ? ? ? ? LRESULT
macOs? ? ? ? ? ?“NSEvent”? ? ? ? ? ? ? ? ? ? ? ? ? ? ? NSEvent *? ? ? ? ? ? ? ? ? ? ? ? 無
XCB(Linux)?? ?“xcb_generic_event_t”? ? ? ? ? ?xcb_generic_event_t *? ? ?無
/******************************************************
繼承Qt的基類QAbstractNativeEventFilter
class HHNativeEventFilter : public QAbstractNativeEventFilter
{
protected:
? ? bool nativeEventFilter(const QByteArray &eventType, void *message, long *)
? ? {
? ? ? ? if (eventType == "windows_generic_MSG"
? ? ? ? ? ? ? ? || eventType == "windows_dispatcher_MSG")
? ? ? ? {
? ? ? ? ? ? PMSG msg = static_cast<PMSG>(message);
? ? ? ? ? ? if(msg->message == WM_CLOSE )
? ? ? ? ? ? {
? ? ? ? ? ? ? ? qApp->exit();
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? return false;
? ? }
};
利用QApplication注冊類對象
app.installNativeEventFilter(new NativeEventFilter);
?
總結
以上是生活随笔為你收集整理的Qt-捕获Windows消息的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Qt-窗口消息处理机制及拦截消息的五种方
- 下一篇: js 实现用window.print()