Hello All,
I was wondering how to click a button on the internet or another program via a program in visual basic 2010.

An example would be: Have a program being able to click the "Search" button in google or the "I'm feeling lucky" button???

Does anyone know how to do this???

also I'm using Visual Basic 2010 and Windows 7.

Thanks is advance!

Member Avatar


Don't know if its possible to detect a button or its location for another program.

I could help you if the window was going to be at a fixed postion and you know the screen coordinates of the button. I could show you how to send a button press to the foreground window at postion(125,50) or any other position.

Let me know if thats something that would help you.

Member Avatar


This will maximize internet explorer if your on the google site and then press the I'm feeling Lucky button.

The coordinates are hard coded. The code that presses the
button is in the timer.Tick event. It will move the cursor
over the button. If its not over it just change the point

If you ever find out how to get the location of a button from
another program, Let me know.

Imports System.Runtime.InteropServices

Public Class Form1

    Private Const LeftMouseDown As Integer = 2
    Private Const LeftMouseUp As Integer = 4
    Private WithEvents t As New Timer

    <DllImport("user32.dll")> _
  Private Shared Function SendInput( _
        ByVal nInputs As Integer, _
        ByVal pInputs() As INPUT, _
        ByVal cbSize As Integer) As Integer
    End Function

    <StructLayout(LayoutKind.Explicit)> _
   Private Structure INPUT
        'Field offset 32 bit machine 4
        '64 bit machine 8
        <FieldOffset(0)> _
        Public type As Integer
        <FieldOffset(8)> _
        Public mi As MOUSEINPUT
        <FieldOffset(8)> _
        Public ki As KEYBDINPUT
        <FieldOffset(8)> _
        Public hi As HARDWAREINPUT
    End Structure

    Private Structure MOUSEINPUT
        Public dx As Integer
        Public dy As Integer
        Public mouseData As Integer
        Public dwFlags As Integer
        Public time As Integer
        Public dwExtraInfo As IntPtr
    End Structure

    Private Structure KEYBDINPUT
        Public wVk As Short
        Public wScan As Short
        Public dwFlags As Integer
        Public time As Integer
        Public dwExtraInfo As IntPtr
    End Structure

    Private Structure HARDWAREINPUT
        Public uMsg As Integer
        Public wParamL As Short
        Public wParamH As Short
    End Structure

    <System.Runtime.InteropServices.DllImport("user32.dll")> _
  Public Shared Function FindWindow( _
       ByVal lpClassName As String, _
       ByVal lpWindowName As String) As IntPtr
    End Function

    <System.Runtime.InteropServices.DllImport("user32.dll")> _
  Public Shared Function ShowWindow( _
       ByVal hWnd As IntPtr, _
       ByVal nCmdShow As Integer) As Boolean
    End Function

    <System.Runtime.InteropServices.DllImport("user32.dll")> _
    Public Shared Function GetWindowThreadProcessId( _
        ByVal hWnd As IntPtr, _
        ByVal lpwdProcessId As IntPtr) As IntPtr
    End Function

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        t.Interval = 10000

    End Sub

    Private Sub t_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles t.Tick
        'If the window title isn't Google - Windows Internet Explorer 
        'then this will do nothing

        Dim WindowHandle As IntPtr = FindWindow(Nothing, "Google - Windows Internet Explorer")
        Dim WindowID As IntPtr = GetWindowThreadProcessId(WindowHandle, IntPtr.Zero)

        If WindowID = IntPtr.Zero Then Exit Sub
        ShowWindow(WindowHandle, 3) 'If the window is minimized, bring it up

        Dim Inpts(1) As INPUT
        Dim screenPoint As Point = PointToScreen(Point.Empty)

        'Hard coded I'm Feeling Lucky Coordinates
        screenPoint.X = 500
        screenPoint.Y = 425

        'Move the cursor over the button and send the press.
        Cursor.Position = screenPoint

        'Mouse Down
        Inpts(0).type = 0
        Inpts(0).mi.dx = screenPoint.X
        Inpts(0).mi.dy = screenPoint.Y
        Inpts(0).mi.dwFlags = LeftMouseDown

        'Mouse Up
        Inpts(1).type = 0
        Inpts(1).mi.dx = screenPoint.X
        Inpts(1).mi.dy = screenPoint.Y
        Inpts(1).mi.dwFlags = LeftMouseUp

        SendInput(2, Inpts, Marshal.SizeOf(GetType(INPUT)))

    End Sub

End Class