Click here to Skip to main content
15,893,337 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to develop some code to do a button click onto an application without moving the cursor over to it. I can see messages under Spy++ but nothing happens.
Thanks in advance...

VB.NET
Public Class Form1
    Const WM_LBUTTONDOWN = &H201
    Const WM_LBUTTONUP = &H202
    Const MK_LBUTTON = &H1
    Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Int32, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam 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 hwnd As Long = FindWindow(vbNullString, handle.Text)
        Dim lParam As Int32
        lParam = MakeLParam(txtXBSPerform.Text, txtYBSPerform.Text)
        SendMessage(hwnd, WM_LBUTTONDOWN, WM_LBUTTONUP, MakeLParam(txtXBSPerform.Text, txtYBSPerform.Text))
        SendMessage(hwnd, WM_LBUTTONUP, 0, MakeLParam(txtXBSPerform.Text, txtYBSPerform.Text))
    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


What I have tried:

I tried do a button click onto an application without moving the cursor over to it, I can see my messages under spy++ but nothing happens
Posted
Comments
[no name] 11-Oct-16 6:39am    
I think you should declare the hwnd parameter as 'IntPtr'
Then the window you are trying to hit should not be in minimized mode in the taskbar, also if the window is behind another window that is overlapped. You have to write a function to check if it is on focus.
EastBlood 11-Oct-16 8:30am    
i did but still not working :/
EastBlood 11-Oct-16 9:14am    
if the cursor is above the window, it's working beautifully. How do I do it? do you know?
[no name] 11-Oct-16 7:26am    
You need to change your parameters, http://pinvoke.net/search.aspx?search=sendmessage&namespace=[All]
EastBlood 11-Oct-16 8:33am    
which one do you recommend ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900