mainwindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QAbstractNativeEventFilter> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow,public QAbstractNativeEventFilter { Q_OBJECT public: explicit MainWindow(QWidget *parent = nullptr); ~MainWindow(); bool nativeEventFilter(const QByteArray & eventType, void * message, long * result); private: Ui::MainWindow *ui; }; #endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QDebug> #include <windows.h> #pragma comment(lib, "user32.lib") MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); } MainWindow::~MainWindow() { delete ui; } bool MainWindow::nativeEventFilter(const QByteArray & eventType, void * message, long * result) { if (eventType == "windows_generic_MSG" || eventType == "windows_dispatcher_MSG") { MSG * pMsg = reinterpret_cast<MSG *>(message); if (pMsg->message == WM_NCMOUSEMOVE) { //获取到系统鼠标移动,可以做像qq一样的忙碌检测 qDebug() << "nativeEventFilter:"<<pMsg->pt.x; } } return false; }
main.cpp
#include "mainwindow.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); a.installNativeEventFilter(&w); return a.exec(); }