1. ## GetPixel Function

Hello all, I wrote a function to find a pixel on my bitmap then draw a 10x10 square on the location it finds it.

I first paint the bitmap onto my window, then scan a specific location TLX(top left x) and BRY(bottom right Y) for the collor

dark red (0xCC0000)
(ALSO! how do I incriment a hex value? so I could go from 110000 to FF0000)

if it doesnt find it, it puts a 20x20 square on the screen.

If you see something wrong with my function or something that should be changed plz tell me.

Thankyou!
Code:
```void SearchArea(int TLX,int TLY,int BRX,int BRY)
{
int X;
int Y;
COLORREF clr;
COLORREF clr2;
HDC hdcScrn;
POINT pt;
pt.x = 0;
clr2 = 0xCC0000;
hdcScrn=CreateDC("DISPLAY",0,0,0);

for(Y = 0; Y <= BRY ;Y++)
{
for(X = 0; X<= BRX ; X++)
{
clr=GetPixel(hdcScrn,TLX+X,TLY+Y);
if(clr == clr2)
{
pt.x = TLX+X;
pt.y = TLY+Y;
}
}
if(pt.x != 0)
{
BitBlt(bmoMap,
pt.x,
pt.y,
10,
10,
bmoStar,
0,
0,
SRCPAINT);
}
else
{
BitBlt(bmoMap,
400,
400,
20,
20,
bmoStar,
0,
0,
SRCPAINT);

}
DrawMap();
}```
Edit: The problem is it doesnt find it

2. Run the code in the debugger and find out what values the function is returning.
Are you sure the colour is in 0xRRGGBB format?

> so I could go from 110000 to FF0000)
for ( i = 0x110000 ; i <= 0xFF0000 ; i += 0x10000 )

3. Originally Posted by MSDN_COLORREF
When specifying an explicit RGB color, the COLORREF value has the following hexadecimal form:

0x00bbggrr
The low-order byte contains a value for the relative intensity of red; the second byte contains a value for green; and the third byte contains a value for blue. The high-order byte must be zero. The maximum value for a single byte is 0xFF.

To create a COLORREF color value, use the RGB macro. To extract the individual values for the red, green, and blue components of a color value, use the GetRValue, GetGValue, and GetBValue macros, respectively
Code:
`clr2 = RGB(0xCC, 0x00, 0x00);`

4. With your guys help I have writen what I needed to so far, it sets the mouse to the location to the color, but I just realized something, its off about X,Y the reason is

CreateDC("DISPLAY",0,0,0) which takes in the Display, so whatever is ontop (which will be great for my screenshot function).

BUt I only want to look inside my window im using.

Does anyone know the correct parameters to look inside the form window?

5. Just look inside the rectange returned by GetWindowRect() for your window.

6. GetWindowRect() returns a booL?

All I want to do is use GetPixel regardless of what window is ontop, and only search inside the window its in.

7. The second argument returns a RECT.
Code:
```/* Retrieve the pixels making up a window using GetPixel. */
RECT rc;
GetWindowRect(hwnd, &rc);

for (int x = rc.left;x < rc.right;x++)
{
for (int y = rc.top;y < rc.bottom;y++)
{
clr = GetPixel(hdcScreen, x, y);
...
}
}```
On a side note, GetPixel is not the most efficient method of retrieving a block of pixels.

8. Yeah I tottaly agree.

My problem is the hdcScreen. It seems to target whatever is ontop, I wont it to focus on the programs screen and GetPixel from its own screen regardless what is ontop.

How do I convert the current screens handle (not the one ontop) to an HDC

So GetPixel will only getpixels from its program screen even if there is another program running ontop.

Thnx

9. Use GetDC(), pass the HWND of your dialog as a param.
I would get the required DC outside the function and send the HDC as a param.

Don't forget to ReleaseDC() if you 'get' or DeleteDC() if you create one.

This will only get the client area, not menu ect.

>>Hello all, I wrote a function to find a pixel on my bitmap then draw a 10x10 square on the location it finds it.

Why use the screen DC when you seem to have two globals, bmoMap and bmoStar?

Coords should be pt.x-5, pt.y-5, 10, 10 (but you got that didn't you?)

10. Code:
```void SearchArea(int TLX,int TLY,int BRX,int BRY,HWND hwnd)
{
int X;
int Y;
int i=0;
int G;
int B;
int R = 0;
bool found;
COLORREF clr;
COLORREF Database[1000];

HDC hdcScrn;
POINT pt;
pt.x = 0;
pt.y = 0;

for(B=0;B<=11;B++)
{
for(G=0;G<=25;G++)
{
Database[R] = RGB(103, 101+B, 125+G);
R++;
}
}

hdcScrn=GetDC(hwnd);

found = false;
for(Y = 0; Y <= (BRY-TLY) ;Y++)
{
for(X = 0; X<= (BRX-TLX) ; X++)
{
clr=GetPixel(hdcScrn,TLX+X,TLY+Y);

for(i=0;i<=312;i++)
{
if(clr == Database[i])
{

found = true;
pt.x = TLX+X+8;
pt.y = TLY+Y+30;
}//end IF
}//end IF ColorSearch
if(found)
break;
}//end X
if(found)
break;

}//end Y

if(found)
{

SetCursorPos(pt.x+10,pt.y+30);
}

if(!found)
{
//Do Nothing yet

}
DrawMap();
DeleteDC(hdcScrn);

}```

See, the operations work the mouse placement works, the problem is if another window is ontop it doesnt work.

The Database of colors is the colors its looking for, and whatever it finds first then its set.

So agian, Im not sure how to fix this.

This is a macro program, that takes a screenshot of the top window (works) then in the background blits the image in my prog and searches it for the database of colors and moves the mouse to the location.

The screenshot works, the finding the location works, it just not only searching inside my prog, it searches whatever is ontop.

Is there a way I can search through a bitmap thats not ontop for a specific pixel?

Sorry I havnt been so clear.

11. I'm still not quite sure what you're asking. If you already have a screen shot of the target window, you can search that. Just store it in a memory DC/bitmap and search it. It sounds like you are blitting directly from the screen to your window and not storing the screen shot in a memory bitmap. Is this the case? Maybe you could post the screen shot code.

Or are you asking how to take a screen shot of window that is not on top? Getting a screen shot of a window that is not on top is not simple. Have a look at this article.
Window Contents Capturing using WM_PRINT Message

How would I go about storing the screenshot into memory then searching through the pixels in memory?

Code:
```  HDC hdcDesk;
hdcDesk=GetDC(GetDesktopWindow());
BitBlt(bmoMap, 0, 0, 1024,768,hdcDesk,0,0,SRCCOPY);```
ReleaseDC(HWND_DESKTOP,hdcDesk);

13. Code:
```	// Set up memory dc and bitmap to contain our screen shot (size is cx by cy)...
HDC hdcScreenShot = CreateCompatibleDC(hdcScreen);
HBITMAP hBmpScreenShot = CreateCompatibleBitmap(hdcScreen, cx, cy);
HBITMAP hOldBmpScreenShot = (HBITMAP) SelectObject(hdcScreenShot, hBmpScreenShot);

// Copy screen into our memory bitmap...
BitBlt(hdcScreenShot, 0, 0, cx, cy, hdcScreen, 0, 0, SRCCOPY);

// Use memory bitmap/DC as needed (including GetPixel, bitblt to window, etc)...

// Cleanup (probably in WM_DESTROY)...
SelectObject(hdcScreenShot, hOldBmpScreenShot);
DeleteObject(hBmpScreenShot);
DeleteDC(hdcScreenShot);```
You may want to consider only copying the target window if that is all you need for improved efficiency.

14. Wow man, I really want to thank you for helping me with this, it trully means a lot.

Got it fully working, infact this execellent way of doing it cuts down my application process time by 1000% (really it does)

Thnxs to all who helped