PDA

Click to See Complete Forum and Search --> : Finding Color Under Mouse


Gaming_World
Aug 2nd, 2001, 06:21 PM
I know this can be done, beacuse I read the artical, and used it earily. The only problem is, I don't think I have a copy of the code and I can't get to any thing but these forumals. I also tryed VBSquare and VBAPI and neither of them worked. So, I was hoping that someone here might have the code.

Just to let you know (although the subject says it), I want the code that lets you read the color anywhere on the screen (or is it the form?). GetPixel, I belive it is.

Please don't place any urls to anything but the forumals, as I can't get to anything else.

Thanks for any help.

chrisjk
Aug 2nd, 2001, 06:40 PM
yep, GetPixel...Declare Function GetPixel Lib "gdi32" Alias "GetPixel" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long· hdc
Identifies the device context.

· nXPos
Specifies the logical x-coordinate of the pixel to be examined.

· nYPos
Specifies the logical y-coordinate of the pixel to be examined.
You can get the x,y coordinates of the mouse using the GetCursorPos APIDeclare Function GetCursorPos Lib "user32" Alias "GetCursorPos" (lpPoint As POINTAPI) As Long· lpPoint
Points to a POINT structure that receives the screen coordinates of the cursor.

Gaming_World
Aug 2nd, 2001, 06:47 PM
Can any1 explain to me why the following code doesn't work? It always returns -1 from the getpixel, and false from the isbutton code.


Option Explicit

Private Sub Form_Load()
shpButton.BackColor = RGB(1, 1, 1)
shpButton.BorderColor = RGB(255, 0, 0)

Me.Caption = shpButton.Left & " " & shpButton.Top & " " & (shpButton.Width + shpButton.Left) & " " & (shpButton.Top + shpButton.Height)
End Sub

Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim ColorList&(3)

If Button <> 1 Then Exit Sub

ColorList(0) = RGB(1, 1, 1)
ColorList(1) = RGB(255, 0, 0)
ColorList(2) = RGB(254, 254, 254)
ColorList(3) = RGB(0, 0, 255)

If IsButton(Me.hdc, X, Y, ColorList) = True Then
shpButton.BorderColor = RGB(0, 0, 255)
Else
shpButton.BorderColor = RGB(255, 0, 0)
End If
End Sub

Public Function IsButton(hdc&, ByVal X&, ByVal Y&, ColorList&()) As Boolean
Dim Z%, Color&

Color = GetPixel(hdc, X, Y)

For Z = 0 To 1000
DoEvents
Next Z

For Z = LBound(ColorList) To UBound(ColorList)
If Color = ColorList(Z) Then
IsButton = True
Exit For
End If
Next Z
End Function

Gaming_World
Aug 2nd, 2001, 06:59 PM
Neither mind, I got it to work.