<DllImport("user32.dll", SetLastError:=True)> _
Private Shared Function SendInput( _
ByVal cInputs As Integer, _
ByRef pInputs As INPUT, _
ByVal cbSize As Integer) As Integer
End Function
Const INPUT_MOUSE = 0
' Flattened - nested structures are problematic...
Private Structure INPUT
Public dwType As Integer ' starts at 0
Public Mousedx As Integer ' 4
Public Mousedy As Integer ' 8
Public MousemouseData As Integer ' 12
Public MousedwFlags As Integer ' 16
Public Mousetime As Integer ' 20
Public MousedwExtraInfo As IntPtr ' 24 - 28
End Structure
' These had no type and so were objects!!
Const MOUSEEVENTF_LEFTDOWN As Integer = &H2
Const MOUSEEVENTF_LEFTUP As Integer = &H4
Const MOUSEEVENTF_MIDDLEDOWN As Integer = &H20
Const MOUSEEVENTF_MIDDLEUP As Integer = &H40
Const MOUSEEVENTF_MOVE As Integer = &H1
Const MOUSEEVENTF_ABSOLUTE As Integer = &H8000
Const MOUSEEVENTF_RIGHTDOWN As Integer = &H8
Const MOUSEEVENTF_RIGHTUP As Integer = &H10
Sub ClickMouse(ByVal MouseButton As Integer)
Dim inputevents As New INPUT
inputevents.Mousedx = 0
inputevents.Mousedy = 0
inputevents.MousemouseData = 0
inputevents.MousedwFlags = MouseButton
inputevents.Mousetime = 0
inputevents.dwType = INPUT_MOUSE
Dim cbSize As Integer = Marshal.SizeOf(GetType(INPUT))
Dim result As Integer = SendInput(1, inputevents, cbSize)
Me.Text = "result: " & result
Dim lasterror As Integer = Marshal.GetLastWin32Error
Debug.WriteLine("LastError: " & lasterror)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'move mouse to the start button
Dim newMousePosition As New System.Drawing.Point
newMousePosition.X = 20
newMousePosition.Y = Screen.PrimaryScreen.Bounds.Height - 20
System.Windows.Forms.Cursor.Position = newMousePosition
Windows.Forms.Application.DoEvents()
ClickMouse(MOUSEEVENTF_LEFTDOWN) 'press left button
ClickMouse(MOUSEEVENTF_LEFTUP) 'release left button
End Sub