/************************************************************ FileName: SendMouse.cs Description: 模拟鼠标点击,左键,右键 Version: ***********************************************************/ using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Automation; using System.ComponentModel; using System.Runtime.InteropServices; using System.Windows; namespace 命名空间 { /// <summary> /// 操作鼠标左右键点击 /// </summary> public class SendMouse { #region ClickMouse #region Import DLL /// <summary> /// Add mouse move event /// </summary> /// <param name="x">Move to specify x coordinate</param> /// <param name="y">Move to specify y coordinate</param> /// <returns></returns> [DllImport("user32.dll")] extern static bool SetCursorPos(int x, int y); /// <summary> /// Mouse click event /// </summary> /// <param name="mouseEventFlag">MouseEventFlag </param> /// <param name="incrementX">X coordinate</param> /// <param name="incrementY">Y coordinate</param> /// <param name="data"></param> /// <param name="extraInfo"></param> [DllImport("user32.dll")] extern static void mouse_event(int mouseEventFlag, int incrementX, int incrementY, uint data, UIntPtr extraInfo); const int MOUSEEVENTF_MOVE = 0x0001; const int MOUSEEVENTF_LEFTDOWN = 0x0002; const int MOUSEEVENTF_LEFTUP = 0x0004; const int MOUSEEVENTF_RIGHTDOWN = 0x0008; const int MOUSEEVENTF_RIGHTUP = 0x0010; const int MOUSEEVENTF_MIDDLEDOWN = 0x0020; const int MOUSEEVENTF_MIDDLEUP = 0x0040; const int MOUSEEVENTF_ABSOLUTE = 0x8000; #endregion public static void ClickLeftMouse(AutomationElement element,int yc) { if (element == null) { throw new NullReferenceException(string.Format("Element with AutomationId '{0}' and Name '{1}' can not be find.", element.Current.AutomationId, element.Current.Name)); } Rect rect = element.Current.BoundingRectangle; int IncrementX = (int)(rect.Left + rect.Width / 2); int IncrementY = (int)(rect.Top + rect.Height / yc); //Make the cursor position to the element. SetCursorPos(IncrementX, IncrementY); //Make the left mouse down and up. mouse_event(MOUSEEVENTF_LEFTDOWN, IncrementX, IncrementY, 0, UIntPtr.Zero); mouse_event(MOUSEEVENTF_LEFTUP, IncrementX, IncrementY, 0, UIntPtr.Zero); } public static void ClickLeftMouse(AutomationElement element, double paddingRight) { if (element == null) { throw new NullReferenceException(string.Format("Element with AutomationId '{0}' and Name '{1}' can not be find.", element.Current.AutomationId, element.Current.Name)); } Rect rect = element.Current.BoundingRectangle; int IncrementX = (int)(rect.Left + rect.Width / 2 + paddingRight); int IncrementY = (int)(rect.Top + rect.Height / 2); //Make the cursor position to the element. SetCursorPos(IncrementX, IncrementY); //Make the left mouse down and up. mouse_event(MOUSEEVENTF_LEFTDOWN, IncrementX, IncrementY, 0, UIntPtr.Zero); mouse_event(MOUSEEVENTF_LEFTUP, IncrementX, IncrementY, 0, UIntPtr.Zero); } public static void ClickLeftMouse(AutomationElement element, double paddingRight,double paddingTop) { if (element == null) { throw new NullReferenceException(string.Format("Element with AutomationId '{0}' and Name '{1}' can not be find.", element.Current.AutomationId, element.Current.Name)); } Rect rect = element.Current.BoundingRectangle; int IncrementX = (int)(rect.Left + rect.Width / 2 + paddingRight); int IncrementY = (int)(rect.Top + rect.Height / 2 + paddingTop); //Make the cursor position to the element. SetCursorPos(IncrementX, IncrementY); //Make the left mouse down and up. mouse_event(MOUSEEVENTF_LEFTDOWN, IncrementX, IncrementY, 0, UIntPtr.Zero); mouse_event(MOUSEEVENTF_LEFTUP, IncrementX, IncrementY, 0, UIntPtr.Zero); } public static void ClickLeftMouse(AutomationElement element) { if (element == null) { throw new NullReferenceException(string.Format("Element with AutomationId '{0}' and Name '{1}' can not be find.", element.Current.AutomationId, element.Current.Name)); } Rect rect = element.Current.BoundingRectangle; int IncrementX = (int)(rect.Left + rect.Width / 2); int IncrementY = (int)(rect.Top + rect.Height / 2); //Make the cursor position to the element. SetCursorPos(IncrementX, IncrementY); //Make the left mouse down and up. mouse_event(MOUSEEVENTF_LEFTDOWN, IncrementX, IncrementY, 0, UIntPtr.Zero); mouse_event(MOUSEEVENTF_LEFTUP, IncrementX, IncrementY, 0, UIntPtr.Zero); } /// <summary> /// 左键点击指定区域 /// </summary> /// <param name="rect">区域</param> public static void ClickLeft(Rect rect) { int IncrementX = (int)(rect.Left + rect.Width / 2); int IncrementY = (int)(rect.Top + rect.Height / 2); //Make the cursor position to the element. SetCursorPos(IncrementX, IncrementY); //Make the left mouse down and up. mouse_event(MOUSEEVENTF_LEFTDOWN, IncrementX, IncrementY, 0, UIntPtr.Zero); mouse_event(MOUSEEVENTF_LEFTUP, IncrementX, IncrementY, 0, UIntPtr.Zero); } public static void ClickLeftMouseRightEdge(AutomationElement element) { if (element == null) { throw new NullReferenceException(string.Format("Element with AutomationId '{0}' and Name '{1}' can not be find.", element.Current.AutomationId, element.Current.Name)); } Rect rect = element.Current.BoundingRectangle; int IncrementX = (int)(rect.Right- rect.Height / 2); int IncrementY = (int)(rect.Top + rect.Height / 2); //Make the cursor position to the element. SetCursorPos(IncrementX, IncrementY); //Make the left mouse down and up. mouse_event(MOUSEEVENTF_LEFTDOWN, IncrementX, IncrementY, 0, UIntPtr.Zero); mouse_event(MOUSEEVENTF_LEFTUP, IncrementX, IncrementY, 0, UIntPtr.Zero); } public static void ClickRightMouse(AutomationElement element) { if (element == null) { throw new NullReferenceException(string.Format("Element with AutomationId '{0}' and Name '{1}' can not be find.", element.Current.AutomationId, element.Current.Name)); } Rect rect = element.Current.BoundingRectangle; int IncrementX = (int)(rect.Left + rect.Width / 2); int IncrementY = (int)(rect.Top + rect.Height / 2); //Make the cursor position to the element. SetCursorPos(IncrementX, IncrementY); //Make the left mouse down and up. mouse_event(MOUSEEVENTF_RIGHTDOWN, IncrementX, IncrementY, 0, UIntPtr.Zero); mouse_event(MOUSEEVENTF_RIGHTUP, IncrementX, IncrementY, 0, UIntPtr.Zero); } public static void SetCursorPosByCon(int IncrementX, int IncrementY) { SetCursorPos(IncrementX, IncrementY); mouse_event(MOUSEEVENTF_LEFTDOWN, IncrementX, IncrementY, 0, UIntPtr.Zero); mouse_event(MOUSEEVENTF_LEFTUP, IncrementX, IncrementY, 0, UIntPtr.Zero); } #endregion } public enum ScrollDrict { LeftScroll, RightScroll, TopScroll, BottomScroll } }