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();
    }
    
  • 相关阅读:
    第五章 数据的共享与保护
    实验6
    实验5
    实验4 类与对象2)
    实验三 类与对象
    实验2
    2018—3-21第二章程序例题(2)
    第二章思维导图
    2018—3-18C++第二章程序例题
    汇编实验九
  • 原文地址:https://www.cnblogs.com/xiang--liu/p/12034608.html
Copyright © 2011-2022 走看看