Home » Category » Microsoft Visual Basic

Microsoft Visual Basic: Colour grabber

104| Sat, 01 Dec 2007 02:24:00 GMT| rjhare| Comments (7)
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

Keywords & Tags: colour, grabber, microsoft, visual basic, vb

URL: http://www.programmerbase.com/visual-basic/80740/
 
«« Prev - Next »» 7 helpful answers below.
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?

mlewis | Wed, 05 Dec 2007 14:58:00 GMT |

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.

serge | Wed, 05 Dec 2007 14:59:00 GMT |

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

cogman | Wed, 05 Dec 2007 15:00:00 GMT |

Thanks to everyone that replied. All really helpful - and yes, I did want to get the colour from within my application.

Richard

rjhare | Wed, 05 Dec 2007 15:00:00 GMT |

Cogman; if you pass 0 into the GetDC() function, it will return the DC for the entire screen.

plenderj | Wed, 05 Dec 2007 15:02:00 GMT |

still works. :D

cogman | Wed, 05 Dec 2007 15:03:00 GMT |

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...

plenderj | Wed, 05 Dec 2007 15:04:00 GMT |

Microsoft Visual Basic Hot Answers

Microsoft Visual Basic New questions

Microsoft Visual Basic Related Categories