diff --git a/README.md b/README.md index c1c2981..47b87eb 100644 --- a/README.md +++ b/README.md @@ -21,4 +21,5 @@ Qt编写的一些开源的demo,包括串口调试助手、网络调试助手 | 15 | dbpage | 通用数据库翻页查询 | | 16 | pngtool | PNG图片警告去除工具 | | 17 | savelog | 日志重定向输出类 | -| 18 | saveruntime | 运行时间记录类 | \ No newline at end of file +| 18 | saveruntime | 运行时间记录类 | +| 19 | colorwidget | 颜色拾取器 | \ No newline at end of file diff --git a/colorwidget/colorwidget.cpp b/colorwidget/colorwidget.cpp new file mode 100644 index 0000000..192cadd --- /dev/null +++ b/colorwidget/colorwidget.cpp @@ -0,0 +1,172 @@ +#pragma execution_character_set("utf-8") + +#include "colorwidget.h" +#include "qmutex.h" +#include "qgridlayout.h" +#include "qlabel.h" +#include "qlineedit.h" +#include "qapplication.h" +#include "qdesktopwidget.h" +#include "qtimer.h" +#include "qevent.h" +#include "qdebug.h" + +#if (QT_VERSION > QT_VERSION_CHECK(5,0,0)) +#include "qscreen.h" +#endif + +ColorWidget *ColorWidget::instance = 0; +ColorWidget *ColorWidget::Instance() +{ + if (!instance) { + static QMutex mutex; + QMutexLocker locker(&mutex); + if (!instance) { + instance = new ColorWidget; + } + } + + return instance; +} + +ColorWidget::ColorWidget(QWidget *parent) : QWidget(parent) +{ + gridLayout = new QGridLayout(this); + gridLayout->setSpacing(6); + gridLayout->setContentsMargins(11, 11, 11, 11); + + verticalLayout = new QVBoxLayout(); + verticalLayout->setSpacing(0); + + labColor = new QLabel(this); + labColor->setText("+"); + labColor->setStyleSheet("background-color: rgb(255, 107, 107);color: rgb(250, 250, 250);"); + labColor->setAlignment(Qt::AlignCenter); + QFont font; + font.setPixelSize(35); + font.setBold(true); + labColor->setFont(font); + + QSizePolicy sizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding); + sizePolicy.setHorizontalStretch(0); + sizePolicy.setVerticalStretch(0); + sizePolicy.setHeightForWidth(labColor->sizePolicy().hasHeightForWidth()); + labColor->setSizePolicy(sizePolicy); + labColor->setMinimumSize(QSize(80, 70)); + labColor->setMaximumSize(QSize(80, 70)); + labColor->setCursor(QCursor(Qt::CrossCursor)); + labColor->setFrameShape(QFrame::StyledPanel); + labColor->setFrameShadow(QFrame::Sunken); + + verticalLayout->addWidget(labColor); + + label = new QLabel(this); + label->setMinimumSize(QSize(0, 18)); + label->setStyleSheet("background-color: rgb(0, 0, 0);color: rgb(200, 200, 200);"); + label->setAlignment(Qt::AlignCenter); + + verticalLayout->addWidget(label); + gridLayout->addLayout(verticalLayout, 0, 0, 3, 1); + + labWeb = new QLabel(this); + gridLayout->addWidget(labWeb, 0, 1, 1, 1); + + txtWeb = new QLineEdit(this); + gridLayout->addWidget(txtWeb, 0, 2, 1, 1); + + labRgb = new QLabel(this); + gridLayout->addWidget(labRgb, 1, 1, 1, 1); + + txtRgb = new QLineEdit(this); + gridLayout->addWidget(txtRgb, 1, 2, 1, 1); + + labPoint = new QLabel(this); + gridLayout->addWidget(labPoint, 2, 1, 1, 1); + + txtPoint = new QLineEdit(this); + gridLayout->addWidget(txtPoint, 2, 2, 1, 1); + + label->setText("当前颜色显示"); + labWeb->setText("web值:"); + labRgb->setText("rgb值:"); + labPoint->setText("坐标值:"); + + this->setLayout(gridLayout); + this->setWindowTitle("屏幕拾色器"); + this->setFixedSize(270, 108); + + cp = QApplication::clipboard(); + pressed = false; + + timer = new QTimer(this); + timer->setInterval(100); + connect(timer, SIGNAL(timeout()), this, SLOT(showColorValue())); + timer->start(); +} + +ColorWidget::~ColorWidget() +{ +} + +void ColorWidget::mousePressEvent(QMouseEvent *e) +{ + if (labColor->rect().contains(e->pos())) { + pressed = true; + } +} + +void ColorWidget::mouseReleaseEvent(QMouseEvent *) +{ + pressed = false; +} + +void ColorWidget::showColorValue() +{ + if (!pressed) { + return; + } + + int x = QCursor::pos().x(); + int y = QCursor::pos().y(); + + txtPoint->setText(tr("x:%1 y:%2").arg(x).arg(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 & 0xFF, 2, 16, QChar('0')); + QString strBlue = tr("%1").arg(blue & 0xFF, 2, 16, QChar('0')); + + strDecimalValue = tr("%1, %2, %3").arg(red).arg(green).arg(blue); + strHex = tr("#%1%2%3").arg(strRed.toUpper()).arg(strGreen.toUpper()).arg(strBlue.toUpper()); + } + } + } + + if (red > 200 && green > 200 && blue > 200) { + strTextColor = "10, 10, 10"; + } else { + strTextColor = "255, 255, 255"; + } + + QString str = tr("background-color: rgb(%1);color: rgb(%2)").arg(strDecimalValue).arg(strTextColor); + labColor->setStyleSheet(str); + txtRgb->setText(strDecimalValue); + txtWeb->setText(strHex); +} diff --git a/colorwidget/colorwidget.h b/colorwidget/colorwidget.h new file mode 100644 index 0000000..faa2934 --- /dev/null +++ b/colorwidget/colorwidget.h @@ -0,0 +1,55 @@ +#ifndef COLORWIDGET_H +#define COLORWIDGET_H + +#include + +class QGridLayout; +class QVBoxLayout; +class QLabel; +class QLineEdit; + +#ifdef quc +#if (QT_VERSION < QT_VERSION_CHECK(5,7,0)) +#include +#else +#include +#endif + +class QDESIGNER_WIDGET_EXPORT ColorWidget : public QWidget +#else +class ColorWidget : public QWidget +#endif + +{ + Q_OBJECT +public: + static ColorWidget *Instance(); + explicit ColorWidget(QWidget *parent = 0); + ~ColorWidget(); + +protected: + void mousePressEvent(QMouseEvent *); + void mouseReleaseEvent(QMouseEvent *); + +private: + static ColorWidget *instance; + QClipboard *cp; + bool pressed; + QTimer *timer; + + QGridLayout *gridLayout; + QVBoxLayout *verticalLayout; + QLabel *labColor; + QLabel *label; + QLabel *labWeb; + QLineEdit *txtWeb; + QLabel *labRgb; + QLineEdit *txtRgb; + QLabel *labPoint; + QLineEdit *txtPoint; + +private Q_SLOTS: + void showColorValue(); +}; + +#endif // COLORWIDGET_H diff --git a/colorwidget/colorwidget.pro b/colorwidget/colorwidget.pro new file mode 100644 index 0000000..da7c56b --- /dev/null +++ b/colorwidget/colorwidget.pro @@ -0,0 +1,18 @@ +#------------------------------------------------- +# +# Project created by QtCreator 2017-01-05T22:11:54 +# +#------------------------------------------------- + +QT += core gui + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +TARGET = colorwidget +TEMPLATE = app +DESTDIR = $$PWD/../bin +CONFIG += warn_off + +SOURCES += main.cpp +SOURCES += colorwidget.cpp +HEADERS += colorwidget.h diff --git a/colorwidget/main.cpp b/colorwidget/main.cpp new file mode 100644 index 0000000..025223f --- /dev/null +++ b/colorwidget/main.cpp @@ -0,0 +1,30 @@ +#pragma execution_character_set("utf-8") + +#include "colorwidget.h" +#include +#include +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + a.setFont(QFont("Microsoft Yahei", 9)); + +#if (QT_VERSION <= QT_VERSION_CHECK(5,0,0)) +#if _MSC_VER + QTextCodec *codec = QTextCodec::codecForName("gbk"); +#else + QTextCodec *codec = QTextCodec::codecForName("utf-8"); +#endif + QTextCodec::setCodecForLocale(codec); + QTextCodec::setCodecForCStrings(codec); + QTextCodec::setCodecForTr(codec); +#else + QTextCodec *codec = QTextCodec::codecForName("utf-8"); + QTextCodec::setCodecForLocale(codec); +#endif + + ColorWidget::Instance()->show(); + + return a.exec(); +}