1.派生QAbstractNativeEventFilter,重写nativeEventFilter()函数
#ifndef CMYMESSAGEHANDLER_H
#define CMYMESSAGEHANDLER_H
#include <QAbstractNativeEventFilter>
#define WM_SEND_MY_MESSAGE WM_USER+1000
class CMyMessageHandler : public QAbstractNativeEventFilter
{
//Q_OBJECT //这个不需要...
public:
virtual bool nativeEventFilter(const QByteArray &eventType, void *message, long *result) Q_DECL_OVERRIDE;
//Signals:
// void SigTest() //新增加信号SigTest()。
};
#endif // CMYMESSAGEHANDLER_H
#include "CMyMessageHandler.h"
#include <windows.h>
#pragma comment(lib, "user32.lib")
bool CMyMessageHandler::nativeEventFilter( const QByteArray &eventType, void *message, long *result )
{
MSG* pMsg = reinterpret_cast<MSG*>(message);
if(pMsg->message == WM_SEND_MY_MESSAGE)
{
// ....处理
return true;
}
return false;
}
2.main函数添加类对象
#include <QtWidgets/QApplication>
#include "CMyMessageHandler.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
ChateRoom w;
w.show();
//------------------main函数添加----------------
CMyMessageHandler myMsgHanler;
a.installNativeEventFilter(&myMsgHanler);
//----------------------------------------------
return a.exec();
}