html模拟鼠标点击桌面,C#实现模拟鼠标点击事件(点击桌面的其他程序 )

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

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 }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值