Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


[C++]GetPixel issues

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
yoyonerd
Grandmaster Cheater
Reputation: 0

Joined: 26 Apr 2008
Posts: 699
Location: -->formerly yoyonerd<--

PostPosted: Fri May 29, 2009 7:38 pm    Post subject: [C++]GetPixel issues Reply with quote

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
View user's profile Send private message AIM Address
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Fri May 29, 2009 8:09 pm    Post subject: Reply with quote

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
View user's profile Send private message
FullyAwesome
I post too much
Reputation: 0

Joined: 05 Apr 2007
Posts: 4438
Location: Land Down Under

PostPosted: Fri May 29, 2009 8:49 pm    Post subject: Reply with quote

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
View user's profile Send private message MSN Messenger
yoyonerd
Grandmaster Cheater
Reputation: 0

Joined: 26 Apr 2008
Posts: 699
Location: -->formerly yoyonerd<--

PostPosted: Fri May 29, 2009 11:53 pm    Post subject: Reply with quote

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
View user's profile Send private message AIM Address
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Sun May 31, 2009 3:55 pm    Post subject: Reply with quote

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
View user's profile Send private message
yoyonerd
Grandmaster Cheater
Reputation: 0

Joined: 26 Apr 2008
Posts: 699
Location: -->formerly yoyonerd<--

PostPosted: Sun May 31, 2009 4:18 pm    Post subject: Reply with quote

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
View user's profile Send private message AIM Address
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Sun May 31, 2009 5:43 pm    Post subject: Reply with quote

It is already possible? GetDC takes a HWND...
Back to top
View user's profile Send private message
yoyonerd
Grandmaster Cheater
Reputation: 0

Joined: 26 Apr 2008
Posts: 699
Location: -->formerly yoyonerd<--

PostPosted: Sun May 31, 2009 8:19 pm    Post subject: Reply with quote

slovach wrote:
It is already possible? GetDC takes a HWND...


Sorry lol, I worded that last post wrong, thanks anyways.

_________________
Back to top
View user's profile Send private message AIM Address
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites