概述
注释感觉已经很清楚了,有不懂的欢迎评论
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.ComponentModel;4 usingSystem.Data;5 usingSystem.Drawing;6 usingSystem.Linq;7 usingSystem.Runtime.InteropServices;8 usingSystem.Text;9 usingSystem.Threading;10 usingSystem.Threading.Tasks;11 usingSystem.Windows.Forms;12
13 namespaceWindowsFormsApp114 {15 public partial classForm1 : Form16 {17 publicForm1()18 {19 InitializeComponent();20 this.Hide();21 }22 ///
23 ///引用user32.dll动态链接库(windows api),24 ///使用库中定义 API:SetCursorPos25 ///
26 [DllImport("user32.dll")]27 private static extern int SetCursorPos(int x, inty);28 ///
29 ///移动鼠标到指定的坐标点30 ///
31 public voidMoveMouseToPoint(Point p)32 {33 SetCursorPos(p.X, p.Y);34 }35 ///
36 ///设置鼠标的移动范围37 ///
38 public voidSetMouseRectangle(Rectangle rectangle)39 {40 System.Windows.Forms.Cursor.Clip =rectangle;41 }42 ///
43 ///设置鼠标位于屏幕中心44 ///
45 public voidSetMouseAtCenterScreen()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 = newPoint(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, intdwExtraInfo);62 public const int MOUSEEVENTF_LEFTDOWN = 0x2;63 public const int MOUSEEVENTF_LEFTUP = 0x4;64 public enumMouseEventFlags65 {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 intloginx, loginy;78 private void Form1_Load(objectsender, 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 }
最后
以上就是整齐手套为你收集整理的html模拟鼠标点击桌面,C#实现模拟鼠标点击事件(点击桌面的其他程序 )的全部内容,希望文章能够帮你解决html模拟鼠标点击桌面,C#实现模拟鼠标点击事件(点击桌面的其他程序 )所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复