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

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
6 Months
Discussion Span
Last Post by Reverend Jim

any help ?

Did you even bother to check out the links I gave you? I have used both of these products to automate tasks for years.

Votes + Comments
Automatic +1

yes i did but if the cursor is above the window it's working, if not, doesn't work


In autoit you can make the desired window active then send commands to click specific controls. If you install Autoit, look at the AutoitX help file. You can instantiate an autoitx object from within vb, c# or even vbscript. Typically to click a button in a window you use

WinActivate - to make the desirec window active
WinWaitActive - to wait for the window to become active
MouseClick - to click a particular button

AutoIt includes a program which you can use to get the name of the button you want to click.


i tried AutoItx3 DLL in VB.NET, used this codes but same problem;
if the cursor is above the window it's working, if not, doesn't work

AutoitCommand.WinActivate("MainWindow", "")
AutoitCommand.WinWaitActive("MainWindow", 1)
AutoitCommand.ControlClick("MainWindow", "", "", "LEFT", 1, 180, 220)

You are clicking based on coordinates. Try clicking based on the button name. For example, I have a script attached to a hotkey for saving multiple images from my browser. I copy a base name string into the clipboard, then when I right click and select SAVE AS I can press a hotkey to save the image in a folder (it takes the base name and cycles through basename ###.jpg from zero and up until it finds an unused file name. It then clicks the SAVE button.

set aut = CreateObject("AutoItX3.Control")
aut.ControlSetText "Save Image", "", "Edit1", filename
aut.ControlClick "Save Image", "", "Button2"

I used the AutoIt utility program to get the names of the filename field (Edit1) and Save (Button2) controls.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.