Is there a call I can use that will let me grab the colour from another application/screen. I want to click on the screen and return an RGB value or Hex.
Any help would be appreciated
Printable View
Is there a call I can use that will let me grab the colour from another application/screen. I want to click on the screen and return an RGB value or Hex.
Any help would be appreciated
Kludge method:
Get screen shot (press PRINTSCREEN)
Open Paint
Go to Edit/Paste
Find the color you want
Click the color grabber tool in Paint, click the color
Go to Colors menu, Edit Colors (on Win2k, may be different elsewhere)
Press Define Custom Colors >>
You will see the red, green, blue in the boxes on the right.
Now you can put that in RGB command, and use Hex(RGB(red,green,blue)) to get the hex of the color.
Easy eh?
If you want to do it programmatically then use GetPixel API.
Pass then hDC of the window and X and Y of the mouse pointer.
Better yet. I found the best thing to use is something like this
needs button, picturebox, and 2 labels.
Code:Private Type POINTAPI
X As Long
Y As Long
End Type
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long) As Long
Private Sub Command1_Click()
dim dadc as long
dim z as pointapi
dadc = GetDC(0) 'Don't Know why this works but it does
Do ' Does it until the program closes
Doevents: Doevents 'For Protection
GetCursorPos z ' Tells the computer where mouse is
Picture1.Background = GetPixel(dadc, z.X, z.Y) 'tells it to show the color in a picture box
Label1.Caption = GetPixel(dadc, z.X, z.Y) 'Gives you the number value
Label2.Caption = "X: " & z.X & " " & "Y: " z.Y 'Tells where your mouse is
DoEvents: Doevents 'Overkill, but it works
loop
end sub
Thanks to everyone that replied. All really helpful - and yes, I did want to get the colour from within my application.
Richard
Cogman; if you pass 0 into the GetDC() function, it will return the DC for the entire screen.
still works. :D
Well of course it does, your application is on the screen :)
Anyway I only mentioned it because you said you didnt know why you were passing a 0...