473,387 Members | 1,876 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 1671

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

Similar topics

2
by: Rene van Hoek | last post by:
Hi, I am using Xalan 1.8.0 and Xerces 2.6.0 in C++. I have an XML document which I first transform into an other XML document using an XSL styelsheet. Then I want to parse with XPathEvaluator...
1
by: Sergio Geralnik | last post by:
I am trying to use this method to get a MethodInfo object but the method always returns a null. See my sample code below: this.button1 = new System.Windows.Forms.Button(); this.button1.Click +=...
0
by: Aslam | last post by:
We have an abstract class FieldBase and FooFieldBase is extended from FieldBase We are checking, if the FooFieldBase is a subclass of FieldBase with the following line of code return...
8
by: Brian | last post by:
This is causing me to not be able to create a relation in my dataset. Here's my code: dc1 = ds.Tables .Columns ; dc2 = ds.Tables .Columns ; dr1 = new System.Data.DataRelation...
5
by: Tim | last post by:
Hello All, I am writing a program that checks for the NumLock status and turns the NumLock on if it is off. I'm not sure what I'm overlooking at this point, but the code will compile and run, but...
1
by: Tim | last post by:
Hello All, I am writing a program that checks for the NumLock status and turns the NumLock on if it is off. I'm not sure what I'm overlooking at this point, but the code will compile and run, but...
10
by: Lyle Fairfield | last post by:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbaac11/html/acfctNZ_HV05186465.asp "If the value of the variant argument is Null, the Nz function returns the number zero or a...
2
by: Joe Rattz | last post by:
I am trying to create an XmlReader using XmlReader.Create, but it always returns {None}. I have tried several examples from the web, and Create always returns "{None}". Here is my code: ...
6
by: Michi Henning | last post by:
I'm running the following code in Safari 2.0.4: document.cookie = 'MyCookie=1'; if(document.cookie == '') alert('document.cookie is empty!'); document.cookie always returns the empty string,...
5
by: yogeshmk | last post by:
I'm writing an application which is required to function with many languages and also compile on Linux & windows. Here's how I find the locale .. # include <stdio.h> # include <locale.h> int...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.