Results 1 to 9 of 9

Thread: Help! Get color of pixel at location "X, Y"

  1. #1

    Thread Starter
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Hang on, you mean:

    Check if the specific screen pixel is a certain colour, and if so, simulate a click event on it?

    You need some API for this. What to do: WindowFromPoint will return the window handle. Then, offset the point into coordinates for that window. Then, get the colour using GetPixel. Compare that, and if so, simulate a click at that position, using SendMessage and WM_CLICK. If you need an example I'll post it here.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  2. #2
    Guest
    is this what you mean?

    Code:
    Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long) As Long
    
    Private Declare Function SetPixel Lib "gdi32" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long, ByVal crColor As Long) As Long
    
    Private Sub Command1_Click()
    For X = 0 To Picture1.Width
      For Y = 0 To Picture1.Height
        If GetPixel(Picture1.hdc, X, Y) = vbRed Then
          Call SetPixel(Picture1.hdc, X, Y, vbYellow)
        End If
      Next
    Next
    End Sub

  3. #3
    New Member
    Join Date
    Aug 2000
    Posts
    3

    Talking parksie:

    parksie: You've got what I'm looking for there. Could you please post that example? Thanks a lot
    Thanks

  4. #4

    Thread Starter
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    will post tomorrow as my isp is about to disconnect me.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  5. #5
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    I would like to differ from parksies suggestion,

    1. You Get the desktopwindow's DC, with getwindowDc and getdesktopwindow
    2. Get the color with getpixel
    3. Use mouseevent to click
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  6. #6
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Ok, i put this together
    Code:
    'In a module
    Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
    Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Long) As Long
    Declare Function GetDesktopWindow Lib "user32" () As Long
    Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
    Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long
    
    Sub OnColorClick(x As Long, y As Long, matchcolor As Long)
        If GetPixel(GetWindowDC(GetDesktopWindow), x, y) = matchcolor Then
            SetCursorPos x, y
             mouse_event 2, 0, 0, 0, 0
             mouse_event 4, 0, 0, 0, 0
        End If
    End Sub
    
    'In code (for instance)
    
    OnColorClick 50, 100, vbYellow
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  7. #7
    Guest
    Code:
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
    Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
    Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
    Private Declare Function GetDesktopWindow Lib "user32" () As Long
    Private Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Long) As Long
    
    Private Const MOUSEEVENTF_LEFTDOWN = &H2 '  left button down
    Private Const MOUSEEVENTF_LEFTUP = &H4 '  left button up
    
    Private Type POINTAPI
            x As Long
            y As Long
    End Type
    
    Dim ThePoint As POINTAPI
    Dim TheHwnd As Long
    Dim TheDC As Long
    Dim TheColor As Long
    
    
    
    
    Private Sub Form_Load()
      
      Timer1.Interval = 25
      Timer2.Interval = 100
    
    End Sub
    
    Private Sub Timer1_Timer()
    
      Call GetCursorPos(ThePoint)
    
      TheHwnd = GetDesktopWindow
      TheDC = GetWindowDC(TheHwnd)
      TheColor = GetPixel(TheDC, ThePoint.x, ThePoint.y)
    
      If TheColor = 12632256 Then
        Call mouse_event(MOUSEEVENTF_LEFTDOWN, ThePoint.x, ThePoint.y, 0, 0)
        Call mouse_event(MOUSEEVENTF_LEFTUP, ThePoint.x, ThePoint.y, 0, 0)
      End If
    
    End Sub
    
    Private Sub Timer2_Timer()
    
      Timer1.Enabled = True
    
    End Sub
    the second Timer is needed because it seems that timer1 keeps disabling itself.

  8. #8
    New Member
    Join Date
    Aug 2000
    Posts
    3

    Talking THANKS! :D

    denniswrenn: THAT was just what I was looking for, thanks a MILLION!!!
    Thanks

  9. #9
    Guest
    I am glad to be of help to you

    oh incase you havent noticed, I have a SendMessage declaration in there, you can remove it, I just forgot to after I finished with it(I was trying to use that instead of mouse_event(BTW: Thanks kedaman!)).

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