Results 1 to 3 of 3

Thread: PostMessage Button Click without mouse pointer (VB.Net)

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2016
    Posts
    4

    PostMessage Button Click without mouse pointer (VB.Net)

    Hello,

    I'm trying to develop some code to do a button click onto an application without moving the cursor over to it. The code below is just not doing clicking actions or an error. Its setting focus but not doing a click, and I've tried several different X/Y combinations. Not sure what is going on. Ultimately I'd like to be able to click things on a window even if its not focused... I've searched online for days and I find the similar code repeating but I don't understand how I'm not getting it to work! Any help appreciated thanks!

    Edit :: I can't see any messages under Spy++ so, this might not be a Win32 application? wpf perhaps? Does anyone have experience with clicking on these applications?

    Public Class Main
    Public Const WM_LBUTTONDOWN = &H201
    Public Const WM_LBUTTONUP = &H202
    Private Const BM_CLICK = &HF5
    Private Const WM_ACTIVATE = &H6
    Private Const MA_ACTIVATE = 1
    Private Const MK_LBUTTON = &H1

    Public Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Int32, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As Int32
    Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As IntPtr
    Private Declare Function SetFocus Lib "user32.dll" (ByVal hwnd As Int32) As Int32
    Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Int32


    Private Sub cmdBSPerform_Click(sender As Object, e As EventArgs) Handles cmdBSPerform.Click
    Dim lParam As Int32
    Dim hwnd As Long = FindWindow(vbNullString, "BlueStacks App Player")
    Select Case cmbBSPerforms.SelectedItem
    Case "LeftClick"

    lParam = MakeLParam(txtXBSPerform.Text, txtYBSPerform.Text)

    SetForegroundWindow(hwnd)
    SetFocus(hwnd)
    PostMessage(hwnd, WM_LBUTTONDOWN, MK_LBUTTON, MakeLParam(txtXBSPerform.Text, txtYBSPerform.Text))
    PostMessage(hwnd, WM_LBUTTONUP, 0, MakeLParam(txtXBSPerform.Text, txtYBSPerform.Text))
    PostMessage(hwnd, WM_LBUTTONDOWN, 1, MakeLParam(txtXBSPerform.Text, txtYBSPerform.Text))
    PostMessage(hwnd, WM_LBUTTONUP, 0, MakeLParam(txtXBSPerform.Text, txtYBSPerform.Text))

    End Select
    End Sub

    Public Function MakeLParam(ByVal LoWord As Int32, ByVal HiWord As Int32) As Int32
    Return (HiWord << 16) Or (LoWord And &HFFFF)
    End Function
    End Class
    Last edited by virosoft; Jun 12th, 2016 at 05:02 AM. Reason: win32/wpf

  2. #2
    New Member
    Join Date
    Oct 2016
    Posts
    5

    Re: PostMessage Button Click without mouse pointer (VB.Net)

    If you can't see messages maybe hwnd is wrong

  3. #3
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,807

    Re: PostMessage Button Click without mouse pointer (VB.Net)

    Try calling the System.Runtime.InteropServices.Marshal.GetLastWin32Error() function immediately after calling the PostMessage API function to get the error code of any error that might have occurred. You can a description for the error code as follows:

    Code:
    Dim Message As String = New System.ComponentModel.Win32Exception(error code).Message
    This could help you find out whether any errors are occurring during an API call.

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