QT 屏幕拾色器 - 获取 屏幕某点的颜色值

原理:

通过 qApp 获取桌面 句柄

使用 QPixmap::grabWindow 函数,获取鼠标点选位置的图片

分析图片的像素,最终获取 颜色值

 

int x = QCursor::pos().x();
int y = QCursor::pos().y();

QString strDecimalValue, strHex, strTextColor;
int red, green, blue;

#if (QT_VERSION <= QT_VERSION_CHECK(5,0,0))

    QPixmap pixmap = QPixmap::grabWindow(QApplication::desktop()->winId(), x, y, 2, 2);

#else

    QScreen *screen = QApplication::primaryScreen();
    QPixmap pixmap = screen->grabWindow(QApplication::desktop()->winId(), x, y, 2, 2);

endif

    if (!pixmap.isNull()) {
        QImage image = pixmap.toImage();

        if (!image.isNull()) {
            if (image.valid(0, 0)) {
                QColor color = image.pixel(0, 0);
                red = color.red();
                green = color.green();
                blue = color.blue();

                QString strRed = tr("%1").arg(red & 0xFF, 2, 16, QChar('0'));
                QString strGreen = tr("%1").arg(green &
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值