C#实现模拟鼠标点击事件(点击桌面的其他程序 )
注释感觉已经很清楚了,有不懂的欢迎评论 1 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms; namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Hide();
}
/// <summary>
/// 引用user32.dll动态链接库(windows api),
/// 使用库中定义 API:SetCursorPos
/// </summary>
[DllImport("user32.dll")]
private static extern int SetCursorPos(int x, int y);
/// <summary>
/// 移动鼠标到指定的坐标点
/// </summary>
public void MoveMouseToPoint(Point p)
{
SetCursorPos(p.X, p.Y);
}
/// <summary>
/// 设置鼠标的移动范围
/// </summary>
public void SetMouseRectangle(Rectangle rectangle)
{
System.Windows.Forms.Cursor.Clip = rectangle;
}
/// <summary>
/// 设置鼠标位于屏幕中心
/// </summary>
public void SetMouseAtCenterScreen()
{
//当前屏幕的宽高
int winHeight = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height;
int winWidth = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width;
//设置鼠标的x,y位置
loginx = winWidth / * ;
loginy = winHeight / + ;
Point centerP = new Point(loginx, loginy);
//移动鼠标
MoveMouseToPoint(centerP);
}
//点击事件
[DllImport("User32")]
//下面这一行对应着下面的点击事件
// public extern static void mouse_event(int dwFlags, int dx, int dy, int dwData, IntPtr dwExtraInfo);
public extern static void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
public const int MOUSEEVENTF_LEFTDOWN = 0x2;
public const int MOUSEEVENTF_LEFTUP = 0x4;
public enum MouseEventFlags
{
Move = 0x0001, //移动鼠标
LeftDown = 0x0002,//模拟鼠标左键按下
LeftUp = 0x0004,//模拟鼠标左键抬起
RightDown = 0x0008,//鼠标右键按下
RightUp = 0x0010,//鼠标右键抬起
MiddleDown = 0x0020,//鼠标中键按下
MiddleUp = 0x0040,//中键抬起
Wheel = 0x0800,
Absolute = 0x8000//标示是否采用绝对坐标
}
//鼠标将要到的x,y位置
public static int loginx, loginy;
private void Form1_Load(object sender, EventArgs e)
{ //移动鼠标
SetMouseAtCenterScreen(); //mouse_event((int)(MouseEventFlags.LeftDown | MouseEventFlags.Absolute), loginx, loginy, 0, IntPtr.Zero); //mouse_event((int)(MouseEventFlags.LeftUp | MouseEventFlags.Absolute), loginx, loginy, 0, IntPtr.Zero);
//点击两次
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, loginx, loginy, , ); mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, loginx, loginy, , ); }
}
}
C#实现模拟鼠标点击事件(点击桌面的其他程序 )的更多相关文章
- 使用Robot类模拟鼠标、键盘事件
Robot类用于模拟鼠标.键盘事件,生成本机系统输入事件.Robot 的主要用于自动化.自运行的程序和其他需要自动控制鼠标和键盘的程序 相当于实际操作的效果,不仅仅只是生成对应的鼠标.键盘事件.比如R ...
- python模拟鼠标键盘操作 GhostMouse tinytask 调用外部脚本或程序 autopy右键另存为
0.关键实现:程序窗口前置 python 通过js控制滚动条拉取全文 通过psutil获取pid窗口句柄,通过win32gui使程序窗口前置 通过pyauto实现右键菜单和另存为操作 1.参考 aut ...
- 2018.7.6 js实现点击事件---点击小图出现大图---时间定时器----注册表单验证
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...
- swift UITapGestureRecognizer获取点击事件点击的位置point
func picTap(sender: UITapGestureRecognizer) { let point = sender.location(in: sender.view) } 其中获取的po ...
- C# 模拟鼠标移动与点击
我们需要用到的mouse_event函数,位于user32.dll这个库文件里面,所以我们要先声明引用. [System.Runtime.InteropServices.DllImport(" ...
- C# 模拟鼠标移动和点击
我们需要用到的mouse_event函数,位于user32.dll这个库文件里面,所以我们要先声明引用. [System.Runtime.InteropServices.DllImport(" ...
- linux下如何模拟按键输入和模拟鼠标【转】
转自:http://www.cnblogs.com/leaven/archive/2010/11/30/1891947.html 查看/dev/input/eventX是什么类型的事件, cat /p ...
- 【ImageView】ImageView点击事件报错空指针
今天在使用自定义圆形imageview的时候,想利用其点击事件来实现查看个人资料功能,但是该空间在Activity中的onCreate方法中调用点击事件总是出现空指针异常,每次程序都进不去主页面,到处 ...
- jquery点击事件失效原因和解决办法
在使用jQuery绑定点击事件的时候,有时候会遇到点击无效,这种情况大多出现在动态添加元素的时候 例如:给demo里添加li元素给li绑定点击事件 $("#demo").appen ...
- jquery 与javascript关系 ①取元素 ②操作内容 ③操作属性 ④操作 样式 ⑤ 事件 点击变色
jQuery的min版本和原版功能是一样的,min版主要应用于已经开发成的网页中,而非min版 的文件比较大,里面有整洁的代码书写规范和注释,主要应用于脚本开发过程当中. JQuery是继protot ...
随机推荐
- [安卓自动化测试] 001.UIAutomator初探
*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...
- Spring boot Sample 002之spring-boot-banner
一.环境 1.1.Idea 2020.1 1.2.JDK 1.8 二.目的 学习Spring Boot Banner自定义的操作 三.步骤 3.1.点击File -> New Project ...
- Chisel3 - 运算符和位宽推断
https://mp.weixin.qq.com/s/rI-CJM6GyI6EUHPZ3uYiFg 如同Verilog中的众多运算符,Chisel也针对自身的数据类型,提供了很多运算符. Ch ...
- Java实现 LeetCode 822 翻转卡片游戏(暴力)
822. 翻转卡片游戏 在桌子上有 N 张卡片,每张卡片的正面和背面都写着一个正数(正面与背面上的数有可能不一样). 我们可以先翻转任意张卡片,然后选择其中一张卡片. 如果选中的那张卡片背面的数字 X ...
- C# winform 学习(一)
目标 1.类和对象 2.定义类 3.对象的操作 4.命名空间 一.类和对象 1.理解 1)类:具有共同特征和行为的一类事物的统称 2)对象:类的一个具体唯一的实例 eg: 1路公交车;(类) 车牌为F ...
- Java实现 LeetCode 36 有效的数独
36. 有效的数独 判断一个 9x9 的数独是否有效.只需要根据以下规则,验证已经填入的数字是否有效即可. 数字 1-9 在每一行只能出现一次. 数字 1-9 在每一列只能出现一次. 数字 1-9 在 ...
- Java实现LeetCode_0007_ReverseInteger
package javaLeetCode_primary; import java.util.Scanner; /** * Given a 32-bit signed integer, reverse ...
- cocos2dx 实现遮罩
参考博文:http://blog.csdn.net/myarrow/article/details/19913653 参考博文:http://blog.csdn.net/song_hui_xiang/ ...
- mysql基础-数据库表的管理-记录(四)
0x01 MySQL中字符大小写 1.SQL关键字及函数不区分大小写 2.数据库.表及视图名称的大小写区分与否取决于底层OS及FS 3.存储过程.存储函数及事件调度器的名字不区分大小写,但触发器区分大 ...
- hibernate 用注解方式生成uuid方法
//配置uuid,本来jpa是不支持uuid的,但借用hibernate的方法可以实现. @GeneratedValue(generator = "uuid") @Generate ...