博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# 模拟鼠标操作
阅读量:5069 次
发布时间:2019-06-12

本文共 1962 字,大约阅读时间需要 6 分钟。

[Flags]

enum MouseEventFlag : uint //设置鼠标动作的键值
{
Move = 0x0001, //发生移动
LeftDown = 0x0002, //鼠标按下左键
LeftUp = 0x0004, //鼠标松开左键
RightDown = 0x0008, //鼠标按下右键
RightUp = 0x0010, //鼠标松开右键
MiddleDown = 0x0020, //鼠标按下中键
MiddleUp = 0x0040, //鼠标松开中键
XDown = 0x0080,
XUp = 0x0100,
Wheel = 0x0800, //鼠标轮被移动
VirtualDesk = 0x4000, //虚拟桌面
Absolute = 0x8000
}

 

//设置当前鼠标位置

[DllImport("user32.dll")]

static extern bool SetCursorPos(int x, int y);

//操作鼠标,第一个函数定义操作类型,包括移动,单击双击等

[DllImport("user32.dll")]

static extern bool mouse_event(MouseEventFlag flags, int x,int y,uint data,UIntPtr extraInfo);

 

[DllImport("USER32.DLL")]

public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

 

public static void MouseClick()

{
mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, UIntPtr.Zero);
mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);
}

public static void MouseDoubleClick()

{
mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, UIntPtr.Zero);
mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);
Thread.Sleep(50);
mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, UIntPtr.Zero);
mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);
}

public static void MouseRightClick()

{
mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, UIntPtr.Zero);
mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);
}

public static void MouseDoubleRightClick()

{
mouse_event(MouseEventFlag.RightDown, 0, 0, 0, UIntPtr.Zero);
mouse_event(MouseEventFlag.RightUp, 0, 0, 0, UIntPtr.Zero);
Thread.Sleep(50);
mouse_event(MouseEventFlag.RightDown, 0, 0, 0, UIntPtr.Zero);
mouse_event(MouseEventFlag.RightUp, 0, 0, 0, UIntPtr.Zero);
}

public static void SetString(int x,int y,string input)

{
Thread.Sleep(50);
SetCursorPos(x, y);
Thread.Sleep(50);

MouseClick();

Thread.Sleep(50);

Clipboard.SetText(input);

keybd_event(0xA2, 0, 0, 0);

keybd_event(0x56, 0, 0, 0);
keybd_event(0x56, 0, 2, 0);
keybd_event(0xA2, 0, 2, 0);
}

转载于:https://www.cnblogs.com/yangyang12138/p/6473609.html

你可能感兴趣的文章
设置虚拟机虚拟机中fedora上网配置-bridge连接方式(图解)
查看>>
[置顶] Android仿人人客户端(v5.7.1)——人人授权访问界面
查看>>
ES6内置方法find 和 filter的区别在哪
查看>>
Android实现 ScrollView + ListView无滚动条滚动
查看>>
java学习笔记之String类
查看>>
UVA 11082 Matrix Decompressing 矩阵解压(最大流,经典)
查看>>
硬件笔记之Thinkpad T470P更换2K屏幕
查看>>
iOS开发——缩放图片
查看>>
HTTP之URL的快捷方式
查看>>
满世界都是图论
查看>>
配置链路聚合中极小错误——失之毫厘谬以千里
查看>>
蓝桥杯-分小组-java
查看>>
Android Toast
查看>>
iOS开发UI篇—Quartz2D使用(绘制基本图形)
查看>>
docker固定IP地址重启不变
查看>>
桌面图标修复||桌面图标不正常
查看>>
JavaScript基础(四)关于对象及JSON
查看>>
JAVA面试常见问题之Redis篇
查看>>
jdk1.8 api 下载
查看>>
getElement的几中属性介绍
查看>>