The find code works for me (I looked for black, RGB(0, 0, 0), rather than RGB(95, 103, 65).)
(Are you sure you have RGB(95, 103, 65) on your screen? You could screen cap to Paint, or some other bitmap editor, and check the RGB value is what you expect.)
But the coordinates used by the GDI and the Console APIs are not the same: GDI's GetPixel is working with (well) pixels; the Console SetConsoleCursorPosition call uses character rows and columns. So you need to convert the GDI coordinates into the correct row and column for the console.
I've never had the never to do this, nor have I come across a neat way to do it. If you don't actually have to move the console cursor, you could move the mouse instead -- mouse_event with MOUSEEVENTF_MOVE (this might be necessary whether or not you move the console cursor.)
But while I'm here:
#0 mouse_event has been superseded by SendInput. You should use the newer function instead.
#1 I had to tweak the cout line to
cout<<x+","+y;
(C++ does not know how to add integers and strings, or even how to add two C-style strings. The << verions says output x, then output ",", etc.)
#2 goto is
bad -- you should rework your code to use for-loops. For information about them, see:
Control Structures -- The for loop
http://www.cplusplus.com/doc/tutorial/control/#for
You will end up with less code if you do this.
#3 The coords of the screen run from 0 to N-1, where N is the width or height. So for a 1280 x 1024 mode, the pixels run from (0, 0) to (1279, 1023)
#4 This
1 2 3 4 5 6 7
|
if( r == 95 ){
if(g == 103){
if(b == 65){
// do stuff
}
}
}
|
can be better written as
1 2 3
|
if( r == 95 && g == 103 && b == 65 ){
// do stuff
}
|
or even (if you like to make the grouping more obvious)
1 2 3
|
if( (r == 95) && (g == 103) && (b == 65) ){
// do stuff
}
|
(C++ uses short circuit evaluation, from left to right, so it will not bother to evaluate g == 103 or b == 65 if r is not 95.)
But in this case, rather than breaking up the value returned by GetPixel and testing it color by color, you could create an RGB value to compare with the value returned.
1 2 3 4 5 6 7 8 9 10 11
|
// COLORREF is a typedef of DWORD provided for this kind of use
COLORREF colorWanted = RGB(95, 103, 65);
// etc
COLORREF color = GetPixel(hdc, x, y);
if(color == colorWanted)
{
// etc
|
Andy
PS As it looks like you're new here...
Please use code tags --> the
<>
button below the new post box or to the right of the post edit box. As well as looking nice, it provides line numbers which make it easier to refer to posted code.
1 2 3 4 5 6 7 8
|
#include <iostream>
using namespace std;
int main() {
cout << "Hello nicely formatted world!\n";
return 0;
}
|
And a couple of articles:
How to use code tags
http://www.cplusplus.com/articles/jEywvCM9/
How to use tags
http://www.cplusplus.com/articles/z13hAqkS/
(for code and other types of tag)
You can even go back and add tags to your earlier posts...