View Full Version: get background color

C++ Learning Community > C++ Help > get background color


Title: get background color


NOFX - January 11, 2007 06:47 PM (GMT)
Hey, i know its possible to get the hexvalue of a color with your mouse cursor.
So if i would hover over a white area with my cursor then i would get the value 0xFFFFFF or something like that.
But im wondering if its possible to get the color of a certain pixel / coordinate of the screen.

So that i could say something like: get the color of this possition of the screen: x 200 - y 300

Is that possible and if so, how would i do such a thing..?

myork - January 11, 2007 07:11 PM (GMT)

In C++ there is no standard way to do this.
C++ does not understand anything about mice screens colours etc.

What you need to do is look at the OS you are using and then look at what GUI toolkit accesses that kind of information for you.

NOFX - January 11, 2007 09:55 PM (GMT)
I thought that this would be possible with the GDI API's or something. I once wrote the following code in Visual Basic which gives the current color (in hex value) where the mousecursor is located.

CODE

Private Type POINTAPI  
  x As Long  
  y As Long  
  End Type  
 
Private Declare Function GetPixel Lib "gdi32.dll" ( _  
   ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long  
Private Declare Function GetCursorPos Lib "user32.dll" (lpPoint As POINTAPI) As Long  
Private Declare Function GetDC Lib "user32.dll" (ByVal hwnd As Long) As Long  
 
Dim cPos       As POINTAPI  
Dim LastColour As Long  
Dim ScreenDC   As Long  
 
Private Sub Form_Load()  
  LastColour = -1  
  ScreenDC = GetDC(0)  
  Show  
  Timer1.Interval = 100  
  End Sub  
 
Private Sub Timer1_Timer()  
  Dim pColour As Long  
 
  GetCursorPos cPos  
  pColour = GetPixel(GetDC(0), cPos.x, cPos.y)  
  If pColour = LastColour Then Exit Sub  
  LastColour = pColour  
  Label1.BackColor = pColour  
  Label1.Caption = pColour  
End Sub


I can write what i want in VB, thats no problem, but i want to stop doing that and just try to make everything in C++. But i do think it is possible in some similair way like this. I'll do some more search about this tomorrow, but if anyone else has some ideas, then please share. :)

TheHawgMaster - January 12, 2007 04:20 AM (GMT)
1) Use GetCursorPos pick up the mouse state
2) Gain access to the screen with GetDC
3) Apply GetPixel to obtain the color of the pixel at that location
4) Clean up with ReleaseDC

myork - January 12, 2007 01:52 PM (GMT)
QUOTE (NOFX @ Jan 11 2007, 04:55 PM)
I thought that this would be possible with the GDI API's or something. I once wrote the following code in Visual Basic



The missing piece of information: You are using Windows.

The Windows OS provides API's to retrieve this information. See TheHawgMaster's post for details. One of the most important things to learn about C/C++ is what can be done with the language on what requires additional APIs from the OS or other package.




Hosted for free by InvisionFree