Hi all!
I created example to get pixel color using WINAPI. When I get full screen, it's work fine
My source
#ifndef GETCOLOR_H
#define GETCOLOR_H
#include <QObject>
#include <QDebug>
#include <windows.h>
#include <QPoint>
#include <QString>
{
Q_OBJECT
public:
explicit getColor
(QObject *parent
= 0);
Q_INVOKABLE void getMyPixel(int _x, int _y);
signals:
public slots:
void posApplication
(QPoint _pos
);
void posXApplication(int _posX);
void posYApplication(int _posY);
private:
int posX;
int posY;
};
#endif // GETCOLOR_H
#ifndef GETCOLOR_H
#define GETCOLOR_H
#include <QObject>
#include <QDebug>
#include <windows.h>
#include <QPoint>
#include <QString>
class getColor : public QObject
{
Q_OBJECT
public:
explicit getColor(QObject *parent = 0);
Q_INVOKABLE void getMyPixel(int _x, int _y);
signals:
void colorPixel(QString _color);
public slots:
void posApplication(QPoint _pos);
void posXApplication(int _posX);
void posYApplication(int _posY);
private:
int posX;
int posY;
QString colorR;
QString colorG;
QString colorB;
QString colorsend;
};
#endif // GETCOLOR_H
To copy to clipboard, switch view to plain text mode
#include "getcolor.h"
getColor
::getColor(QObject *parent
) :{
posX = posY = 0;
}
void getColor::getMyPixel(int _x, int _y)
{
// HDC dc = GetDC(NULL);
HDC dc = GetWindowDC(NULL);
COLORREF color = GetPixel(dc, _x + posX, _y + posY);
int _red = GetRValue(color);
int _green = GetGValue(color);
int _blue = GetBValue(color);
// qDebug()<< "red"<< _red << "green"<<_green << "blue" << _blue;
ReleaseDC(NULL, dc);
colorR
= QString::number( _red,
16 );
if(colorR.length() == 1) {
colorR.insert(0,"0");
}
colorG
= QString::number( _green,
16 );
if(colorG.length() == 1) {
colorG.insert(0,"0");
}
colorB
= QString::number( _blue,
16 );
if(colorB.length() == 1) {
colorB.insert(0,"0");
}
colorsend = "#";
colorsend.append(colorR).append(colorG).append(colorB);
emit colorPixel(colorsend);
}
void getColor
::posApplication(QPoint _pos
) {
posX = _pos.x();
posY = _pos.y();
}
void getColor::posXApplication(int _posX)
{
posX = _posX;
}
void getColor::posYApplication(int _posY)
{
posY = _posY;
}
#include "getcolor.h"
getColor::getColor(QObject *parent) :
QObject(parent)
{
posX = posY = 0;
}
void getColor::getMyPixel(int _x, int _y)
{
// HDC dc = GetDC(NULL);
HDC dc = GetWindowDC(NULL);
COLORREF color = GetPixel(dc, _x + posX, _y + posY);
int _red = GetRValue(color);
int _green = GetGValue(color);
int _blue = GetBValue(color);
// qDebug()<< "red"<< _red << "green"<<_green << "blue" << _blue;
ReleaseDC(NULL, dc);
colorR = QString::number( _red, 16 );
if(colorR.length() == 1) {
colorR.insert(0,"0");
}
colorG = QString::number( _green, 16 );
if(colorG.length() == 1) {
colorG.insert(0,"0");
}
colorB = QString::number( _blue, 16 );
if(colorB.length() == 1) {
colorB.insert(0,"0");
}
colorsend = "#";
colorsend.append(colorR).append(colorG).append(colorB);
emit colorPixel(colorsend);
}
void getColor::posApplication(QPoint _pos)
{
posX = _pos.x();
posY = _pos.y();
}
void getColor::posXApplication(int _posX)
{
posX = _posX;
}
void getColor::posYApplication(int _posY)
{
posY = _posY;
}
To copy to clipboard, switch view to plain text mode
#include <QApplication>
#include "qtquick2applicationviewer.h"
#include <QQmlApplicationEngine>
#include <QQmlApplicationEngine>
#include <QQmlEngine>
#include <QString>
#include <QQmlContext> // for setContextProperty
#include "getcolor.h"
#include "qwindow.h"
int main(int argc, char *argv[])
{
getColor m_getColor;
QWindow m_window;
QQmlApplicationEngine engine;
QtQuick2ApplicationViewer viewer;
viewer.
setSource(QUrl("qrc:///main.qml"));
viewer.rootContext()->setContextProperty("_getColor",&m_getColor);
viewer.activeFocusItem();
viewer.show();
m_getColor.posApplication(viewer.position());
QObject::connect(&viewer,
SIGNAL(xChanged
(int)),
&m_getColor,
SLOT(posXApplication
(int)));
QObject::connect(&viewer,
SIGNAL(yChanged
(int)),
&m_getColor,
SLOT(posYApplication
(int)));
return app.exec();
}
#include <QApplication>
#include "qtquick2applicationviewer.h"
#include <QQmlApplicationEngine>
#include <QQmlApplicationEngine>
#include <QQmlEngine>
#include <QString>
#include <QQmlContext> // for setContextProperty
#include "getcolor.h"
#include "qwindow.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
getColor m_getColor;
QWindow m_window;
QQmlApplicationEngine engine;
QtQuick2ApplicationViewer viewer;
viewer.setSource(QUrl("qrc:///main.qml"));
viewer.rootContext()->setContextProperty("_getColor",&m_getColor);
viewer.activeFocusItem();
viewer.show();
m_getColor.posApplication(viewer.position());
QObject::connect(&viewer,SIGNAL(xChanged(int)),&m_getColor,SLOT(posXApplication(int)));
QObject::connect(&viewer,SIGNAL(yChanged(int)),&m_getColor,SLOT(posYApplication(int)));
return app.exec();
}
To copy to clipboard, switch view to plain text mode
import QtQuick 2.3
import QtQuick.Controls 1.2
Item {
id: mainwindows
visible: true
width: 640
height: 580
property int r : 20
property color colorPK: "white"
Rectangle {
width: 640
height: 480
gradient: Gradient {
GradientStop {
position: 0.0
id: id1
SequentialAnimation on color {
id: update1
loops: Animation.Infinite
ColorAnimation { from: "red"; to: "magenta"; duration: 1000 }
ColorAnimation { from: "magenta"; to: "blue"; duration: 1000 }
ColorAnimation { from: "blue"; to: "cyan"; duration: 1000 }
ColorAnimation { from: "cyan"; to: "lime"; duration: 1000 }
ColorAnimation { from: "lime"; to: "yellow"; duration: 1000 }
ColorAnimation { from: "yellow"; to: "red"; duration: 1000 }
}
}
GradientStop {
position: 1
id: id2
SequentialAnimation on color {
id: update2
loops: Animation.Infinite
ColorAnimation { from: "yellow"; to: "red"; duration: 1000 }
ColorAnimation { from: "red"; to: "magenta"; duration: 1000 }
ColorAnimation { from: "magenta"; to: "blue"; duration: 1000 }
ColorAnimation { from: "blue"; to: "cyan"; duration: 1000 }
ColorAnimation { from: "cyan"; to: "lime"; duration: 1000 }
ColorAnimation { from: "lime"; to: "yellow"; duration: 1000 }
}
}
}
}
Rectangle {
id: pickerCursor
x: r; y: r
width: r*2; height: r*2
radius: r
border.color: "black"; border.width: 2
color: "transparent"
Rectangle {
anchors.fill: parent; anchors.margins: 2;
border.color: "white"; border.width: 1
radius: width/2
color: "transparent"
}
MouseArea{
anchors.fill: parent
id: dragConsolWindowAreaa
drag.target: parent
drag.minimumX: 0
drag.maximumX: mainwindows.width - pickerCursor.width
drag.minimumY: 0
drag.maximumY: mainwindows.height - pickerCursor.height
onPositionChanged: {
}
}
}
Rectangle{
id: colorReciver
anchors.bottom: parent.bottom; anchors.bottomMargin: 0
anchors.horizontalCenter: parent.horizontalCenter
width: 100
height: 100
border.color: "black"
color: colorPK
}
Connections{
target: _getColor
onColorPixel:{
colorPK = _color
console.log(_color)
}
}
Timer{
interval: 30
repeat: true
running: true
onTriggered: {
_getColor.getMyPixel(pickerCursor.x + pickerCursor.width / 2,pickerCursor.y + pickerCursor.height / 2)
}
}
}
import QtQuick 2.3
import QtQuick.Controls 1.2
Item {
id: mainwindows
visible: true
width: 640
height: 580
property int r : 20
property color colorPK: "white"
Rectangle {
width: 640
height: 480
gradient: Gradient {
GradientStop {
position: 0.0
id: id1
SequentialAnimation on color {
id: update1
loops: Animation.Infinite
ColorAnimation { from: "red"; to: "magenta"; duration: 1000 }
ColorAnimation { from: "magenta"; to: "blue"; duration: 1000 }
ColorAnimation { from: "blue"; to: "cyan"; duration: 1000 }
ColorAnimation { from: "cyan"; to: "lime"; duration: 1000 }
ColorAnimation { from: "lime"; to: "yellow"; duration: 1000 }
ColorAnimation { from: "yellow"; to: "red"; duration: 1000 }
}
}
GradientStop {
position: 1
id: id2
SequentialAnimation on color {
id: update2
loops: Animation.Infinite
ColorAnimation { from: "yellow"; to: "red"; duration: 1000 }
ColorAnimation { from: "red"; to: "magenta"; duration: 1000 }
ColorAnimation { from: "magenta"; to: "blue"; duration: 1000 }
ColorAnimation { from: "blue"; to: "cyan"; duration: 1000 }
ColorAnimation { from: "cyan"; to: "lime"; duration: 1000 }
ColorAnimation { from: "lime"; to: "yellow"; duration: 1000 }
}
}
}
}
Rectangle {
id: pickerCursor
x: r; y: r
width: r*2; height: r*2
radius: r
border.color: "black"; border.width: 2
color: "transparent"
Rectangle {
anchors.fill: parent; anchors.margins: 2;
border.color: "white"; border.width: 1
radius: width/2
color: "transparent"
}
MouseArea{
anchors.fill: parent
id: dragConsolWindowAreaa
drag.target: parent
drag.minimumX: 0
drag.maximumX: mainwindows.width - pickerCursor.width
drag.minimumY: 0
drag.maximumY: mainwindows.height - pickerCursor.height
onPositionChanged: {
}
}
}
Rectangle{
id: colorReciver
anchors.bottom: parent.bottom; anchors.bottomMargin: 0
anchors.horizontalCenter: parent.horizontalCenter
width: 100
height: 100
border.color: "black"
color: colorPK
}
Connections{
target: _getColor
onColorPixel:{
colorPK = _color
console.log(_color)
}
}
Timer{
interval: 30
repeat: true
running: true
onTriggered: {
_getColor.getMyPixel(pickerCursor.x + pickerCursor.width / 2,pickerCursor.y + pickerCursor.height / 2)
}
}
}
To copy to clipboard, switch view to plain text mode
But when I get on QT-QML app, it's not work
code edit
#include "getcolor.h"
getColor
::getColor(QObject *parent
) :{
posX = posY = 0;
}
void getColor::getMyPixel(int _x, int _y)
{
// HDC dc = GetDC(NULL);
HWND name = FindWindowA(0,"GetpixelColor");
HDC dc = GetWindowDC(name);
COLORREF color = GetPixel(dc, _x, _y);
int _red = GetRValue(color);
int _green = GetGValue(color);
int _blue = GetBValue(color);
// qDebug()<< "red"<< _red << "green"<<_green << "blue" << _blue;
ReleaseDC(NULL, dc);
colorR
= QString::number( _red,
16 );
if(colorR.length() == 1) {
colorR.insert(0,"0");
}
colorG
= QString::number( _green,
16 );
if(colorG.length() == 1) {
colorG.insert(0,"0");
}
colorB
= QString::number( _blue,
16 );
if(colorB.length() == 1) {
colorB.insert(0,"0");
}
colorsend = "#";
colorsend.append(colorR).append(colorG).append(colorB);
emit colorPixel(colorsend);
}
void getColor
::posApplication(QPoint _pos
) {
posX = _pos.x();
posY = _pos.y();
}
void getColor::posXApplication(int _posX)
{
posX = _posX;
}
void getColor::posYApplication(int _posY)
{
posY = _posY;
}
#include "getcolor.h"
getColor::getColor(QObject *parent) :
QObject(parent)
{
posX = posY = 0;
}
void getColor::getMyPixel(int _x, int _y)
{
// HDC dc = GetDC(NULL);
HWND name = FindWindowA(0,"GetpixelColor");
HDC dc = GetWindowDC(name);
COLORREF color = GetPixel(dc, _x, _y);
int _red = GetRValue(color);
int _green = GetGValue(color);
int _blue = GetBValue(color);
// qDebug()<< "red"<< _red << "green"<<_green << "blue" << _blue;
ReleaseDC(NULL, dc);
colorR = QString::number( _red, 16 );
if(colorR.length() == 1) {
colorR.insert(0,"0");
}
colorG = QString::number( _green, 16 );
if(colorG.length() == 1) {
colorG.insert(0,"0");
}
colorB = QString::number( _blue, 16 );
if(colorB.length() == 1) {
colorB.insert(0,"0");
}
colorsend = "#";
colorsend.append(colorR).append(colorG).append(colorB);
emit colorPixel(colorsend);
}
void getColor::posApplication(QPoint _pos)
{
posX = _pos.x();
posY = _pos.y();
}
void getColor::posXApplication(int _posX)
{
posX = _posX;
}
void getColor::posYApplication(int _posY)
{
posY = _posY;
}
To copy to clipboard, switch view to plain text mode
I try get color in another app, it's work very fine, but only do not work in QT - QML app. I don't know why?
Please help me to solve this problem.
Thank you for reading!
My project!
https://www.dropbox.com/s/554no9q9eg...Color.rar?dl=0
Bookmarks