zoukankan      html  css  js  c++  java
  • qt捕获全局windows消息

    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();
    }
    
  • 相关阅读:
    vs 编译加速
    leetcode 1405
    sort 从大到小从小到大
    leetcode1404
    leetcode 1406
    leetcode 1556
    杭电研究生自动登录联网脚本
    Cannot mix incompatible Qt library (version ) with this library (version ) 解决方案
    目标检测算法优化技巧
    特征工程和模型融合--机器学习--思维导图和笔记
  • 原文地址:https://www.cnblogs.com/xiang--liu/p/12034608.html
Copyright © 2011-2022 走看看