Results 1 to 5 of 5

Thread: [vb2008] Press the button of an external not foreground / minimized window

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2009
    Posts
    463

    [vb2008] Press the button of an external not foreground / minimized window

    known the external window size, it Hwnd and the x/y coordinates of the button to press, I am trying this code, like seen on the web:

    Code:
        <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
        Private Shared Function PostMessage(ByVal hWnd As IntPtr, _
        ByVal Msg As  UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Boolean
        End Function
    
        Const WM_LBUTTONDOWN As UInteger = &H201
    
        'button X coordinates, tried both X => respect to it window coordinates
        'and X respect to Screen coordinates (when the window is not minimized)
        Dim X as Integer = ....     
    
        'button Y coordinates, tried both Y => respect to it window coordinates 
        'and Y respect to Screen coordinates (when the window is not minimized)
        Dim Y as Integer = ....   
     
        Dim Hwnd as IntPtr = ...   ' external window handle
    
        PostMessage(Hwnd, WM_LBUTTONDOWN, IntPtr.Zero, MakeLParam(X, Y))
    
    
        Private Function MakeLParam(ByVal LoWord As Integer, ByVal HiWord As Integer) As IntPtr
    
            Return New IntPtr((HiWord << 16) Or (LoWord And &HFFFF))
    
        End Function
    but nothing happens, both if the external window is in foreground, NOT in foreground or minimized...

    what's wrong?

    ty
    Last edited by phil2000; Dec 28th, 2010 at 09:15 PM.

  2. #2
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: [vb2008] Press the button of an external not foreground / minimized window

    If you know the handle of the button you might want to try...
    Code:
        Private Declare Function PostMessage Lib "user32.dll" Alias "PostMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
        Private Const BM_CLICK As Integer = &HF5
    
    '
    '
        Dim ButnHwnd as Integer = ...   ' external button handle
        PostMessage(ButnHwnd, BM_CLICK, 0, 0)

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    May 2009
    Posts
    463

    Re: [vb2008] Press the button of an external not foreground / minimized window

    unfortunately I don't know the handle of the button and I can't know it: it's not a standard Windows button....

  4. #4
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: [vb2008] Press the button of an external not foreground / minimized window

    Quote Originally Posted by phil2000 View Post
    unfortunately I don't know the handle of the button and I can't know it: it's not a standard Windows button....
    One problem might be sending IntPtr.Zero as the mouse button param, shouldn't that be 1 for a left mouse button? Also you would most likely send a WM_LBUTTONUP after sending the WM_LBUTTONDOWN message.

    I don't have time to test in .NET so I just did this in VB6 real quick to send a mouse up & down message to a VB10 apps picturebox, it worked even when the VB10 app was minimized.
    ' <<<< VB6 code >>>>>>
    Code:
    Dim Butn As Long
    Dim DaWord As Long
    Dim rc As RECT
    
    'Butn = ... ' handle of the VB10 app pic box.
    If Butn <> 0 Then
        GetWindowRect Butn, rc ' get cords. 
        DaWord = MakeDWord((rc.Right - rc.Left) / 2, (rc.Bottom - rc.Top) / 2) ' center
        PostMessage Butn, WM_LBUTTONDOWN, 1&, DaWord
        PostMessage Butn, WM_LBUTTONUP, 1&, DaWord
    End If
    
    Private Function MakeDWord(ByVal LoWord As Integer, ByVal HiWord As Integer) As Long
        MakeDWord = (HiWord * &H10000) Or (LoWord And &HFFFF&)
    End Function
    EDIT
    BTW, I noticed when the the VB10 app was minimized the pic box didn't fire a Click event, only the mouse Down and Up events fired, so making it work will depend how your external apps control is coded, if the control detects key presses then sending WM_KEYDOWN/UP messages might work better?

    FWIW, my attempt to convert the VB6 to VB10 code, it works in my test but not sure if it is correctly coded.
    Code:
    Public Class Form1
        Const WM_LBUTTONDOWN As Int32 = &H201
        Const WM_LBUTTONUP As Int32 = &H202
    
        Structure RECT
            Public Left As Int32
            Public Top As Int32
            Public Right As Int32
            Public Bottom As Int32
        End Structure
    
        Declare Function GetWindowRect Lib "user32.dll" (ByVal hwnd As Int32, ByRef lpRect As RECT) As Int32
        Declare Function PostMessage Lib "user32.dll" Alias "PostMessageA" (ByVal hwnd As Int32, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As Int32
    
        Private Function MakeDWord(ByVal LoWord As Int32, ByVal HiWord As Int32) As Int32
            MakeDWord = (HiWord * &H10000) Or (LoWord And &HFFFF)
        End Function
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim Butn, DaWord As Int32
            Dim rc As RECT
    
            ' handle of the control to send messages to
            Butn = 0 ' <<< change to the controls handle.
    
            If Butn Then
                ' cords of control
                GetWindowRect(Butn, rc)
                ' center of control
                DaWord = MakeDWord((rc.Right - rc.Left) / 2, (rc.Bottom - rc.Top) / 2)
                ' messages to control
                PostMessage(Butn, WM_LBUTTONDOWN, 1, DaWord)
                PostMessage(Butn, WM_LBUTTONUP, 1, DaWord)
            End If
        End Sub
    
    End Class
    Last edited by Edgemeal; Dec 29th, 2010 at 01:35 PM. Reason: add more BS

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    May 2009
    Posts
    463

    Re: [vb2008] Press the button of an external not foreground / minimized window

    ty for the reply... nothing to do...

    replacing IntPtr.Zero with Ctype(1,IntPtr) and adding the WM_LBUTTONUP code nothing changes

    at this point may be the better way should be to find the handle of the button... but if I look with Spy++ I can see that in the window there are 8 buttons all with the same ClassName and all without Caption... *** can Spy++ find the handles of 8 different buttons with Same Class Name and without Caption text?????

    EDIT

    no way!!

    I have got the button handle by Spy++ (hex 00010822) and tried:

    Code:
    PostMessage(67618, BM_CLICK, 0, 0)
    but it don't works.... damn Poker Room user defined buttons!
    Last edited by phil2000; Dec 29th, 2010 at 04:47 PM.

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