Simulation souris

Contenu du snippet

'Imports System
'Imports System.Runtime.InteropServices
'Imports System.Windows.Forms

Private Const INPUT_MOUSE As Integer = 0
Private Const MOUSEEVENTF_MOVE As UInteger = &H1UI
Private Const MOUSEEVENTF_LEFTDOWN As UInteger = &H2UI
Private Const MOUSEEVENTF_LEFTUP As UInteger = &H4UI
Private Const MOUSEEVENTF_RIGHTDOWN As UInteger = &H8UI
Private Const MOUSEEVENTF_RIGHTUP As UInteger = &H10UI
Private Const MOUSEEVENTF_MIDDLEDOWN As UInteger = &H20UI
Private Const MOUSEEVENTF_MIDDLEUP As UInteger = &H40UI
Private Const MOUSEEVENTF_XDOWN As UInteger = &H80UI
Private Const MOUSEEVENTF_XUP As UInteger = &H100UI
Private Const MOUSEEVENTF_WHEEL As UInteger = &H800UI
Private Const MOUSEEVENTF_HWHEEL As UInteger = &H1000UI
Private Const MOUSEEVENTF_ABSOLUTE As UInteger = &H8000UI
Private Const XBUTTON1 As Integer = &H1UI
Private Const XBUTTON2 As Integer = &H2UI
Private Const WHEEL_DELTA As Integer = 120UI

<StructLayout(LayoutKind.Sequential)> _
Private Structure MOUSEINPUT
  Public dx As Integer
  Public dy As Integer
  Public mouseData As Integer
  Public dwFlags As UInteger
  Public time As UInteger
  Public dwExtraInfo As IntPtr
End Structure

<StructLayout(LayoutKind.Explicit)> _
Private Structure INPUT
  <FieldOffset(0)> _
  Public type As UInteger
  <FieldOffset(4)> _
  Public mi As MOUSEINPUT
End Structure

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

Public Function MouseEvent(ByVal dx As Integer, ByVal dy As Integer, ByVal mouseData As Integer, ByVal dwFlags As UInteger, ByVal time As UInteger, ByVal dwExtraInfo As IntPtr) As UInteger
  Dim mInput As New INPUT()
  mInput.type = INPUT_MOUSE
  mInput.mi = New MOUSEINPUT()
  mInput.mi.dx = dx
  mInput.mi.dy = dy
  mInput.mi.mouseData = mouseData
  mInput.mi.dwFlags = dwFlags
  mInput.mi.time = time
  mInput.mi.dwExtraInfo = dwExtraInfo
  Return SendInput(1UI, mInput, Marshal.SizeOf(mInput))
End Function
Public Function MouseMove(ByVal dx As Integer, ByVal dy As Integer) As UInteger
  Return MouseEvent(dx * 65535 \ Screen.PrimaryScreen.Bounds.Width, dy * 65535 \ Screen.PrimaryScreen.Bounds.Height, 0, MOUSEEVENTF_ABSOLUTE Or MOUSEEVENTF_MOVE, 0UI, IntPtr.Zero)
End Function
Public Function MouseButtonDown(ByVal Button As MouseButtons) As UInteger
  Select Case (Button)
    Case MouseButtons.Left
      Return MouseEvent(0, 0, 0, MOUSEEVENTF_LEFTDOWN, 0UI, IntPtr.Zero)
    Case MouseButtons.Middle
      Return MouseEvent(0, 0, 0, MOUSEEVENTF_MIDDLEDOWN, 0UI, IntPtr.Zero)
    Case MouseButtons.Right
      Return MouseEvent(0, 0, 0, MOUSEEVENTF_RIGHTDOWN, 0UI, IntPtr.Zero)
    Case MouseButtons.XButton1
      Return MouseEvent(0, 0, XBUTTON1, MOUSEEVENTF_XDOWN, 0UI, IntPtr.Zero)
    Case MouseButtons.XButton2
      Return MouseEvent(0, 0, XBUTTON2, MOUSEEVENTF_XDOWN, 0UI, IntPtr.Zero)
    Case Else
      Return 0
  End Select
End Function
Public Function MouseButtonDown(ByVal Button As MouseButtons, ByVal dx As Integer, ByVal dy As Integer) As UInteger
  Return MouseMove(dx, dy) + MouseButtonDown(Button)
End Function
Public Function MouseButtonUp(ByVal Button As MouseButtons) As UInteger
  Select Case (Button)
    Case MouseButtons.Left
      Return MouseEvent(0, 0, 0, MOUSEEVENTF_LEFTUP, 0UI, IntPtr.Zero)
    Case MouseButtons.Middle
      Return MouseEvent(0, 0, 0, MOUSEEVENTF_MIDDLEUP, 0UI, IntPtr.Zero)
    Case MouseButtons.Right
      Return MouseEvent(0, 0, 0, MOUSEEVENTF_RIGHTUP, 0UI, IntPtr.Zero)
    Case MouseButtons.XButton1
      Return MouseEvent(0, 0, XBUTTON1, MOUSEEVENTF_XUP, 0UI, IntPtr.Zero)
    Case MouseButtons.XButton2
      Return MouseEvent(0, 0, XBUTTON2, MOUSEEVENTF_XUP, 0UI, IntPtr.Zero)
    Case Else
      Return 0
  End Select
End Function
Public Function MouseButtonUp(ByVal Button As MouseButtons, ByVal dx As Integer, ByVal dy As Integer) As UInteger
  Return MouseMove(dx, dy) + MouseButtonUp(Button)
End Function
Public Function MouseButtonClick(ByVal Button As MouseButtons) As UInteger
  Return MouseButtonDown(Button) + MouseButtonUp(Button)
End Function
Public Function MouseButtonClick(ByVal Button As MouseButtons, ByVal dx As Integer, ByVal dy As Integer) As UInteger
  Return MouseMove(dx, dy) + MouseButtonDown(Button) + MouseButtonUp(Button)
End Function
Public Function MouseWheel(ByVal Count As Integer) As UInteger
  Return MouseEvent(0, 0, CInt(WHEEL_DELTA * Count), MOUSEEVENTF_WHEEL, 0UI, IntPtr.Zero)
End Function
Public Function MouseHWheel(ByVal Count As Integer) As UInteger
  Return MouseEvent(0, 0, CInt(WHEEL_DELTA * Count), MOUSEEVENTF_HWHEEL, 0UI, IntPtr.Zero)
End Function



Compatibilité : VB 2005, VB 2008, VB.NET 1.x

Disponible dans d'autres langages :

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.