I have found a LOT of info on searching the screen for a pixel color, but none of it even remotely definitive (as far as i can tell, because i'm rather noobish)

I understand programming on a fundemental level, but I'm new to C++

I'm hoping to figure out how to search a pixel range (ie a box with corners of x=500 y=500 through x=550 y=550)

I understand how to handle the variables and the looping but I'm having a hard time figuring out the windows handler/syntax/use of this bit of code (which keep popping up but im too noobish to understand how to use)

copied from here (http://www.daniweb.com/software-development/cpp/threads/226108/help-codescanning-screen-for-pixel-color)

    /* Untested code */
    COLORREF tofind = RGB(175, 163, 134);
    COLORREF col;
    for (int y = 0; y < 801; ++y) {
    for (int x = 0; x < 1280; ++x) {
    col = GetPixel( hdc, x, y );
    if ( col == tofind ) {
    cout << "Pixel found at: \t" << "X: "<< x << "\t\t" << "Y: " << y << "\n";

I understand (i THINK) that the CORREF is defining a type of variable? so "tofind" is one variable defined, and "col" is another...

the for loops... got that

i don't know how to use the col=GetPixel(hdc,x,y)... not sure what that's doing so i don't know how to use it

i think it's mostly that i keep seeing the hdc (variable?) in various forms and i'm not sure what to do with it

so i think i understand the flow here but I simply can't figure out how to make "col=GetPixel(hdc,x,y)" do anything

is it looking at the active window? or is it looking at the entire screen? what is hdc?

also if anyone has a good link to a primer for c++ that would be awesome too... specifically stuff like... what to do to use basic programming stuff to actually yield results in a windows environment

my end goal is to program a video game... long ways off... my current goal is to right a program to interact with point and click type video games

Recommended Answers

All 6 Replies

^^^ THANK YOU!!!! i really don't like having stuff handed to me... i really want to learn all this stuff myself so i can actually adapt things to do what i need them to... that was a very good start

#include <iostream>
#include <windows.h>
#include <string>
using namespace std;

int main()
    SetCursorPos (100,100); // Move mouse to 100,100
    Sleep (500);
    mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0); // Left click
    Sleep (500);
    COLORREF find = RGB(0, 0, 0);
    COLORREF color;
    HDC hdc;
    for (int y = 800; y < 820; ++y) {
    for (int x = 600; x < 620; ++x) {
    color = GetPixel( hdc, x, y );
    if ( col == find ) {
    SetCursorPos (1000,1000); // Move mouse to 100,100
    Sleep (500);
    MessageBox (NULL, color, "Color Found", MB_OK);
    MessageBox (NULL, find, "Color to Find", MB_OK);


so this is what i've been playing with

it just clicks over to make an active window (because i don't know how to code that yet)

then if it finds black... it gives a high beep... and when it finishes it gives a low beep

everything SHOULD work swimmingly... but for some reason it never finds black or white... or anything... no matter how much black there is...

also i can't figure out how to diagnose this because when i try to compile with the message boxes to provide some output, it gives me errors about the color and find variables as below...

main.cpp|29|error: initializing argument 2 of 'int MessageBoxA(HWND__*, const CHAR*, const CHAR*, UINT)'|

how would i convert the COLORREF into something that i can see in a msg box?

(i am running this as a GUI)

everything SHOULD work swimmingly

Unfortunately, your code needs a lot of work to morph it into a Win32 app capable of using COLORREF and GetPixel. For starters, you have to create a Window or at least use the Desktop for your app. You also need to create a message pump to receive and process messages from the OS. Yep, that's right, receive messages from the OS. You're accustomed to sending messages (commands) to the OS such as commands to open a file etc. but a Win32 app receives and processes the OS messages. You'll have to create a HDC, (Handle Device Context) for your target window so that you can call GetPixel etc. on that target window. And on and on and on.....

I would suggest you follow the tutorials from the very beginning in order to get a good working knowledge of Win32 programming.

I know that you don't want anything handed to you but I can post an old Petzold example that can give you pointers to fix your code.

Member Avatar

A bit off topic but I was doing this the other day and I found getPixel too slow:


has a noticeable increase in speed. My end goal was to write an undetectable chess auto player and blitz my way to the top using a bit of human play and then switching to a weak engine for the very end seconds thereby winning on time.

ohh holy hell... lol
see you in a month...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.