169 lines
5.3 KiB
C#
169 lines
5.3 KiB
C#
using System;
|
|
using System.Runtime.InteropServices;
|
|
|
|
// ReSharper disable InconsistentNaming
|
|
|
|
namespace VAR.ScreenAutomation.Code
|
|
{
|
|
public static class Mouse
|
|
{
|
|
public enum MouseButtons
|
|
{
|
|
Left,
|
|
Middle,
|
|
Right
|
|
}
|
|
|
|
public static void SetButton(MouseButtons button, bool down)
|
|
{
|
|
INPUT input = new INPUT
|
|
{
|
|
Type = INPUT_MOUSE
|
|
};
|
|
input.Data.Mouse.X = 0;
|
|
input.Data.Mouse.Y = 0;
|
|
if (button == MouseButtons.Left)
|
|
{
|
|
input.Data.Mouse.Flags = down ? MOUSEEVENTF_LEFTDOWN : MOUSEEVENTF_LEFTUP;
|
|
}
|
|
|
|
if (button == MouseButtons.Middle)
|
|
{
|
|
input.Data.Mouse.Flags = down ? MOUSEEVENTF_MIDDLEDOWN : MOUSEEVENTF_MIDDLEUP;
|
|
}
|
|
|
|
if (button == MouseButtons.Right)
|
|
{
|
|
input.Data.Mouse.Flags = down ? MOUSEEVENTF_RIGHTDOWN : MOUSEEVENTF_RIGHTUP;
|
|
}
|
|
|
|
INPUT[] inputs = new INPUT[] { input };
|
|
if (SendInput(1, inputs, Marshal.SizeOf(typeof(INPUT))) == 0)
|
|
throw new Exception();
|
|
}
|
|
|
|
public static void Click(MouseButtons button)
|
|
{
|
|
SetButton(button, true);
|
|
System.Threading.Thread.Sleep(500);
|
|
SetButton(button, false);
|
|
}
|
|
|
|
public static void GetPosition(out UInt32 x, out UInt32 y)
|
|
{
|
|
GetCursorPos(out POINT lpPoint);
|
|
x = lpPoint.X;
|
|
y = lpPoint.Y;
|
|
}
|
|
|
|
public static void SetPosition(UInt32 x, UInt32 y)
|
|
{
|
|
SetCursorPos(x, y);
|
|
}
|
|
|
|
[StructLayout(LayoutKind.Sequential)]
|
|
public struct INPUT
|
|
{
|
|
public uint Type;
|
|
public MOUSEKEYBDHARDWAREINPUT Data;
|
|
}
|
|
|
|
public const int INPUT_MOUSE = 0;
|
|
public const int INPUT_KEYBOARD = 1;
|
|
public const int INPUT_HARDWARE = 2;
|
|
|
|
/// <summary>
|
|
/// http://social.msdn.microsoft.com/Forums/en/csharplanguage/thread/f0e82d6e-4999-4d22-b3d3-32b25f61fb2a
|
|
/// </summary>
|
|
[StructLayout(LayoutKind.Explicit)]
|
|
public struct MOUSEKEYBDHARDWAREINPUT
|
|
{
|
|
[FieldOffset(0)] public HARDWAREINPUT Hardware;
|
|
|
|
[FieldOffset(0)] public KEYBDINPUT Keyboard;
|
|
|
|
[FieldOffset(0)] public MOUSEINPUT Mouse;
|
|
}
|
|
|
|
/// <summary>
|
|
/// http://msdn.microsoft.com/en-us/library/windows/desktop/ms646310(v=vs.85).aspx
|
|
/// </summary>
|
|
[StructLayout(LayoutKind.Sequential)]
|
|
public struct HARDWAREINPUT
|
|
{
|
|
public uint Msg;
|
|
public ushort ParamL;
|
|
public ushort ParamH;
|
|
}
|
|
|
|
/// <summary>
|
|
/// http://msdn.microsoft.com/en-us/library/windows/desktop/ms646310(v=vs.85).aspx
|
|
/// </summary>
|
|
[StructLayout(LayoutKind.Sequential)]
|
|
public struct KEYBDINPUT
|
|
{
|
|
public ushort Vk;
|
|
public ushort Scan;
|
|
public uint Flags;
|
|
public uint Time;
|
|
public IntPtr ExtraInfo;
|
|
}
|
|
|
|
/// <summary>
|
|
/// http://social.msdn.microsoft.com/forums/en-US/netfxbcl/thread/2abc6be8-c593-4686-93d2-89785232dacd
|
|
/// https://msdn.microsoft.com/es-es/library/windows/desktop/ms646273%28v=vs.85%29.aspx
|
|
/// </summary>
|
|
[StructLayout(LayoutKind.Sequential)]
|
|
public struct MOUSEINPUT
|
|
{
|
|
public int X;
|
|
public int Y;
|
|
public uint MouseData;
|
|
public uint Flags;
|
|
public uint Time;
|
|
public IntPtr ExtraInfo;
|
|
}
|
|
|
|
public const int MOUSEEVENTD_XBUTTON1 = 0x0001;
|
|
public const int MOUSEEVENTD_XBUTTON2 = 0x0002;
|
|
|
|
public const uint MOUSEEVENTF_ABSOLUTE = 0x8000;
|
|
public const uint MOUSEEVENTF_HWHEEL = 0x01000;
|
|
public const uint MOUSEEVENTF_MOVE = 0x0001;
|
|
public const uint MOUSEEVENTF_MOVE_NOCOALESCE = 0x2000;
|
|
public const uint MOUSEEVENTF_LEFTDOWN = 0x0002;
|
|
public const uint MOUSEEVENTF_LEFTUP = 0x0004;
|
|
public const uint MOUSEEVENTF_RIGHTDOWN = 0x0008;
|
|
public const uint MOUSEEVENTF_RIGHTUP = 0x0010;
|
|
public const uint MOUSEEVENTF_MIDDLEDOWN = 0x0020;
|
|
public const uint MOUSEEVENTF_MIDDLEUP = 0x0040;
|
|
public const uint MOUSEEVENTF_VIRTUALDESK = 0x4000;
|
|
public const uint MOUSEEVENTF_WHEEL = 0x0800;
|
|
public const uint MOUSEEVENTF_XDOWN = 0x0080;
|
|
public const uint MOUSEEVENTF_XUP = 0x0100;
|
|
|
|
[DllImport("User32.dll")]
|
|
public static extern int SendInput(int nInputs, INPUT[] pInputs, int cbSize);
|
|
|
|
|
|
/// <summary>
|
|
/// Struct representing a point.
|
|
/// </summary>
|
|
[StructLayout(LayoutKind.Sequential)]
|
|
public struct POINT
|
|
{
|
|
public UInt32 X;
|
|
public UInt32 Y;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retrieves the cursor's position, in screen coordinates.
|
|
/// </summary>
|
|
/// <see>See MSDN documentation for further information.</see>
|
|
[DllImport("user32.dll")]
|
|
public static extern bool GetCursorPos(out POINT lpPoint);
|
|
|
|
[DllImport("User32.dll")]
|
|
public static extern Boolean SetCursorPos(UInt32 x, UInt32 y);
|
|
}
|
|
} |