彻底改版2.0
This commit is contained in:
174
widget/colorwidget/colorwidget.cpp
Normal file
174
widget/colorwidget/colorwidget.cpp
Normal file
@@ -0,0 +1,174 @@
|
||||
#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 "qtimer.h"
|
||||
#include "qevent.h"
|
||||
#include "qdebug.h"
|
||||
|
||||
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
|
||||
#include "qscreen.h"
|
||||
#define deskGeometry qApp->primaryScreen()->geometry()
|
||||
#define deskGeometry2 qApp->primaryScreen()->availableGeometry()
|
||||
#else
|
||||
#include "qdesktopwidget.h"
|
||||
#define deskGeometry qApp->desktop()->geometry()
|
||||
#define deskGeometry2 qApp->desktop()->availableGeometry()
|
||||
#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(300, 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));
|
||||
|
||||
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
|
||||
QScreen *screen = qApp->primaryScreen();
|
||||
QPixmap pixmap = screen->grabWindow(0, x, y, 2, 2);
|
||||
#else
|
||||
QPixmap pixmap = QPixmap::grabWindow(qApp->desktop()->winId(), x, y, 2, 2);
|
||||
#endif
|
||||
|
||||
int red, green, blue;
|
||||
QString strDecimalValue, strHex;
|
||||
if (pixmap.isNull()) {
|
||||
return;
|
||||
}
|
||||
|
||||
QImage image = pixmap.toImage();
|
||||
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());
|
||||
}
|
||||
|
||||
//根据背景色自动计算合适的前景色
|
||||
QColor color(red, green, blue);
|
||||
double gray = (0.299 * color.red() + 0.587 * color.green() + 0.114 * color.blue()) / 255;
|
||||
QColor textColor = gray > 0.5 ? Qt::black : Qt::white;
|
||||
|
||||
QString str = tr("background:rgb(%1);color:%2").arg(strDecimalValue).arg(textColor.name());
|
||||
labColor->setStyleSheet(str);
|
||||
txtRgb->setText(strDecimalValue);
|
||||
txtWeb->setText(strHex);
|
||||
}
|
||||
58
widget/colorwidget/colorwidget.h
Normal file
58
widget/colorwidget/colorwidget.h
Normal file
@@ -0,0 +1,58 @@
|
||||
#ifndef COLORWIDGET_H
|
||||
#define COLORWIDGET_H
|
||||
|
||||
/**
|
||||
* 屏幕拾色器 作者:feiyangqingyun(QQ:517216493) 2016-11-11
|
||||
* 1. 鼠标按下实时采集鼠标处的颜色。
|
||||
* 2. 实时显示颜色值。
|
||||
* 3. 支持16进制格式和rgb格式。
|
||||
* 4. 实时显示预览颜色。
|
||||
* 5. 根据背景色自动计算合适的前景色。
|
||||
*/
|
||||
|
||||
#include <QWidget>
|
||||
|
||||
class QGridLayout;
|
||||
class QVBoxLayout;
|
||||
class QLabel;
|
||||
class QLineEdit;
|
||||
|
||||
#ifdef quc
|
||||
class Q_DECL_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
|
||||
17
widget/colorwidget/colorwidget.pro
Normal file
17
widget/colorwidget/colorwidget.pro
Normal file
@@ -0,0 +1,17 @@
|
||||
QT += core gui
|
||||
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
|
||||
greaterThan(QT_MAJOR_VERSION, 5): QT += core5compat
|
||||
|
||||
TARGET = colorwidget
|
||||
TEMPLATE = app
|
||||
DESTDIR = $$PWD/../bin
|
||||
CONFIG += warn_off
|
||||
|
||||
SOURCES += main.cpp
|
||||
SOURCES += frmcolorwidget.cpp
|
||||
SOURCES += colorwidget.cpp
|
||||
|
||||
HEADERS += colorwidget.h
|
||||
HEADERS += frmcolorwidget.h
|
||||
|
||||
FORMS += frmcolorwidget.ui
|
||||
18
widget/colorwidget/frmcolorwidget.cpp
Normal file
18
widget/colorwidget/frmcolorwidget.cpp
Normal file
@@ -0,0 +1,18 @@
|
||||
#include "frmcolorwidget.h"
|
||||
#include "ui_frmcolorwidget.h"
|
||||
#include "colorwidget.h"
|
||||
|
||||
frmColorWidget::frmColorWidget(QWidget *parent) : QWidget(parent), ui(new Ui::frmColorWidget)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
}
|
||||
|
||||
frmColorWidget::~frmColorWidget()
|
||||
{
|
||||
delete ui;
|
||||
}
|
||||
|
||||
void frmColorWidget::on_pushButton_clicked()
|
||||
{
|
||||
ColorWidget::Instance()->show();
|
||||
}
|
||||
25
widget/colorwidget/frmcolorwidget.h
Normal file
25
widget/colorwidget/frmcolorwidget.h
Normal file
@@ -0,0 +1,25 @@
|
||||
#ifndef FRMCOLORWIDGET_H
|
||||
#define FRMCOLORWIDGET_H
|
||||
|
||||
#include <QWidget>
|
||||
|
||||
namespace Ui {
|
||||
class frmColorWidget;
|
||||
}
|
||||
|
||||
class frmColorWidget : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit frmColorWidget(QWidget *parent = 0);
|
||||
~frmColorWidget();
|
||||
|
||||
private slots:
|
||||
void on_pushButton_clicked();
|
||||
|
||||
private:
|
||||
Ui::frmColorWidget *ui;
|
||||
};
|
||||
|
||||
#endif // FRMCOLORWIDGET_H
|
||||
32
widget/colorwidget/frmcolorwidget.ui
Normal file
32
widget/colorwidget/frmcolorwidget.ui
Normal file
@@ -0,0 +1,32 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ui version="4.0">
|
||||
<class>frmColorWidget</class>
|
||||
<widget class="QWidget" name="frmColorWidget">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>800</width>
|
||||
<height>600</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
<string>Form</string>
|
||||
</property>
|
||||
<widget class="QPushButton" name="pushButton">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>10</x>
|
||||
<y>10</y>
|
||||
<width>92</width>
|
||||
<height>28</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>弹出</string>
|
||||
</property>
|
||||
</widget>
|
||||
</widget>
|
||||
<resources/>
|
||||
<connections/>
|
||||
</ui>
|
||||
31
widget/colorwidget/main.cpp
Normal file
31
widget/colorwidget/main.cpp
Normal file
@@ -0,0 +1,31 @@
|
||||
#pragma execution_character_set("utf-8")
|
||||
|
||||
#include "frmcolorwidget.h"
|
||||
#include <QApplication>
|
||||
#include <QTextCodec>
|
||||
|
||||
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
|
||||
|
||||
frmColorWidget w;
|
||||
w.setWindowTitle("屏幕拾色器");
|
||||
w.show();
|
||||
|
||||
return a.exec();
|
||||
}
|
||||
Reference in New Issue
Block a user