Les Snippets

Connexion

Simulation souris

Niveau requis pour utiliser/comprendre cette source : 1 ( Débutant )
Créé le 17/05/2009 22:10:21 et initié par Charles Racaud [Liste]
Vue : 4327
Catégorie(s) : API, Trucs & Astuces
Langages dispo pour ce code :
- C# 1.x, C# 2.x, C# 3.x
- VB 2005, VB 2008, VB.NET 1.x
- VB6, VBA



Langage : C# 1.x , C# 2.x , C# 3.x
Date ajout : 17/05/2009
Posté par Charles Racaud [Liste]
//using System;
//using System.Runtime.InteropServices;
//using System.Windows.Forms;

private const int INPUT_MOUSE = 0;
private const uint MOUSEEVENTF_MOVE = 0x0001U;
private const uint MOUSEEVENTF_LEFTDOWN = 0x0002U;
private const uint MOUSEEVENTF_LEFTUP = 0x0004U;
private const uint MOUSEEVENTF_RIGHTDOWN = 0x0008U;
private const uint MOUSEEVENTF_RIGHTUP = 0x0010U;
private const uint MOUSEEVENTF_MIDDLEDOWN = 0x0020U;
private const uint MOUSEEVENTF_MIDDLEUP = 0x0040U;
private const uint MOUSEEVENTF_XDOWN = 0x0080U;
private const uint MOUSEEVENTF_XUP = 0x0100U;
private const uint MOUSEEVENTF_WHEEL = 0x0800U;
private const uint MOUSEEVENTF_HWHEEL = 0x01000U;
private const uint MOUSEEVENTF_ABSOLUTE = 0x8000U;
private const int XBUTTON1 = 0x0001;
private const int XBUTTON2 = 0x0002;
private const int WHEEL_DELTA = 120;

[StructLayout(LayoutKind.Sequential)]
private struct MOUSEINPUT {
  public int dx;
  public int dy;
  public int mouseData;
  public uint dwFlags;
  public uint time;
  public IntPtr dwExtraInfo;
};

[StructLayout(LayoutKind.Explicit)]
private struct INPUT {
  [FieldOffset(0)]
  public uint type;
  [FieldOffset(4)]
  public MOUSEINPUT mi;
};

[DllImport("user32.dll")]
private static extern uint SendInput(uint nInputs, ref INPUT pInputs, int cbSize);

public static uint MouseEvent(int dx, int dy, int mouseData, uint dwFlags, uint time, IntPtr dwExtraInfo) {
  INPUT mInput = 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(1U, ref mInput, Marshal.SizeOf(mInput));
}
public static uint MouseMove(int dx, int dy) {
  return MouseEvent(dx * 65535 / Screen.PrimaryScreen.Bounds.Width, dy * 65535 / Screen.PrimaryScreen.Bounds.Height, 0, MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE, 0U, IntPtr.Zero);
}
public static uint MouseButtonDown(MouseButtons Button) {
  switch (Button) {
    case MouseButtons.Left:
      return MouseEvent(0, 0, 0, MOUSEEVENTF_LEFTDOWN, 0U, IntPtr.Zero);
    case MouseButtons.Middle:
      return MouseEvent(0, 0, 0, MOUSEEVENTF_MIDDLEDOWN, 0U, IntPtr.Zero);
    case MouseButtons.Right:
      return MouseEvent(0, 0, 0, MOUSEEVENTF_RIGHTDOWN, 0U, IntPtr.Zero);
    case MouseButtons.XButton1:
      return MouseEvent(0, 0, XBUTTON1, MOUSEEVENTF_XDOWN, 0U, IntPtr.Zero);
    case MouseButtons.XButton2:
      return MouseEvent(0, 0, XBUTTON2, MOUSEEVENTF_XDOWN, 0U, IntPtr.Zero);
    default:
      return 0;
  }
}
public static uint MouseButtonDown(MouseButtons Button, int dx, int dy) {
  return MouseMove(dx, dy) + MouseButtonDown(Button);
}
public static uint MouseButtonUp(MouseButtons Button) {
  switch (Button) {
    case MouseButtons.Left:
      return MouseEvent(0, 0, 0, MOUSEEVENTF_LEFTUP, 0U, IntPtr.Zero);
    case MouseButtons.Middle:
      return MouseEvent(0, 0, 0, MOUSEEVENTF_MIDDLEUP, 0U, IntPtr.Zero);
    case MouseButtons.Right:
      return MouseEvent(0, 0, 0, MOUSEEVENTF_RIGHTUP, 0U, IntPtr.Zero);
    case MouseButtons.XButton1:
      return MouseEvent(0, 0, XBUTTON1, MOUSEEVENTF_XUP, 0U, IntPtr.Zero);
    case MouseButtons.XButton2:
      return MouseEvent(0, 0, XBUTTON2, MOUSEEVENTF_XUP, 0U, IntPtr.Zero);
    default:
      return 0;
  }
}
public static uint MouseButtonUp(MouseButtons Button, int dx, int dy) {
  return MouseMove(dx, dy) + MouseButtonUp(Button);
}
public static uint MouseButtonClick(MouseButtons Button) {
  return MouseButtonDown(Button) + MouseButtonUp(Button);
}
public static uint MouseButtonClick(MouseButtons Button, int dx, int dy) {
  return MouseMove(dx, dy) + MouseButtonDown(Button) + MouseButtonUp(Button);
}
public static uint MouseWheel(int Count) {
  return MouseEvent(0, 0, (int)(WHEEL_DELTA * Count), MOUSEEVENTF_WHEEL, 0U, IntPtr.Zero);
}
public static uint MouseHWheel(int Count) {
  return MouseEvent(0, 0, (int)(WHEEL_DELTA * Count), MOUSEEVENTF_HWHEEL, 0U, IntPtr.Zero);
}
Langage : VB.NET 1.x , VB 2005 , VB 2008
Date ajout : 17/05/2009
Posté par Charles Racaud [Liste]

'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 UIntegerByRef pInputs As INPUT, ByVal cbSize As IntegerAs UInteger
End Function

Public Function MouseEvent(ByVal dx As IntegerByVal dy As IntegerByVal mouseData As IntegerByVal dwFlags As UIntegerByVal time As UIntegerByVal 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 IntegerByVal dy As IntegerAs 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(000, MOUSEEVENTF_LEFTDOWN, 0UI, IntPtr.Zero)
    Case MouseButtons.Middle
      Return MouseEvent(000, MOUSEEVENTF_MIDDLEDOWN, 0UI, IntPtr.Zero)
    Case MouseButtons.Right
      Return MouseEvent(000, MOUSEEVENTF_RIGHTDOWN, 0UI, IntPtr.Zero)
    Case MouseButtons.XButton1
      Return MouseEvent(00, XBUTTON1, MOUSEEVENTF_XDOWN, 0UI, IntPtr.Zero)
    Case MouseButtons.XButton2
      Return MouseEvent(00, XBUTTON2, MOUSEEVENTF_XDOWN, 0UI, IntPtr.Zero)
    Case Else
      Return 0
  End Select
End Function
Public Function MouseButtonDown(ByVal Button As MouseButtons, ByVal dx As IntegerByVal dy As IntegerAs 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(000, MOUSEEVENTF_LEFTUP, 0UI, IntPtr.Zero)
    Case MouseButtons.Middle
      Return MouseEvent(000, MOUSEEVENTF_MIDDLEUP, 0UI, IntPtr.Zero)
    Case MouseButtons.Right
      Return MouseEvent(000, MOUSEEVENTF_RIGHTUP, 0UI, IntPtr.Zero)
    Case MouseButtons.XButton1
      Return MouseEvent(00, XBUTTON1, MOUSEEVENTF_XUP, 0UI, IntPtr.Zero)
    Case MouseButtons.XButton2
      Return MouseEvent(00, XBUTTON2, MOUSEEVENTF_XUP, 0UI, IntPtr.Zero)
    Case Else
      Return 0
  End Select
End Function
Public Function MouseButtonUp(ByVal Button As MouseButtons, ByVal dx As IntegerByVal dy As IntegerAs 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 IntegerByVal dy As IntegerAs UInteger
  Return MouseMove(dx, dy) + MouseButtonDown(Button) + MouseButtonUp(Button)
End Function
Public Function MouseWheel(ByVal Count As IntegerAs UInteger
  Return MouseEvent(00CInt(WHEEL_DELTA * Count), MOUSEEVENTF_WHEEL, 0UI, IntPtr.Zero)
End Function
Public Function MouseHWheel(ByVal Count As IntegerAs UInteger
  Return MouseEvent(00CInt(WHEEL_DELTA * Count), MOUSEEVENTF_HWHEEL, 0UI, IntPtr.Zero)
End Function


Langage : VB6 , VBA
Date ajout : 17/05/2009
Posté par Charles Racaud [Liste]
Private Const SM_CXSCREEN = 0
Private Const SM_CYSCREEN = 1
Private Const INPUT_MOUSE = 0
Private Const MOUSEEVENTF_MOVE = &H1
Private Const MOUSEEVENTF_LEFTDOWN = &H2
Private Const MOUSEEVENTF_LEFTUP = &H4
Private Const MOUSEEVENTF_RIGHTDOWN = &H8
Private Const MOUSEEVENTF_RIGHTUP = &H10
Private Const MOUSEEVENTF_MIDDLEDOWN = &H20
Private Const MOUSEEVENTF_MIDDLEUP = &H40
Private Const MOUSEEVENTF_XDOWN = &H80
Private Const MOUSEEVENTF_XUP = &H100
Private Const MOUSEEVENTF_WHEEL = &H800
Private Const MOUSEEVENTF_HWHEEL = &H1000
Private Const MOUSEEVENTF_ABSOLUTE = &H8000
Private Const MOUSE_XBUTTON1 = &H1
Private Const MOUSE_XBUTTON2 = &H2
Private Const WHEEL_DELTA = 120

Public Enum MouseButtons
  None = 0
  Left = 1
  Right = 2
  Middle = 4
  XButton1 = 8
  XButton2 = 16
End Enum

Private Type MOUSEINPUT
  dx As Long
  dy As Long
  mouseData As Long
  dwFlags As Long
  time As Long
  dwExtraInfo As Long
End Type
Private Type INPUT_TYPE
  type As Long
  mi As MOUSEINPUT
End Type

Private Declare Function GetSystemMetrics Lib "user32.dll" (ByVal nIndex As LongAs Long
Private Declare Function SendInput Lib "user32.dll" (ByVal nInputs As Long, pInputs As INPUT_TYPE, ByVal cbSize As LongAs Long

' Evénement souris personnalisé
Public Function MouseEvent(ByVal dx As LongByVal dy As LongByVal mouseData As LongByVal dwFlags As LongByVal time As LongByVal dwExtraInfo As LongAs Long
  Dim mInput As INPUT_TYPE
  mInput.type = INPUT_MOUSE
  mInput.mi.dx = dx
  mInput.mi.dy = dy
  mInput.mi.mouseData = mouseData
  mInput.mi.dwFlags = dwFlags
  mInput.mi.time = time
  mInput.mi.dwExtraInfo = dwExtraInfo
  MouseEvent = SendInput(1, mInput, LenB(mInput))
End Function

' Simule le déplacement de la souris
Public Function MouseMove(ByVal dx As LongByVal dy As LongAs Long
  MouseMove = MouseEvent(dx * 65535 \ GetSystemMetrics(SM_CXSCREEN), dy * 65535 \ GetSystemMetrics(SM_CYSCREEN), &O0, MOUSEEVENTF_ABSOLUTE Or MOUSEEVENTF_MOVE, &O0&O0)
End Function

' Simule l'enfoncement d'un bouton de la souris
Public Function MouseButtonDown(ByVal Button As MouseButtons) As Long
  Select Case Button
    Case MouseButtons.Left
      MouseButtonDown = MouseEvent(0&0&0&, MOUSEEVENTF_LEFTDOWN, 0&0&)
    Case MouseButtons.Middle
      MouseButtonDown = MouseEvent(0&0&0&, MOUSEEVENTF_MIDDLEDOWN, 0&0&)
    Case MouseButtons.Right
      MouseButtonDown = MouseEvent(0&0&0&, MOUSEEVENTF_RIGHTDOWN, 0&0&)
    Case MouseButtons.XButton1
      MouseButtonDown = MouseEvent(0&0&, MOUSE_XBUTTON1, MOUSEEVENTF_XDOWN, 0&0&)
    Case MouseButtons.XButton2
      MouseButtonDown = MouseEvent(0&0&, MOUSE_XBUTTON2, MOUSEEVENTF_XDOWN, 0&0&)
    Case Else
      MouseButtonDown = 0
  End Select
End Function

' Simule l'enfoncement d'un bouton de la souris après un déplacement
Public Function MouseButtonDownXY(ByVal Button As MouseButtons, ByVal dx As LongByVal dy As LongAs Long
    MouseButtonDownXY = MouseMove(dx, dy) + MouseButtonDown(Button)
End Function

' Simule le relâchement d'une bouton de la souris
Public Function MouseButtonUp(ByVal Button As MouseButtons) As Long
  Select Case Button
    Case MouseButtons.Left
      MouseButtonUp = MouseEvent(0&0&0&, MOUSEEVENTF_LEFTUP, 0&0&)
    Case MouseButtons.Middle
      MouseButtonUp = MouseEvent(0&0&0&, MOUSEEVENTF_MIDDLEUP, 0&0&)
    Case MouseButtons.Right
      MouseButtonUp = MouseEvent(0&0&0&, MOUSEEVENTF_RIGHTUP, 0&0&)
    Case MouseButtons.XButton1
      MouseButtonUp = MouseEvent(0&0&, MOUSE_XBUTTON1, MOUSEEVENTF_XUP, 0&0&)
    Case MouseButtons.XButton2
      MouseButtonUp = MouseEvent(0&0&, MOUSE_XBUTTON2, MOUSEEVENTF_XUP, 0&0&)
    Case Else
      MouseButtonUp = 0
  End Select
End Function

' Simule le relâchement d'une bouton de la souris après un déplacement
Public Function MouseButtonUpXY(ByVal Button As MouseButtons, ByVal dx As LongByVal dy As LongAs Long
  MouseButtonUpXY = MouseMove(dx, dy) + MouseButtonUp(Button)
End Function

' Simule un clique souris simple
Public Function MouseButtonClick(ByVal Button As MouseButtons) As Long
  MouseButtonClick = MouseButtonDown(Button) + MouseButtonUp(Button)
End Function

' Simule un clique souris simple après un déplacement
Public Function MouseButtonClickXY(ByVal Button As MouseButtons, ByVal dx As LongByVal dy As LongAs Long
  MouseButtonClickXY = MouseMove(dx, dy) + MouseButtonDown(Button) + MouseButtonUp(Button)
End Function

' Simule la molette souris
Public Function MouseWheel(ByVal Count As LongAs Long
  MouseWheel = MouseEvent(0&0&, WHEEL_DELTA * Count, MOUSEEVENTF_WHEEL, 0&0&)
End Function

' Simule la molette horizontale souris
Public Function MouseHWheel(ByVal Count As LongAs Long
  MouseHWheel = MouseEvent(0&0&, WHEEL_DELTA * Count, MOUSEEVENTF_HWHEEL, 0&0&)
End Function

Snippets en rapport avec : Souris, Déplacer, Simuler, Simulation, Clique



Codes sources en rapport avec : Souris, Déplacer, Simuler, Simulation, Clique

{Visual Basic, VB6, VB.NET, VB 2005} MOUSE SIMULATOR
Permet de faire une simulation parfaite de sa souris !!! By JejeSoftware® (autres pseudo Grossoft...

{JAVA / J2EE} SIMULATION D'UN RÉSEAU ATM (BRASSE DES VP/VC)
Il s'agit d'une application JAVA qui "tente" (il reste du travail) de modéliser le brassage des VP/V...

{Visual Basic, VB6, VB.NET, VB 2005} ENREGISTREMENT ET SIMULATION DES CLIQUES DE SOURIS
Ce code a pour but d'enregistrer et simuler les cliques de souris. Mais il permet d'enregistrer dire...

{Visual Basic, VB6, VB.NET, VB 2005} ENREGISTREMENT ET RETRANSCRIPTION DES ACTIONS SOURIS
Voila je me lance, c'est la première source que je poste. C'est un petit programme que j'ai fait po...

{Javascript / DHTML} "DRAG" DE BLOC

Un petit exemple de drag en javascript, pour déplacer un bloc à l'aide de la souris. J'ai pas trouv...

{Javascript / DHTML} MENU CONTEXTUEL GRÂCE AU CLICK DROIT DE LA SOURIS AVEC DES MENUS SECONDAIRES VALIDÉ IE ET FIREFOX
Ce menu contextuel est déclenché par le clique droit de la souris. Le style du menu est uniquement ...

{Visual Basic, VB6, VB.NET, VB 2005} DÉPLACER LA SOURIS PAR CLAVIER
Il suffit de la déplacer avec les touches du clavier (haut, bas, gauche, droite). Code pas trop dif...

{Visual Basic, VB6, VB.NET, VB 2005} ENREGISTRER LES MOUVEMENT ET LES ACTIONS DE LA SOURIS PUIS LES REPRODUIRES
tout est dans le titre......

{Visual Basic, VB6, VB.NET, VB 2005} DÉPLACER LES CONTROLS DE SA FORM AVEC LA SOURIS
Un code pour bouger les controls de la form avec la souris. Ca peut être utile pour la réalisation ...

{C# / C#.NET} PERFORMCLICK, SIMULER LES CLIC SUR CONTRÔLES
Comme il s'agit d'un type de question qui a tendance à revenir régulièrement, voici une classe utili...