<1>[QTCN]颜色拾取器

简介: 桌面置顶获取全局鼠标坐标和颜色(Web值、RGB)新增: 按下鼠标右键停止/开启

参考

<1>[QTCN]颜色拾取器

功能

  • 桌面置顶
  • 获取全局鼠标坐标和颜色(Web值、RGB)
  • 新增: 按下鼠标右键停止/开启

实现

06GetColorValueTools.pro

LIBS += -luser32 -lAdvapi32

main.cpp

#include "getcolorvaluetools.h"
#pragma execution_character_set("utf-8")
#include <QApplication>
#include <QTextCodec>
#include <QDesktopWidget>
#include <QStyleFactory>

#include <QDebug>
#include <QApplication>
#include <windows.h>
#include <QFont>
bool isMouseButtonPress=false;
HHOOK g_hook;
// 使用 WinAPI 来捕捉全局鼠标事件中的鼠标右键按下事件
LRESULT CALLBACK MouseHookProc(int nCode, WPARAM wParam, LPARAM lParam)
{
   
    if (nCode >= 0 && wParam == WM_RBUTTONDOWN)  // 捕捉鼠标右键按下事件
    {
   
        isMouseButtonPress=!isMouseButtonPress;
        qDebug() << "Right mouse button pressed!按下鼠标右键!";
    }

    return CallNextHookEx(g_hook, nCode, wParam, lParam);
}


int main(int argc, char *argv[])
{
   
    QApplication a(argc, argv);

    /* SetWindowsHookEx 函数来安装一个低级鼠标钩子 (WH_MOUSE_LL),并将一个回调函数 MouseHookProc 绑定到此钩子上。
      在回调函数中,我们检查钩子的消息码是否为鼠标右键按下事件 (WM_RBUTTONDOWN),如果是,则输出相应的消息 */
    g_hook = SetWindowsHookEx(WH_MOUSE_LL, MouseHookProc, NULL, 0);

    // 设置全局字体大小
    QFont font;
    font.setPointSize(15);  // 设置字体大小
    qApp->setFont(font);

    GetColorValueTools w;
    w.show();

    return a.exec();
}

getcolorvaluetools.h


#ifndef GETCOLORVALUETOOLS_H
#define GETCOLORVALUETOOLS_H
#pragma execution_character_set("utf-8")
#include <QWidget>
#include <QDesktopWidget>
#include <QApplication>
#include <QLabel>
#include <QLineEdit>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QTimer>
#include <QIcon>
#include <QMouseEvent>
#include <QDebug>
#include <QScreen>

class GetColorValueTools : public QWidget

{
   
    Q_OBJECT

public:
    GetColorValueTools(QWidget *parent = nullptr);
    ~GetColorValueTools();
    bool eventFilter(QObject *watched,QEvent *ev) override;
    void  initUI();
    QLabel *labWeb;
    QLabel *labRgb;
    QLabel *labXY;
    QLabel *labTip;
    QLabel *labColor;

    QLineEdit *txtWeb;
    QLineEdit *txtRgb;
    QLineEdit *txtXY;
public slots:
    void ShowColorValue();
};

#endif // GETCOLORVALUETOOLS_H

getcolorvaluetools.cpp


#include "getcolorvaluetools.h"

extern bool isMouseButtonPress;
GetColorValueTools::GetColorValueTools(QWidget *parent)
    : QWidget(parent)
{
   

    initUI();

    // 定时器更新
    QTimer *updateTimer = new QTimer(this);
    updateTimer->callOnTimeout(this,&GetColorValueTools::ShowColorValue);
    updateTimer->start(100);
}

GetColorValueTools::~GetColorValueTools()
{
   
}

bool GetColorValueTools::eventFilter(QObject *watched, QEvent *ev)
{
   
    QMouseEvent *mouseEv=static_cast<QMouseEvent*>(ev);
    if(ev->type()==QEvent::MouseButtonPress&& mouseEv->buttons()&Qt::MouseButton::RightButton)    // 事件为 鼠标按下
    {
   
        qDebug()<<"右键按下"<<isMouseButtonPress;
    }
    return QObject::eventFilter(watched, ev);
}

void GetColorValueTools::initUI()
{
   
    //窗体居中显示
    QDesktopWidget *desktop=QApplication::desktop();
    int width=desktop->width();
    int height=desktop->height();
    //设置窗口只有最小化和关闭按钮 +窗口将始终显示在其他窗口之上
    setWindowFlags(Qt::WindowMinimizeButtonHint|Qt::WindowCloseButtonHint|Qt::WindowStaysOnTopHint);
    this->move((width-this->width())/2,(height-this->height())/2);
    this->setFixedSize(this->width(),this->height());

    setWindowIcon(QIcon(":/1.ico"));
    setWindowTitle(tr("颜色拾取器"));

    labWeb=new QLabel("Web值:",this);
    labRgb=new QLabel("RGB值:",this);
    labXY=new QLabel("XY值:",this);
    labTip=new QLabel("按下鼠标右键停止/开启",this);
    labColor=new QLabel(this);

    txtWeb=new QLineEdit(this);
    txtRgb=new QLineEdit(this);
    txtXY=new QLineEdit(this);

    // 设置自动调整大小的属性
    labWeb->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Preferred);
    labRgb->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Preferred);
    labXY->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Preferred);
    labColor->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Preferred);
    txtWeb->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
    txtRgb->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
    txtXY->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
    labTip->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);

    QHBoxLayout *hLayoutColor=new QHBoxLayout();
    hLayoutColor->addWidget(labColor);
    QHBoxLayout *hLayoutWeb=new QHBoxLayout();
    hLayoutWeb->addWidget(labWeb,1);
    hLayoutWeb->addWidget(txtWeb,2);
    QHBoxLayout *hLayoutRgb=new QHBoxLayout();
    hLayoutRgb->addWidget(labRgb,1);
    hLayoutRgb->addWidget(txtRgb,2);
    QHBoxLayout *hLayoutXY=new QHBoxLayout();
    hLayoutXY->addWidget(labXY,1);
    hLayoutXY->addWidget(txtXY,2);

    QVBoxLayout *vLayoutRight=new QVBoxLayout();
    vLayoutRight->addLayout(hLayoutWeb);
    vLayoutRight->addLayout(hLayoutRgb);
    vLayoutRight->addLayout(hLayoutXY);
    vLayoutRight->addWidget(labTip);

    QHBoxLayout *hLayout=new QHBoxLayout();
    hLayout->addLayout(hLayoutColor);
    hLayout->addLayout(vLayoutRight);

    hLayout->setStretchFactor(hLayoutColor,1);
    hLayout->setStretchFactor(vLayoutRight,1);

    this->setLayout(hLayout);

    this->installEventFilter(this);// 安装事件过滤器

}

void GetColorValueTools::ShowColorValue()
{
   
    QString text;
    if (isMouseButtonPress)
        text = "按下鼠标右键<span style=\"color:red;\">停止</span>/开启";
    else
        text = "按下鼠标右键停止/<span style=\"color:blue;\">开启</span>";
    labTip->setText(text);

    if(isMouseButtonPress)
        return;
    int x = QCursor::pos().x();
    int y = QCursor::pos().y();

    txtXY->setText(tr("X:%1  Y:%2").arg(x).arg(y));
    QString strDecimalValue,strHex;
    // 根据鼠标位置截取屏幕上的一个2x2像素区域的图像
    QScreen *screen = qApp->primaryScreen();  // 获取主屏幕的QScreen对象
    QPixmap pixmap = screen->grabWindow(qApp->desktop()->winId(), x, y, 2, 2);
    if (!pixmap.isNull())
    {
   
        QImage image = pixmap.toImage();
        if (!image.isNull())
        {
   
            if (image.valid(0, 0))  //验证图像中的像素 (0, 0) 是否有效,确保要获取(0, 0)的颜色值存在于图像中
            {
   
                QColor color = image.pixel(0, 0);//获取该图像中 (0,0) 像素的颜色
                int red=color.red();
                int green=color.green();
                int blue=color.blue();
                // 结果转换为一个两位的十六进制字符串,并且如果不足两位,则使用 '0' 来进行填充
                QString strRed=QString("%1").arg(red&0xFF,2,16,QLatin1Char('0'));
                QString strGreen=QString("%1").arg(green&0xFF,2,16,QLatin1Char('0'));
                QString strBlue=QString("%1").arg(blue&0xFF,2,16,QLatin1Char('0'));

                strDecimalValue = tr("%1, %2, %3").arg(red).arg(green).arg(blue);
                // toUpper()所有字符转换为大写形式
                strHex=tr("#%1%2%3").arg(strRed.toUpper()).arg(strGreen.toUpper()).arg(strBlue.toUpper());
            }
        }
    }

    QString str=tr("background-color: rgb(%1);").arg(strDecimalValue);
    labColor->setStyleSheet(str);
    txtRgb->setText(strDecimalValue);
    txtWeb->setText(strHex);
}

效果

image.png
image.png

源码

[Gitee:01GetColorValueTools[QTCN]颜色拾取器]

模糊知识点

  1. QString::arg()
QString QString::arg(int a, int fieldWidth, int base, QChar fillChar)
`a`:要插入的整数值。
`fieldWidth`:字段宽度,用于指定插入的值所占据的最小宽度。如果插入的值的长度小于 fieldWidth,则使用 fillChar 填充剩余的宽度,默认情况下使用空格字符填充。
`base`:进制基数,用于格式化插入的整数值。默认情况下使用十进制。
`fillChar`:填充字符,用于在插入的值的左侧填充空白处。默认情况下使用空格字符。
int red=255;
QString("%1").arg(red&0xFF,2,16,QLatin1Char('0'));
`red & 0xFF`:要插入到占位符中的值,即 red 变量的最低8位。
`2`:指定字段宽度为2,即插入的字符串占据的最小宽度为2。
`16`:指定插入的值以十六进制的形式进行格式化`输出`。
`QLatin1Char('0')`:指定填充字符为 '0',如果插入的字符串宽度小于2,则在左侧用 '0' 填充。
  1. 捕捉windows全局鼠标事件中的鼠标右键按下事件

SetWindowsHookEx函数来安装一个低级鼠标钩子 (WH_MOUSE_LL),并将一个回调函数 MouseHookProc 绑定到此钩子上。
在回调函数中,我们检查钩子的消息码是否为鼠标右键按下事件 (WM_RBUTTONDOWN),如果是,则输出相应的消息

#include <QApplication>
#include <windows.h>
HHOOK g_hook;
// 使用 WinAPI 来捕捉全局鼠标事件中的鼠标右键按下事件
LRESULT CALLBACK MouseHookProc(int nCode, WPARAM wParam, LPARAM lParam)
{
   
    if (nCode >= 0 && wParam == WM_RBUTTONDOWN)  // 捕捉鼠标右键按下事件
    {
   
        qDebug() << "Right mouse button pressed!按下鼠标右键!";
    }

    return CallNextHookEx(g_hook, nCode, wParam, lParam);
}

g_hook = SetWindowsHookEx(WH_MOUSE_LL, MouseHookProc, NULL, 0);
  1. 告诉编译器将源代码文件中的字符解释为 UTF-8 编码,解决中文的最后一位乱码
#pragma execution_character_set("utf-8")
相关文章
|
1天前
|
监控 网络协议 算法
2026年起SSL证书有效期逐年缩短:企业如何避免“续期风暴”!
SSL证书有效期将从2026年起分三阶段逐步缩短至47天,人工续期难以为继。阿里云“WoSign SSL证书+自动化服务”,支持域名监控、自动托管、云产品/服务器一键部署及智能提醒,助力企业高效应对高频续期挑战。
8051 2
|
10天前
|
域名解析 人工智能 Linux
|
20天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
18168 63
|
18天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
4天前
|
人工智能 安全 网络安全
|
2天前
|
人工智能 弹性计算 安全
阿里云 码上2027 AI coding 嘉年华
阿里云“码上2027”AI Coding嘉年华于2026年12月2—3日在上海西岸艺术中心举行,聚焦AI Coding从单点提效迈向企业级研发体系升级。发布Qwen4-max模型、QBot智能协作、AgentCore平台等重磅成果,并推出ECS云服务器特惠(200Mbps流量月付40元起)。
769 0
|
8天前
|
云安全 数据采集 人工智能
|
9天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是 DeepSeek AI 开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、执行命令、处理表格等自动化任务;免费开源,兼容 Win/macOS,需配 API 密钥使用。(239字)
1470 5
|
15天前
|
人工智能 开发工具 开发者
2026最新codex下载安装保姆级使用教程(下载+安装+登录+使用,含所有历史版本)
Codex 是 OpenAI 推出的开源 AI 编程智能体,跑在本地终端和桌面客户端里,能读懂整个代码仓库,一句自然语言就能让它改代码、执行命令、跑测试、修 Bug,支持 Windows、macOS、Linux 三大平台。本文把 Codex下载、安装、登录、汉化、基础使用一次讲全,并整理了国内用户最常踩的坑,跟着做 10 分钟就能跑起来。
2596 8
|
20天前
|
弹性计算 人工智能 API
Codex超详细入门指南(下载·安装·登录·上手,全版本覆盖,2026最新版)
建议收藏本教程,2026 全年各版本更新随时回来查阅。如果你在搭建过程中遇到任何问题,欢迎结合 阿里云工单系统 提交技术支持请求,或在 阿里云开发者社区 搜索 Codex 相关话题交流。 祝你编码愉快,效率翻倍!
4068 22