• Qt+Hook实现全局鼠标背景色


    具体效果如上图:

    实现方法:

    1.使用windows HOOK,获取全局鼠标位置,通过信号传递给qwidget调整窗口位置

    2.设置鼠标事件穿透,屏蔽窗口的点击事件

    3.绘制半透明背景色

    4.使用windows hook将鼠标的点击事件,通过qt信号槽函数传递给窗口,实时更换点击时背景色

    5.不显示状态栏图标,使用小图标退出程序

    1. hookutil.h

    1. // MIT License
    2. // Copyright (c) 2022 hooy
    3. // Permission is hereby granted, free of charge, to any person obtaining a copy
    4. // of this software and associated documentation files (the "Software"), to deal
    5. // in the Software without restriction, including without limitation the rights
    6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    7. // copies of the Software, and to permit persons to whom the Software is
    8. // furnished to do so, subject to the following conditions:
    9. // The above copyright notice and this permission notice shall be included in all
    10. // copies or substantial portions of the Software.
    11. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    12. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    13. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    14. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    15. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    16. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    17. // SOFTWARE.
    18. #ifndef HOOKUTIL_H
    19. #define HOOKUTIL_H
    20. #ifdef _WIN32
    21. #include <windows.h>
    22. #endif
    23. int startMouseHook();
    24. bool stopMouseHook();
    25. #endif // HOOKUTIL_H

     2. hoolutil.cpp

    1. // MIT License
    2. // Copyright (c) 2022 hooy
    3. // Permission is hereby granted, free of charge, to any person obtaining a copy
    4. // of this software and associated documentation files (the "Software"), to deal
    5. // in the Software without restriction, including without limitation the rights
    6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    7. // copies of the Software, and to permit persons to whom the Software is
    8. // furnished to do so, subject to the following conditions:
    9. // The above copyright notice and this permission notice shall be included in all
    10. // copies or substantial portions of the Software.
    11. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    12. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    13. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    14. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    15. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    16. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    17. // SOFTWARE.
    18. #include "hookutil.h"
    19. #include <mousehistory.h>
    20. #include <QDebug>
    21. static HHOOK hMouseHook = NULL;
    22. HMODULE ModuleFromAddress(PVOID pv)
    23. {
    24. MEMORY_BASIC_INFORMATION mbi;
    25. if (VirtualQuery(pv, &mbi, sizeof(mbi)) != 0)
    26. {
    27. return (HMODULE)mbi.AllocationBase;
    28. }
    29. else
    30. {
    31. return NULL;
    32. }
    33. }
    34. bool stopMouseHook()
    35. {
    36. if (hMouseHook == NULL)
    37. {
    38. return FALSE;
    39. }
    40. UnhookWindowsHookEx(hMouseHook);
    41. hMouseHook = NULL;
    42. return TRUE;
    43. }
    44. LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam)
    45. {
    46. if(HC_ACTION == nCode){
    47. if (WM_LBUTTONDOWN == wParam || WM_RBUTTONDOWN == wParam
    48. || WM_MBUTTONDOWN == wParam)
    49. {
    50. if (MouseHistory::instance())
    51. {
    52. MouseHistory::instance()->setPressEvent(1);
    53. }
    54. }
    55. if (WM_LBUTTONUP == wParam || WM_RBUTTONUP == wParam
    56. || WM_MBUTTONUP == wParam)
    57. {
    58. if (MouseHistory::instance())
    59. {
    60. MouseHistory::instance()->setPressEvent(2);
    61. }
    62. }
    63. if (MouseHistory::instance())
    64. {
    65. PMSLLHOOKSTRUCT p = (PMSLLHOOKSTRUCT)lParam;
    66. MouseHistory::instance()->setPosValue(p->pt.x, p->pt.y);
    67. }
    68. }
    69. return CallNextHookEx(hMouseHook, nCode, wParam, lParam);
    70. }
    71. BOOL startMouseHook()
    72. {
    73. if (hMouseHook != NULL)
    74. {
    75. return FALSE;
    76. }
    77. hMouseHook = SetWindowsHookEx(WH_MOUSE_LL, MouseProc, ModuleFromAddress((PVOID)MouseProc), NULL);
    78. if (NULL == hMouseHook)
    79. {
    80. qDebug() << "regiter hook for mouse failed";
    81. return FALSE;
    82. }
    83. return TRUE;
    84. }

    3. mainwindow.h

    1. // MIT License
    2. // Copyright (c) 2022 hooy
    3. // Permission is hereby granted, free of charge, to any person obtaining a copy
    4. // of this software and associated documentation files (the "Software"), to deal
    5. // in the Software without restriction, including without limitation the rights
    6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    7. // copies of the Software, and to permit persons to whom the Software is
    8. // furnished to do so, subject to the following conditions:
    9. // The above copyright notice and this permission notice shall be included in all
    10. // copies or substantial portions of the Software.
    11. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    12. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    13. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    14. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    15. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    16. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    17. // SOFTWARE.
    18. #ifndef MAINWINDOW_H
    19. #define MAINWINDOW_H
    20. #include <QMainWindow>
    21. #include <QPaintEvent>
    22. #include <QMouseEvent>
    23. class QSystemTrayIcon;
    24. class QMenu;
    25. QT_BEGIN_NAMESPACE
    26. namespace Ui { class MainWindow; }
    27. QT_END_NAMESPACE
    28. class MainWindow : public QMainWindow
    29. {
    30. Q_OBJECT
    31. public:
    32. MainWindow(QWidget *parent = nullptr);
    33. ~MainWindow();
    34. protected:
    35. void paintEvent(QPaintEvent *event);
    36. private:
    37. void InitTrayIcon();
    38. private:
    39. Ui::MainWindow *ui;
    40. QSystemTrayIcon *_trayIcon{nullptr};
    41. QMenu *_trayMenu{nullptr};
    42. //鼠标点击事件
    43. bool pressed_{false};
    44. };
    45. #endif // MAINWINDOW_H

    4. mainwindow.cpp

    1. // MIT License
    2. // Copyright (c) 2022 hooy
    3. // Permission is hereby granted, free of charge, to any person obtaining a copy
    4. // of this software and associated documentation files (the "Software"), to deal
    5. // in the Software without restriction, including without limitation the rights
    6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    7. // copies of the Software, and to permit persons to whom the Software is
    8. // furnished to do so, subject to the following conditions:
    9. // The above copyright notice and this permission notice shall be included in all
    10. // copies or substantial portions of the Software.
    11. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    12. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    13. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    14. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    15. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    16. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    17. // SOFTWARE.
    18. #include "mainwindow.h"
    19. #include "./ui_mainwindow.h"
    20. #include <hookutil.h>
    21. #include <mousehistory.h>
    22. #include <QPainter>
    23. #include <QDebug>
    24. #include <QMenu>
    25. #include <QSystemTrayIcon>
    26. MainWindow::MainWindow(QWidget *parent)
    27. : QMainWindow(parent)
    28. , ui(new Ui::MainWindow)
    29. {
    30. ui->setupUi(this);
    31. InitTrayIcon();
    32. setAttribute(Qt::WA_TransparentForMouseEvents, true);
    33. setFixedSize(QSize(50,50));
    34. setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint | Qt::SplashScreen);
    35. setAttribute(Qt::WA_TranslucentBackground, true);
    36. connect(MouseHistory::instance(), &MouseHistory::getPos, [=](long x, long y){
    37. move(x-25, y-25);
    38. });
    39. connect(MouseHistory::instance(), &MouseHistory::getPress, [=](int key){
    40. if(key == 1)
    41. pressed_ = true;
    42. else{
    43. pressed_ = false;
    44. }
    45. update();
    46. });
    47. startMouseHook();
    48. }
    49. void MainWindow::InitTrayIcon()
    50. {
    51. _trayIcon = new QSystemTrayIcon(this);
    52. _trayIcon->setIcon(QIcon(":/res/cat.ico"));
    53. _trayIcon->setToolTip("Shallow");
    54. _trayMenu = new QMenu(this);
    55. // _trayMenu->addAction(tr("显示Shallow窗口"),this,[=](){
    56. // this->show();
    57. // this->activateWindow();
    58. // });
    59. _trayMenu->addAction(tr("退出Shallow"),this,[=](){
    60. qApp->quit();
    61. });
    62. _trayIcon->setContextMenu(_trayMenu);
    63. _trayIcon->show();
    64. }
    65. void MainWindow::paintEvent(QPaintEvent *event)
    66. {
    67. QPainter painter(this);
    68. if(pressed_){
    69. painter.setBrush(QColor(255,200,200,100));
    70. }
    71. else{
    72. painter.setBrush(QColor(125,125,125,100));
    73. }
    74. painter.setPen(Qt::NoPen);
    75. painter.setRenderHint(QPainter::Antialiasing, true);
    76. painter.save();
    77. painter.drawEllipse(QPoint(this->rect().left()+25, this->rect().top()+25), 25, 25);
    78. }
    79. MainWindow::~MainWindow()
    80. {
    81. delete ui;
    82. stopMouseHook();
    83. }

    5. mousehistory.h

    1. // MIT License
    2. // Copyright (c) 2022 hooy
    3. // Permission is hereby granted, free of charge, to any person obtaining a copy
    4. // of this software and associated documentation files (the "Software"), to deal
    5. // in the Software without restriction, including without limitation the rights
    6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    7. // copies of the Software, and to permit persons to whom the Software is
    8. // furnished to do so, subject to the following conditions:
    9. // The above copyright notice and this permission notice shall be included in all
    10. // copies or substantial portions of the Software.
    11. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    12. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    13. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    14. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    15. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    16. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    17. // SOFTWARE.
    18. #ifndef MOUSEHISTORY_H
    19. #define MOUSEHISTORY_H
    20. #include <QObject>
    21. class MouseHistory : public QObject
    22. {
    23. Q_OBJECT
    24. public:
    25. MouseHistory(QObject* parent = nullptr):QObject(parent){}
    26. virtual ~MouseHistory(){}
    27. static MouseHistory *&instance()
    28. {
    29. static MouseHistory *s = nullptr;
    30. if (s == nullptr)
    31. {
    32. s = new MouseHistory();
    33. }
    34. return s;
    35. }
    36. public:
    37. void setPosValue(long x, long y)
    38. {
    39. emit getPos(x,y);
    40. }
    41. void setPressEvent(int key){
    42. emit getPress(key);
    43. }
    44. signals:
    45. void getPos(long, long);
    46. void getPress(int);
    47. };
    48. #endif // MOUSEHISTORY_H

    6. main.cpp

    1. // MIT License
    2. // Copyright (c) 2022 hooy
    3. // Permission is hereby granted, free of charge, to any person obtaining a copy
    4. // of this software and associated documentation files (the "Software"), to deal
    5. // in the Software without restriction, including without limitation the rights
    6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    7. // copies of the Software, and to permit persons to whom the Software is
    8. // furnished to do so, subject to the following conditions:
    9. // The above copyright notice and this permission notice shall be included in all
    10. // copies or substantial portions of the Software.
    11. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    12. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    13. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    14. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    15. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    16. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    17. // SOFTWARE.
    18. #include "mainwindow.h"
    19. #include <QApplication>
    20. #pragma comment(linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" )
    21. int main(int argc, char *argv[])
    22. {
    23. QApplication app(argc, argv);
    24. app.setWindowIcon(QIcon(":/res/cat.ico"));
    25. MainWindow w;
    26. w.show();
    27. return app.exec();
    28. }

    具体代码地址在github

  • 相关阅读:
    零基础 Chrome 扩展开发指南
    Linux入门教程:P11->文件查找类
    微信小程序:(更新)云开发微群人脉
    springBoot整合讯飞星火认知大模型
    【C++】设计模式之——建造者
    MIPI CSI-2笔记(15) -- 数据格式(简介、通用8-bit长包数据类型)
    泛微项目二次开发
    实战案例(MDL语句)
    Mybatis简介
    LabVIEW应用开发——控件的使用(二)
  • 原文地址:https://blog.csdn.net/CHNIM/article/details/126950228