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
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