Simulation souris

Soyez le premier à donner votre avis sur cette source.

Snippet vu 16 035 fois - Téléchargée 7 fois

Contenu du snippet

//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);
}

Compatibilité : C# 1.x, C# 2.x, C# 3.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.