zoukankan      html  css  js  c++  java
  • C#实现模拟鼠标点击事件(点击桌面的其他程序 )

    注释感觉已经很清楚了,有不懂的欢迎评论

    1
    using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Runtime.InteropServices; 8 using System.Text; 9 using System.Threading; 10 using System.Threading.Tasks; 11 using System.Windows.Forms; 12 13 namespace WindowsFormsApp1 14 { 15 public partial class Form1 : Form 16 { 17 public Form1() 18 { 19 InitializeComponent(); 20 this.Hide(); 21 } 22 /// <summary> 23 /// 引用user32.dll动态链接库(windows api), 24 /// 使用库中定义 API:SetCursorPos 25 /// </summary> 26 [DllImport("user32.dll")] 27 private static extern int SetCursorPos(int x, int y); 28 /// <summary> 29 /// 移动鼠标到指定的坐标点 30 /// </summary> 31 public void MoveMouseToPoint(Point p) 32 { 33 SetCursorPos(p.X, p.Y); 34 } 35 /// <summary> 36 /// 设置鼠标的移动范围 37 /// </summary> 38 public void SetMouseRectangle(Rectangle rectangle) 39 { 40 System.Windows.Forms.Cursor.Clip = rectangle; 41 } 42 /// <summary> 43 /// 设置鼠标位于屏幕中心 44 /// </summary> 45 public void SetMouseAtCenterScreen() 46 { 47 //当前屏幕的宽高 48 int winHeight = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height; 49 int winWidth = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width; 50 //设置鼠标的x,y位置 51 loginx = winWidth / 3 * 2; 52 loginy = winHeight / 4 + 5; 53 Point centerP = new Point(loginx, loginy); 54 //移动鼠标 55 MoveMouseToPoint(centerP); 56 } 57 //点击事件 58 [DllImport("User32")] 59 //下面这一行对应着下面的点击事件 60 // public extern static void mouse_event(int dwFlags, int dx, int dy, int dwData, IntPtr dwExtraInfo); 61 public extern static void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo); 62 public const int MOUSEEVENTF_LEFTDOWN = 0x2; 63 public const int MOUSEEVENTF_LEFTUP = 0x4; 64 public enum MouseEventFlags 65 { 66 Move = 0x0001, //移动鼠标 67 LeftDown = 0x0002,//模拟鼠标左键按下 68 LeftUp = 0x0004,//模拟鼠标左键抬起 69 RightDown = 0x0008,//鼠标右键按下 70 RightUp = 0x0010,//鼠标右键抬起 71 MiddleDown = 0x0020,//鼠标中键按下 72 MiddleUp = 0x0040,//中键抬起 73 Wheel = 0x0800, 74 Absolute = 0x8000//标示是否采用绝对坐标 75 } 76 //鼠标将要到的x,y位置 77 public static int loginx, loginy; 78 private void Form1_Load(object sender, EventArgs e) 79 { 80 81 82 //移动鼠标 83 SetMouseAtCenterScreen(); 84 85 86 //mouse_event((int)(MouseEventFlags.LeftDown | MouseEventFlags.Absolute), loginx, loginy, 0, IntPtr.Zero); 87 88 //mouse_event((int)(MouseEventFlags.LeftUp | MouseEventFlags.Absolute), loginx, loginy, 0, IntPtr.Zero); 89 //点击两次 90 mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, loginx, loginy, 0, 0); 91 92 mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, loginx, loginy, 0, 0); 93 94 } 95 } 96 }
  • 相关阅读:
    第一次上传文件到linux服务器
    @JsonFormat与@DateTimeFormat的区别
    Tomcat端口被占用处理
    简单的Tomcat部署
    PHP源码加密,zend guard与ioncube
    PHP计算年龄
    My97DatePicker时间控件 JQuery UI
    php二维数组自定义排序
    CodeSmith Generator 7.0.2激活步骤
    SQL Server Management Studio 2008版本的安装
  • 原文地址:https://www.cnblogs.com/a1439775520/p/13181657.html
Copyright © 2011-2022 走看看