Cant find the Pixel color

Good day/Good Evening to everyone here at cplusplus,
It is my first time to ask question about c++,
I'm currently self studying this language for about a month now
And my question is this,

my problem is i cannot find the RGB = 0, 0, 255,
i/ve made a rectangle blue on paint then make it as a background
I've tried different color and it worked,
here is my code



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <windows.h>
#include <cstdlib>
#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
	int bez = 0, bez2 = 3;
	int r, g, b;
	int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmdLine, int nCmdShow);
	

	do
	{

		HDC hdcScreen = GetDC(HWND_DESKTOP);
		COLORREF pixel;
		for (int y = 0; y < 1024; y++) {   //scans Y values 
		for (int x = 0; x < 1200; x++) {   // scans X 
				pixel = GetPixel(hdcScreen, x, y);
				r = GetRValue(pixel);
				g = GetGValue(pixel);
				b = GetBValue(pixel);
				ReleaseDC(0, hdcScreen);

				if (r == 0 && g == 0 && b == 255)      //  
				{
				MessageBox(NULL, "Found it!!", "Hi!", MB_OK);
				}
				else 
				{
				MessageBox(NULL, "Not Found", "Hi!", MB_OK);
				}
			}
		}

	} while (bez != bez2);

	return 0;
}


is there something i missed ?
Thanks in advance !
There are ways to improve your code.
Are you sure that the Blue value is exactly 255 ?
Try this code - maybe set rows and cols to a smaller value to save time
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <windows.h>
#include <cstdlib>
#include <iostream>
#include <cstring>

int main()
{
  COLORREF crSearchColor = RGB(0, 0, 255); // BLUE
  HDC hdcScreen = GetDC(HWND_DESKTOP);
  bool found = false;

  if (hdcScreen == nullptr)
  {
    std::cerr << "Windows error: " << ::GetLastError() << "\n\n";
    return -1;
  }
  // The height of the screen of the primary display monitor, in pixels
  int rows = ::GetSystemMetrics(SM_CYSCREEN); 

  // The width of the screen of the primary display monitor, in pixels
  int cols = ::GetSystemMetrics(SM_CXSCREEN);

  for (int y = 0; y < rows; y++)    //scans Y values 
  {
    for (int x = 0; x < cols; x++)   // scans X 
    {
      /*if (y % 100 == 0 && x % 100 == 0)
        std::cout << "y =" << y << "\tx = " << x << "\n";*/
      if(::GetPixel(hdcScreen, x, y) == crSearchColor)
      {
        MessageBoxA(nullptr, "Found it!!", "Hi!", MB_OK);
        found = true;
      }
    }    
  }
  if(!found)
    ::MessageBoxA(nullptr, "Not found", "Sorry", MB_OK);

  ::ReleaseDC(HWND_DESKTOP, hdcScreen);

return 0;
}


What book or tutorials do you use ?
thank you very much for replying,

yes i use edit color on paint so i could manually input RGB value on it,

its my first time to see the GetSystemMetrics function i

Im using the tutorial here at cplusplus and also watching some free tutorials on udemy and if i get too confused i go to google its my first time here so i appreciate your reply

again thank you so much,

I'm planning to make a pixel search with setcursorpos but I'm not finish with this one so i haven't added the function yet
Tutorials can give you only specific pieces of knowledge. To get the whole picture it's better to use a book. This book is the "Unofficial Windows Bible" and worth getting.

https://www.amazon.com/Programming-Windows%C2%AE-Fifth-Developer-Reference/dp/157231995X/ref=sr_1_2?s=books&ie=UTF8&qid=1505638496&sr=1-2&keywords=petzold
Last edited on
thanks a lot good sir

btw
1
2
 /*if (y % 100 == 0 && x % 100 == 0)
        std::cout << "y =" << y << "\tx = " << x << "\n";*/


what is this for?
Last edited on
It was just for some testing purpose. It took a long time you I wanted to make sure it was doing some work.
is this to show the coordinate? am i right my good sir?

and also sir i've tried to make pixel search for a certain software only like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <windows.h>
#include <cstdlib>
#include <iostream>
#include <tchar.h>

using namespace std;

int main()
{
	int bez = 0, bez2 = 3;
	int r, g, b;
	int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmdLine, int nCmdShow);
	
	do
	{

		HWND hwnd = FindWindow(_T("MSPaintApp"), _T("Untitled - Paint"));
		RECT rc;
		GetClientRect(hwnd, &rc);
		ClientToScreen(hwnd, reinterpret_cast<POINT*>(&rc.left));
		ClientToScreen(hwnd, reinterpret_cast<POINT*>(&rc.right));
		HDC hdc = GetDC(hwnd);
		COLORREF pixel;
			for (int y = 0; y < 600; y++) {   //scans Y values (vertical)
			for (int x = 0; x < 800; x++) {   // scans X (horizantal)
				pixel = GetPixel(hdc, x, y);
				r = GetRValue(pixel);
				g = GetGValue(pixel);
				b = GetBValue(pixel);
				ReleaseDC(0, hdc);

				if (r == 0 && g == 0 && b == 255)      //  
				{
					MessageBox(NULL, "Testing", "Hi!", MB_OK);
					//mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);  // mouse click event
					//Sleep(500);
					bez >= bez2; 
					bez++;
					if (bez == bez2) { break;}

				}
			}
		}

	} while (bez != bez2);

	return 0;
}


when i debug its goes successful but when i try to use this 1 it wont work
I am not sure what you mean. What happens ? What should happen ?
this was the same as my first code but i want it to be specific in a certain program which is the paint
when i debug its goes successful but when i try to use this 1 it wont work

Sorry I don't understand your meaning of debug here.
Anyway the problem with MS Paint is that the actual image is in a different (child)window.

HWND hwnd = FindWindow(_T("MSPaintApp"), _T("Untitled - Paint"));
Will give you only the main window.
Topic archived. No new replies allowed.