| View previous topic :: View next topic |
| Author |
Message |
yoyonerd Grandmaster Cheater
Reputation: 0
Joined: 26 Apr 2008 Posts: 699 Location: -->formerly yoyonerd<--
|
Posted: Fri May 29, 2009 7:38 pm Post subject: [C++]GetPixel issues |
|
|
Well, quite simply I am trying to get the pixel color of wherever my mouse is.
No matter what happens, I always end up with R, G, AND B all being 255.
| Code: |
#include <iostream>
#include <Windows.h>
#include <conio.h>
using namespace std;
int main()
{
POINT pos;
GetCursorPos(&pos);
int x=pos.x;
int y=pos.y;
HDC hdc = GetDC(GetForegroundWindow());
COLORREF pixelcolor = GetPixel(hdc, x, y);
int iRed = GetRValue(pixelcolor);
int iBlue = GetBValue(pixelcolor);
int iGreen = GetGValue(pixelcolor);
while (true)
{
cout << iRed << endl;
cout << iBlue << endl;
cout << iGreen << endl;
Sleep(100);
system("cls");
}
_getch();
return 0;
} |
_________________
|
|
| Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Fri May 29, 2009 8:09 pm Post subject: |
|
|
You get the cursor position and the pixel color once before the loop... you never actually get any new values. It just sits there repeating.
| Code: | #include <Windows.h>
#include <iostream>
int main()
{
POINT pos;
HDC hdc = GetDC(NULL);
while(!GetAsyncKeyState(VK_ESCAPE)){
GetCursorPos(&pos);
std::cout << std::hex << SetPixel(hdc, pos.x, pos.y, RGB(255, 255, 0)) << std::endl;
Sleep(100);
}
ReleaseDC(NULL, hdc);
return 0;
} |
Last edited by hcavolsdsadgadsg on Sat May 30, 2009 12:54 am; edited 1 time in total |
|
| Back to top |
|
 |
FullyAwesome I post too much
Reputation: 0
Joined: 05 Apr 2007 Posts: 4438 Location: Land Down Under
|
Posted: Fri May 29, 2009 8:49 pm Post subject: |
|
|
besides above (as Slovach pointed out that you're just going to output the same value), i also had a problem with the same thing for a while until i realised i was getting the pixel colour of the white cursor (255,255,255). you need to get the position from where your cursor is, store those values, and then move the mouse (physically or via your code), and then get the pixel colour.
_________________
|
|
| Back to top |
|
 |
yoyonerd Grandmaster Cheater
Reputation: 0
Joined: 26 Apr 2008 Posts: 699 Location: -->formerly yoyonerd<--
|
Posted: Fri May 29, 2009 11:53 pm Post subject: |
|
|
| slovach wrote: | You get the cursor position and the pixel color once before the loop... you never actually get any new values. It just sits there repeating.
| Code: | #include <Windows.h>
#include <iostream>
int main()
{
POINT pos;
HDC hdc = GetDC(NULL);
while(!GetAsyncKeyState(VK_ESCAPE)){
GetCursorPos(&pos);
std::cout << std::hex << SetPixel(hdc, pos.x, pos.y, RGB(255, 255, 0)) << std::endl;
Sleep(100);
}
ReleaseDC(NULL, hdc);
return 0;
} |
|
Is there a way to use FindWindow for GetDC?
When I put in FindWindow to get an HWND, I get the 255 issue again?
_________________
|
|
| Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Sun May 31, 2009 3:55 pm Post subject: |
|
|
huh I thought I replied here...
Is whatever you're trying to use GetPixel on a window?
The coordinates are going to be wrong in regards to where the mouse is, unless you have the window perfectly aligned to the top left. You can use ClientToScreen() to get correct coordinates from your window.
http://msdn.microsoft.com/en-us/library/aa931003.aspx
|
|
| Back to top |
|
 |
yoyonerd Grandmaster Cheater
Reputation: 0
Joined: 26 Apr 2008 Posts: 699 Location: -->formerly yoyonerd<--
|
Posted: Sun May 31, 2009 4:18 pm Post subject: |
|
|
| slovach wrote: | huh I thought I replied here...
Is whatever you're trying to use GetPixel on a window?
The coordinates are going to be wrong in regards to where the mouse is, unless you have the window perfectly aligned to the top left. You can use ClientToScreen() to get correct coordinates from your window.
http://msdn.microsoft.com/en-us/library/aa931003.aspx |
Should using this make it possible to GetPixel on a certain window only?
_________________
|
|
| Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Sun May 31, 2009 5:43 pm Post subject: |
|
|
| It is already possible? GetDC takes a HWND...
|
|
| Back to top |
|
 |
yoyonerd Grandmaster Cheater
Reputation: 0
Joined: 26 Apr 2008 Posts: 699 Location: -->formerly yoyonerd<--
|
Posted: Sun May 31, 2009 8:19 pm Post subject: |
|
|
| slovach wrote: | | It is already possible? GetDC takes a HWND... |
Sorry lol, I worded that last post wrong, thanks anyways.
_________________
|
|
| Back to top |
|
 |
|