468,145 Members | 1,367 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,145 developers. It's quick & easy.

GetPixel() always returns 4294967

Hi! I'm trying to make a program that detects a pixel color at the mouse position but it always returns 4294967. I've tried making clipping regions and bitmaps but I don't know if I'm doing it properly or I've somehow offended GetPixel() and now it is doing this to spite me. Just WHAT IS WRONG WITH MY CODE? Sorry and thank you for any help!
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <Windows.h> // Sleep, GetAsyncKeyState, etc.
  3. #include <Windef.h>
  4. //#include <opencv2\opencv.hpp>
  5.  
  6. using namespace std;
  7.  
  8. int main() {
  9.     while (true) {
  10.  
  11.         LPCWSTR window_title = L"World of Warcraft";
  12.         HWND hWND = FindWindow(NULL, window_title);
  13.  
  14.  
  15.         RECT rWindow;
  16.         RECT rClient;
  17.  
  18.         HRGN hRgnWindow;
  19.         HRGN hRgnClient;
  20.         HRGN hNCRgn;
  21.  
  22.         //C: Get the window and client rectangles for the window.
  23.         GetWindowRect(hWND, &rWindow);
  24.         GetClientRect(hWND, &rClient);
  25.  
  26.         //C: Translate the Client rectangle into screen coordinates.
  27.         POINT p = { 0,0 };
  28.         MapWindowPoints(hWND, NULL, &p, 1);
  29.         OffsetRect(&rClient, p.x, p.y);
  30.  
  31.         //C: Create regions from these two rectangles.
  32.         hRgnWindow = ::CreateRectRgnIndirect(&rWindow);
  33.         hRgnClient = ::CreateRectRgnIndirect(&rClient);
  34.         hNCRgn = ::CreateRectRgn(0, 0, 0, 0);
  35.  
  36.         //C: Subtract the client region from the window region.
  37.         CombineRgn(hNCRgn, hRgnWindow, hRgnClient, RGN_DIFF);
  38.  
  39.         while (hWND == NULL) {
  40.             hWND = FindWindowA(NULL, "World of Warcraft");
  41.             cout << "start game!" << endl;
  42.             Sleep(1000);
  43.         }
  44.         Sleep(10);
  45.         if (GetAsyncKeyState(VK_SHIFT)) { // MousePosition
  46.             //HDC hDC = GetDC(hWND); 
  47.             POINT p;
  48.             GetCursorPos(&p);
  49.             ScreenToClient(hWND, &p); // actually only needs window title not HDC but will need for color
  50.             //ReleaseDC(hWND, hDC);
  51.             cout << "(" << p.x << ","<< p.y << ")" << endl;
  52.             Sleep(1000);
  53.         }
  54.         Sleep(10);
  55.         if (GetAsyncKeyState(VK_LBUTTON)) { // pixelColor
  56.             HDC hDC, hCDC;
  57.             HBITMAP hbwin;
  58.             HRGN RrGN = CreateRectRgn(30, 5, 1277, 690);
  59.             int height, width;
  60.             hDC = GetDC(hWND);
  61.             hCDC = CreateCompatibleDC(hDC);
  62.             SetStretchBltMode(hCDC, COLORONCOLOR);
  63.  
  64.             RECT winsize;
  65.             GetClientRect(hWND, &winsize);
  66.             SetWindowRgn(hWND, RrGN, TRUE);
  67.             SelectClipRgn(hDC, RrGN);
  68.             GetClipRgn(hDC, RrGN);
  69.  
  70.             POINT p;
  71.             GetCursorPos(&p);
  72.  
  73.             hbwin = CreateCompatibleBitmap(hDC, p.x, p.y);
  74.             SelectObject(hCDC, hbwin);
  75.  
  76.             COLORREF color = GetPixel(hCDC, p.x, p.y);
  77.  
  78.             cout << " | Color: " << color << endl;
  79.                 /*cout<<  //"(" << p.x << "," << p.y << ")" << " R: " << (int)GetRValue(color) 
  80.                 << " | G: " << (int)GetGValue(color) << " | B: "
  81.                 << (int)GetBValue(color) << endl;
  82.             BOOL EndPaint(hWND, CONST PAINTSTRUCT * lp);*/
  83.             Sleep(10);
  84.             cout << " | Color: " << color << endl;
  85.             DeleteObject(hbwin);
  86.             DeleteDC(hCDC);
  87.             ReleaseDC(hWND, hDC);
  88.             DeleteObject(RrGN);
  89.             Sleep(1000);
  90.         }
  91.         /*if (GetAsyncKeyState(VK_CONTROL)) {
  92.             HDC hDC = GetDC(hWND);
  93.             COLORREF color;                        //could have the choice of which zone or 
  94.             COLORREF characteristic_ color = ; //color of fishing bobber? --> How to find in different zones
  95.             Sleep(1000);
  96.         }*/
  97.         if (GetAsyncKeyState(VK_MENU)) { // Exit Game
  98.             return 0;
  99.         }
  100.         DeleteObject(hRgnWindow);
  101.         DeleteObject(hRgnClient);
  102.         DeleteObject(hNCRgn);
  103.     }
  104.     return 0;
  105.  
  106. }
Apr 2 '20 #1
0 1133

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Rene van Hoek | last post: by
1 post views Thread by Sergio Geralnik | last post: by
reply views Thread by Aslam | last post: by
5 posts views Thread by Tim | last post: by
1 post views Thread by Tim | last post: by
2 posts views Thread by Joe Rattz | last post: by
6 posts views Thread by Michi Henning | last post: by
5 posts views Thread by yogeshmk | last post: by
30 posts views Thread by didacticone | last post: by
1 post views Thread by gcdp | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.