Hi all!
I created example to get pixel color using WINAPI. When I get full screen, it's work fine
My source
Qt Code:
  1. #ifndef GETCOLOR_H
  2. #define GETCOLOR_H
  3.  
  4. #include <QObject>
  5. #include <QDebug>
  6. #include <windows.h>
  7. #include <QPoint>
  8. #include <QString>
  9.  
  10. class getColor : public QObject
  11. {
  12. Q_OBJECT
  13. public:
  14. explicit getColor(QObject *parent = 0);
  15. Q_INVOKABLE void getMyPixel(int _x, int _y);
  16.  
  17. signals:
  18. void colorPixel(QString _color);
  19.  
  20. public slots:
  21. void posApplication(QPoint _pos);
  22. void posXApplication(int _posX);
  23. void posYApplication(int _posY);
  24. private:
  25. int posX;
  26. int posY;
  27. QString colorR;
  28. QString colorG;
  29. QString colorB;
  30. QString colorsend;
  31.  
  32.  
  33. };
  34.  
  35. #endif // GETCOLOR_H
To copy to clipboard, switch view to plain text mode 
Qt Code:
  1. #include "getcolor.h"
  2.  
  3. getColor::getColor(QObject *parent) :
  4. QObject(parent)
  5. {
  6. posX = posY = 0;
  7.  
  8. }
  9. void getColor::getMyPixel(int _x, int _y)
  10. {
  11. // HDC dc = GetDC(NULL);
  12. HDC dc = GetWindowDC(NULL);
  13. COLORREF color = GetPixel(dc, _x + posX, _y + posY);
  14.  
  15. int _red = GetRValue(color);
  16. int _green = GetGValue(color);
  17. int _blue = GetBValue(color);
  18. // qDebug()<< "red"<< _red << "green"<<_green << "blue" << _blue;
  19. ReleaseDC(NULL, dc);
  20. colorR = QString::number( _red, 16 );
  21. if(colorR.length() == 1) {
  22. colorR.insert(0,"0");
  23. }
  24. colorG = QString::number( _green, 16 );
  25. if(colorG.length() == 1) {
  26. colorG.insert(0,"0");
  27. }
  28. colorB = QString::number( _blue, 16 );
  29. if(colorB.length() == 1) {
  30. colorB.insert(0,"0");
  31. }
  32. colorsend = "#";
  33. colorsend.append(colorR).append(colorG).append(colorB);
  34. emit colorPixel(colorsend);
  35. }
  36.  
  37. void getColor::posApplication(QPoint _pos)
  38. {
  39. posX = _pos.x();
  40. posY = _pos.y();
  41. }
  42.  
  43. void getColor::posXApplication(int _posX)
  44. {
  45. posX = _posX;
  46. }
  47.  
  48. void getColor::posYApplication(int _posY)
  49. {
  50. posY = _posY;
  51. }
To copy to clipboard, switch view to plain text mode 
Qt Code:
  1. #include <QApplication>
  2. #include "qtquick2applicationviewer.h"
  3. #include <QQmlApplicationEngine>
  4. #include <QQmlApplicationEngine>
  5. #include <QQmlEngine>
  6. #include <QString>
  7. #include <QQmlContext> // for setContextProperty
  8. #include "getcolor.h"
  9. #include "qwindow.h"
  10.  
  11. int main(int argc, char *argv[])
  12. {
  13. QApplication app(argc, argv);
  14. getColor m_getColor;
  15. QWindow m_window;
  16.  
  17. QQmlApplicationEngine engine;
  18. QtQuick2ApplicationViewer viewer;
  19. viewer.setSource(QUrl("qrc:///main.qml"));
  20. viewer.rootContext()->setContextProperty("_getColor",&m_getColor);
  21. viewer.activeFocusItem();
  22. viewer.show();
  23.  
  24. m_getColor.posApplication(viewer.position());
  25. QObject::connect(&viewer,SIGNAL(xChanged(int)),&m_getColor,SLOT(posXApplication(int)));
  26. QObject::connect(&viewer,SIGNAL(yChanged(int)),&m_getColor,SLOT(posYApplication(int)));
  27. return app.exec();
  28. }
To copy to clipboard, switch view to plain text mode 
Qt Code:
  1. import QtQuick 2.3
  2. import QtQuick.Controls 1.2
  3.  
  4. Item {
  5. id: mainwindows
  6. visible: true
  7. width: 640
  8. height: 580
  9. property int r : 20
  10. property color colorPK: "white"
  11. Rectangle {
  12. width: 640
  13. height: 480
  14.  
  15. gradient: Gradient {
  16. GradientStop {
  17. position: 0.0
  18. id: id1
  19. SequentialAnimation on color {
  20. id: update1
  21. loops: Animation.Infinite
  22. ColorAnimation { from: "red"; to: "magenta"; duration: 1000 }
  23. ColorAnimation { from: "magenta"; to: "blue"; duration: 1000 }
  24. ColorAnimation { from: "blue"; to: "cyan"; duration: 1000 }
  25. ColorAnimation { from: "cyan"; to: "lime"; duration: 1000 }
  26. ColorAnimation { from: "lime"; to: "yellow"; duration: 1000 }
  27. ColorAnimation { from: "yellow"; to: "red"; duration: 1000 }
  28. }
  29. }
  30. GradientStop {
  31. position: 1
  32. id: id2
  33. SequentialAnimation on color {
  34. id: update2
  35. loops: Animation.Infinite
  36. ColorAnimation { from: "yellow"; to: "red"; duration: 1000 }
  37. ColorAnimation { from: "red"; to: "magenta"; duration: 1000 }
  38. ColorAnimation { from: "magenta"; to: "blue"; duration: 1000 }
  39. ColorAnimation { from: "blue"; to: "cyan"; duration: 1000 }
  40. ColorAnimation { from: "cyan"; to: "lime"; duration: 1000 }
  41. ColorAnimation { from: "lime"; to: "yellow"; duration: 1000 }
  42. }
  43.  
  44. }
  45. }
  46.  
  47. }
  48. Rectangle {
  49. id: pickerCursor
  50. x: r; y: r
  51. width: r*2; height: r*2
  52. radius: r
  53. border.color: "black"; border.width: 2
  54. color: "transparent"
  55. Rectangle {
  56. anchors.fill: parent; anchors.margins: 2;
  57. border.color: "white"; border.width: 1
  58. radius: width/2
  59. color: "transparent"
  60. }
  61. MouseArea{
  62. anchors.fill: parent
  63. id: dragConsolWindowAreaa
  64. drag.target: parent
  65. drag.minimumX: 0
  66. drag.maximumX: mainwindows.width - pickerCursor.width
  67. drag.minimumY: 0
  68. drag.maximumY: mainwindows.height - pickerCursor.height
  69. onPositionChanged: {
  70.  
  71. }
  72. }
  73. }
  74. Rectangle{
  75. id: colorReciver
  76. anchors.bottom: parent.bottom; anchors.bottomMargin: 0
  77. anchors.horizontalCenter: parent.horizontalCenter
  78. width: 100
  79. height: 100
  80. border.color: "black"
  81. color: colorPK
  82. }
  83.  
  84. Connections{
  85. target: _getColor
  86. onColorPixel:{
  87. colorPK = _color
  88. console.log(_color)
  89. }
  90. }
  91.  
  92. Timer{
  93. interval: 30
  94. repeat: true
  95. running: true
  96. onTriggered: {
  97. _getColor.getMyPixel(pickerCursor.x + pickerCursor.width / 2,pickerCursor.y + pickerCursor.height / 2)
  98. }
  99. }
  100. }
To copy to clipboard, switch view to plain text mode 
But when I get on QT-QML app, it's not work
code edit
Qt Code:
  1. #include "getcolor.h"
  2.  
  3. getColor::getColor(QObject *parent) :
  4. QObject(parent)
  5. {
  6. posX = posY = 0;
  7.  
  8. }
  9. void getColor::getMyPixel(int _x, int _y)
  10. {
  11. // HDC dc = GetDC(NULL);
  12. HWND name = FindWindowA(0,"GetpixelColor");
  13. HDC dc = GetWindowDC(name);
  14. COLORREF color = GetPixel(dc, _x, _y);
  15.  
  16. int _red = GetRValue(color);
  17. int _green = GetGValue(color);
  18. int _blue = GetBValue(color);
  19. // qDebug()<< "red"<< _red << "green"<<_green << "blue" << _blue;
  20. ReleaseDC(NULL, dc);
  21. colorR = QString::number( _red, 16 );
  22. if(colorR.length() == 1) {
  23. colorR.insert(0,"0");
  24. }
  25. colorG = QString::number( _green, 16 );
  26. if(colorG.length() == 1) {
  27. colorG.insert(0,"0");
  28. }
  29. colorB = QString::number( _blue, 16 );
  30. if(colorB.length() == 1) {
  31. colorB.insert(0,"0");
  32. }
  33. colorsend = "#";
  34. colorsend.append(colorR).append(colorG).append(colorB);
  35. emit colorPixel(colorsend);
  36. }
  37.  
  38. void getColor::posApplication(QPoint _pos)
  39. {
  40. posX = _pos.x();
  41. posY = _pos.y();
  42. }
  43.  
  44. void getColor::posXApplication(int _posX)
  45. {
  46. posX = _posX;
  47. }
  48.  
  49. void getColor::posYApplication(int _posY)
  50. {
  51. posY = _posY;
  52. }
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