Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting a single pixel color on the screen. 3

Status
Not open for further replies.

PockyBum522

Programmer
Jun 10, 2003
239
US
How do I check the color of a single pixel on the screen at x, y?

Thanks.
 
Don't know about anywhere on the screen, but, here is a code snippet that may be of some help

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Me.Point(X, Y) = RGB(255, 255, 255) Then
MsgBox "I'm a White Pixel!!", vbOKOnly
Else
Me.Text1 = CStr(Me.Point(X, Y))
End If
End Sub
 
It doesn't seem to work, I wanted to return the unknown color of a pixel, not test to see if it is a known color.
 
Works for me!

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Me.Point(X, Y) = RGB(255, 255, 255) Then
MsgBox "I'm a White Pixel!!", vbOKOnly
Else
Me.Text1 = CStr(Me.Point(X, Y))
End If
End Sub

if you put a text box called Text1 on your form then it will show colour value of the pixel the mouse is pointing at as you move it around the form. This is a SAMPLE of how you might do what you are looking to do, of course depending on what your trying to do it could become much more complex as some controls on your form will have mouse move events and some may not.
 
It keeps saying "12632256" I may be wrong, but is this the grey on the form? It only seems to give me that when I move my mouse over the actual form part, if I move it over a picture or textbox control it doesn't change, but if I have the value in the textbox selected, put my mouse on a picture control, and press the delete key to get rid of the stuff in the textbox, it won't change while I move my mouse over the picture, but changes as soon as I move my mouse on the form. What am I doing wrong?
 
you may want to look into using the GetPixel API

good luck

If somethings hard to do, its not worth doing - Homer Simpson
------------------------------------------------------------------------
A General Guide To Excel in VB FAQ222-3383
The Great Date Debate Thread222-368305
File Formats Galore @ or
 
I got it to work with shapes, the problem is so far that's all I've gotten it to work with, and I also need it on the screen, not just on the form. I'll look into getpixel.

Thank you ADoozer and seagoing.
 
Eureka!

I have it!

Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
'This is the data type
Private Type POINTAPI
x As Long 'x coordinate of the mouse
y As Long 'y coordinate of the mouse
End Type
Dim pp As POINTAPI
Private Sub Timer1_Timer()
GetCursorPos pp
Text1.Text = "X: " & pp.x & " Y: " & pp.y
Text2.Text = GetPixel(GetDC(GetDesktopWindow), pp.x, pp.y)
End Sub

This requires a form with 2 textboxes and a module with:

Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long

The first textbox has the mouse coordinates, and the second one has the color of the pixel the mouse is on.
 
Last question: The value of text2.text is a strange color number, it isn't hex or anything I've seen before... Does anyone know how to convert it into rgb?
 
It's probably just a Long. If you want it as 3 hex values:

b = hex$(lngMyColor and &Hff0000)
g = hex$(lngMyColor and &Hff00)
r = hex$(lngMyColor and &Hff)

If you want it as straight Hex:
strHex = Hex$(lngMyColor)

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top