Results 1 to 4 of 4

Thread: Finding Color Under Mouse

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2000
    Location
    Oregon
    Posts
    962

    Finding Color Under Mouse

    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.
    Involved in: Sentience

  2. #2
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    yep, GetPixel...
    VB Code:
    1. 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 API
    VB Code:
    1. Declare 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.
    Last edited by chrisjk; Aug 2nd, 2001 at 06:45 PM.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2000
    Location
    Oregon
    Posts
    962
    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.

    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

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2000
    Location
    Oregon
    Posts
    962
    Neither mind, I got it to work.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width