PDA

Click to See Complete Forum and Search --> : Colour grabber


rjhare
Nov 13th, 2001, 10:56 AM
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

mlewis
Nov 13th, 2001, 12:30 PM
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?

Serge
Nov 13th, 2001, 01:49 PM
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.

cogman
Nov 13th, 2001, 02:47 PM
Better yet. I found the best thing to use is something like this
needs button, picturebox, and 2 labels.

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

rjhare
Nov 14th, 2001, 12:55 AM
Thanks to everyone that replied. All really helpful - and yes, I did want to get the colour from within my application.

Richard

plenderj
Nov 14th, 2001, 04:23 AM
Cogman; if you pass 0 into the GetDC() function, it will return the DC for the entire screen.

cogman
Nov 14th, 2001, 01:25 PM
still works. :D

plenderj
Nov 15th, 2001, 02:25 AM
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...