模拟鼠标/键盘 .NET实现
namespace WinFormsApp_ImitateMouseAndKeyboard
{
/// <summary>
/// Mouse 的摘要说明。
/// </summary>
public class Mouse
{
internal const byte SM_MOUSEPRESENT = 19;
internal const byte SM_CMOUSEBUTTONS = 43;
internal const byte SM_MOUSEWHEELPRESENT = 75;
public const int MOUSEEVENTF_LEFTDOWN = 0x2;
public const int MOUSEEVENTF_LEFTUP = 0x4;
public const int MOUSEEVENTF_MIDDLEDOWN = 0x20;
public const int MOUSEEVENTF_MIDDLEUP = 0x40;
public const int MOUSEEVENTF_MOVE = 0x1;
public const int MOUSEEVENTF_RIGHTDOWN = 0x8;
public const int MOUSEEVENTF_RIGHTUP = 0x10;
public struct POINTAPI
{
public int x;
public int y;
}
public struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SwapMouseButton")]
public extern static int SwapMouseButton(int bSwap);
[System.Runtime.InteropServices.DllImport("user32", EntryPoint = "ClipCursor")]
public extern static int ClipCursor(ref RECT lpRect);
[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "GetCursorPos")]
public extern static int GetCursorPos(ref POINTAPI lpPoint);
[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "ShowCursor")]
public extern static bool ShowCursor(bool bShow);
[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "EnableWindow")]
public extern static int EnableWindow(int hwnd, int fEnable);
[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "GetWindowRect")]
public extern static int GetWindowRect(int hwnd, ref RECT lpRect);
[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SetCursorPos")]
public extern static int SetCursorPos(int x, int y);
[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "GetSystemMetrics")]
public extern static int GetSystemMetrics(int nIndex);
[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SetDoubleClickTime")]
public extern static int SetDoubleClickTime(int wCount);
[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "GetDoubleClickTime")]
public extern static int GetDoubleClickTime();
[System.Runtime.InteropServices.DllImport("kernel32.DLL", EntryPoint = "Sleep")]
public extern static void Sleep(int dwMilliseconds);
[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "mouse_event")]
public static extern void mouse_event(
int dwFlags,
int dx,
int dy,
int cButtons,
int dwExtraInfo
);
//得到鼠标相对与全屏的坐标,不是相对与你的Form的,且与你的分辨率有关系
public static int FullScreenPosition_X
{
get
{
POINTAPI _POINTAPI = new POINTAPI();
GetCursorPos(ref _POINTAPI);
return _POINTAPI.x;
}
}
public static int FullScreenPosition_Y
{
get
{
POINTAPI _POINTAPI = new POINTAPI();
GetCursorPos(ref _POINTAPI);
return _POINTAPI.y;
}
}
// 隐藏 显示 鼠标
public static void Hide()
{
ShowCursor(false);
}
public static void Show()
{
ShowCursor(true);
}
// 将鼠标锁定在你的Form里 不过你得将你的Form先锁了,Form Resize 就失效了
public static void Lock(System.Windows.Forms.Form ObjectForm)
{
RECT _FormRect = new RECT();
GetWindowRect(ObjectForm.Handle.ToInt32(), ref _FormRect);
ClipCursor(ref _FormRect);
}
public static void UnLock()
{
RECT _ScreenRect = new RECT();
_ScreenRect.top = 0;
_ScreenRect.left = 0;
_ScreenRect.bottom = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Bottom;
_ScreenRect.right = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Right;
ClipCursor(ref _ScreenRect);
}
// 鼠标失效,不过失效的好像不只是鼠标,小心哦
public static void Disable(System.Windows.Forms.Form ObjectForm)
{
EnableWindow(ObjectForm.Handle.ToInt32(), 0);
}
public static void Enable(System.Windows.Forms.Form ObjectForm)
{
EnableWindow(ObjectForm.Handle.ToInt32(), 1);
}
// 鼠标自己移动 很想动画哦 参数是2个控件的handle
// 看这个方法前,先用凉水擦把脸。。。 反正我写的时候 头晕
public static void Move(int From_Handle_ToInt32, int To_Handle_ToInt32)
{
RECT rectFrom = new RECT();
RECT rectTo = new RECT();
int i;
GetWindowRect(From_Handle_ToInt32, ref rectFrom);
GetWindowRect(To_Handle_ToInt32, ref rectTo);
if ((rectFrom.left + rectFrom.right) / 2 - (rectTo.left + rectTo.right) / 2 > 0)
{
for (i = (rectFrom.left + rectFrom.right) / 2; i >= (rectTo.left + rectTo.right) / 2; i--)
{
SetCursorPos(i, (rectFrom.top + rectFrom.bottom) / 2);
Sleep(1);
}
}
else
{
for (i = (rectFrom.left + rectFrom.right) / 2; i <= (rectTo.left + rectTo.right) / 2; i++)
{
SetCursorPos(i, (rectFrom.top + rectFrom.bottom) / 2);
Sleep(1);
}
}
if ((rectFrom.top + rectFrom.bottom) / 2 - (rectTo.top + rectTo.bottom) / 2 > 0)
{
for (i = (rectFrom.top + rectFrom.bottom) / 2; i >= (rectTo.top + rectTo.bottom) / 2; i--)
{
SetCursorPos((rectTo.left + rectTo.right) / 2, i);
Sleep(1);
}
}
else
{
for (i = (rectFrom.top + rectFrom.bottom) / 2; i <= (rectTo.top + rectTo.bottom) / 2; i++)
{
SetCursorPos((rectTo.left + rectTo.right) / 2, i);
Sleep(1);
}
}
}
// 得到你的鼠标类型
public static string Type
{
get
{
if (GetSystemMetrics(SM_MOUSEPRESENT) == 0)
{
return "本计算机尚未安装鼠标";
}
else
{
if (GetSystemMetrics(SM_MOUSEWHEELPRESENT) != 0)
{
return GetSystemMetrics(SM_CMOUSEBUTTONS) + "键滚轮鼠标";
}
else
{
return GetSystemMetrics(SM_CMOUSEBUTTONS) + "键鼠标";
}
}
}
}
// 设置鼠标双击时间
public static void DoubleClickTime_Set(int MouseDoubleClickTime)
{
SetDoubleClickTime(MouseDoubleClickTime);
}
public static string DoubleClickTime_Get()
{
return GetDoubleClickTime().ToString();
}
// 设置鼠标默认主键 我是没有见过谁左手用鼠标
public static void DefaultRightButton()
{
SwapMouseButton(1);
}
public static void DefaultLeftButton()
{
SwapMouseButton(0);
}
private static void LeftDown()
{
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
}
private static void LeftUp()
{
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
}
public static void LeftClick()
{
LeftDown();
LeftUp();
}
private static void MiddleDown()
{
mouse_event(MOUSEEVENTF_MIDDLEDOWN, 0, 0, 0, 0);
}
private static void MiddleUp()
{
mouse_event(MOUSEEVENTF_MIDDLEUP, 0, 0, 0, 0);
}
public static void MiddleClick()
{
MiddleDown();
MiddleUp();
}
private static void RightDown()
{
mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0);
}
private static void RightUp()
{
mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0);
}
public static void RightClick()
{
RightDown();
RightUp();
}
}
}
using System.Runtime.InteropServices;
namespace WinFormsApp_ImitateMouseAndKeyboard
{
/// <summary>
/// Screen 的摘要说明。
/// </summary>
public class Screen
{
public enum DMDO
{
DEFAULT = 0,
D90 = 1,
D180 = 2,
D270 = 3
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
struct DEVMODE
{
public const int DM_DISPLAYFREQUENCY = 0x400000;
public const int DM_PELSWIDTH = 0x80000;
public const int DM_PELSHEIGHT = 0x100000;
private const int CCHDEVICENAME = 32;
private const int CCHFORMNAME = 32;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCHDEVICENAME)]
public string dmDeviceName;
public short dmSpecVersion;
public short dmDriverVersion;
public short dmSize;
public short dmDriverExtra;
public int dmFields;
public int dmPositionX;
public int dmPositionY;
public DMDO dmDisplayOrientation;
public int dmDisplayFixedOutput;
public short dmColor;
public short dmDuplex;
public short dmYResolution;
public short dmTTOption;
public short dmCollate;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCHFORMNAME)]
public string dmFormName;
public short dmLogPixels;
public int dmBitsPerPel;
public int dmPelsWidth;
public int dmPelsHeight;
public int dmDisplayFlags;
public int dmDisplayFrequency;
public int dmICMMethod;
public int dmICMIntent;
public int dmMediaType;
public int dmDitherType;
public int dmReserved1;
public int dmReserved2;
public int dmPanningWidth;
public int dmPanningHeight;
}
[DllImport("user32.dll", CharSet = CharSet.Auto)]
//static extern int ChangeDisplaySettings( DEVMODE lpDevMode, int dwFlags);
static extern int ChangeDisplaySettings([In] ref DEVMODE lpDevMode, int dwFlags);
public Screen()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public static void changeScree(int W, int H, int F, out long RetVal)
{
RetVal = 0;
DEVMODE dm = new DEVMODE();
dm.dmSize = (short)Marshal.SizeOf(typeof(DEVMODE));
dm.dmPelsWidth = W;
dm.dmPelsHeight = H;
dm.dmDisplayFrequency = F;
dm.dmFields = DEVMODE.DM_PELSWIDTH | DEVMODE.DM_PELSHEIGHT | DEVMODE.DM_DISPLAYFREQUENCY;
RetVal = ChangeDisplaySettings(ref dm, 0);
}
}
}
namespace WinFormsApp_ImitateMouseAndKeyboard
{
/// <summary>
/// Window 的摘要说明。
/// </summary>
public class Windows
{
[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SendMessage")]
public static extern int SendMessage(
int hwnd,
int wMsg,
int wParam,
int lParam
);
}
}
//模拟鼠标/键盘:
//调用:
Mouse.SetCursorPos(MousePosition.X, MousePosition.Y);//(100, 100);
Mouse.LeftClick();//左键
Mouse.RightClick();//右键
//键盘操作可以用:
// SendKeys.SendWait();
//想了解更多,请查询SystemWindowsFormsSendKeysClass相关信息
//如:
//关闭显示器可以用:
//Windows.SendMessage(this.Handle.ToInt32(),0x112,0xF170,2);
//打开
//Windows.SendMessage(this.Handle.ToInt32(),0x112,0xF170,-1);
转载于:https://www.cnblogs.com/jx270/archive/2013/03/16/2962415.html
模拟鼠标/键盘 .NET实现相关推荐
- Delphi下利用WinIo模拟鼠标键盘详解
本文最早在编程论坛上发表,文章地址:http://programbbs.com/bbs/view12-17207-1.htm,相关文件可以在上述地址的页面中下载.转载时请注明出处. 前言 一日发现Se ...
- C#模拟鼠标键盘控制其他窗口(一)
编写程序模拟鼠标和键盘操作可以方便的实现你需要的功能,而不需要对方程序为你开放接口.比如,操作飞信定时发送短信等.我之前开发过飞信耗子,用的是对飞信协议进行抓包,然后分析协议,进而模拟协议的执行,开发 ...
- Linux 模拟 鼠标 键盘 事件
/************************************************************************* Linux 模拟 鼠标 键盘 事件* 说明:* 以 ...
- python 模拟鼠标键盘_python之模拟鼠标键盘动作
上个月就打算开发个还算好玩的项目,但是一直没时间.这篇是此项目用到的一部分, 处理好此部分基本还差通信等方面的了.首先模拟鼠标键盘按下释放的动作,本人利用X11 这个库,所以要了解X11编程;其次,本 ...
- C# 模拟 鼠标 键盘操作
C# 模拟 鼠标 键盘操作 转载于:https://www.cnblogs.com/macT/p/10208222.html
- python模拟鼠标键盘原理_Python-模拟鼠标键盘动作
引 在使用电脑的时候,偶尔有需求要模拟鼠标键盘,进行一些机械重复的操作(刷新网页.抢票.某些小游戏等).如果为此专门下载一个按键精灵,总感觉杀鸡用牛刀,所以就开始探索一些轻量级解决方案.本人电脑上配置 ...
- python截图工具和模拟鼠标键盘_python PyAutoGUI 模拟鼠标键盘操作和截屏功能
简介 一款跨平台/无依赖的自动化测试工具,目测只能控制鼠标/键盘/获取屏幕尺寸/弹出消息框/截屏. 安装 pip install pyautogui 鼠标键盘控制 >>> impor ...
- 模拟鼠标键盘操作,含硬件模拟技术。[转]
模拟鼠标键盘操作,含硬件模拟技术. 键盘是我们使用计算机的一个很重要的输入设备了,即使在鼠标大行其道的今天,很多程序依然离不开键盘来操作.但是有时候,一些重复性的,很繁琐的键盘操作总会让人疲惫,于是就 ...
- PyMouse模拟鼠标键盘操作
PyMouse模拟鼠标键盘操作 使用PyMouse实现模拟鼠标键盘操作 -----解决方案1 1)安装[pyMouse]( https://pypi.org/project/PyMouse/) 2)安 ...
- Python模拟鼠标键盘:pykeyboard库的使用
目录 1.下载安装:pyHook库.PyWin32 2.安装:pykeyboard库 3.模拟鼠标键盘操作 3.1 鼠标操作 3.2 键盘操作 按键属性 4.输入中文 1.下载安装:pyHook库.P ...
最新文章
- 2018-2019-2 20165330《网络对抗技术》Exp9 Web安全基础
- ios推送通知之ios推送证书的申请和使用配置
- 江翰服务器保护系统(JH-Data Backup server)
- pl/sql to_date
- xss_url通关_1-10
- Git区分文件名大小写
- 详解 | Spring Boot 最核心的 3 个注解详解
- python中求根公式_用python做个带GUI的求根公式吧
- 【数据结构】- 二叉树基础操作
- 如果希望完全安装mysql应选择_Windows下安装MySQL最佳实践
- 怎样用python录音_python实现录音功能(可随时停止录音)
- 已知地球上某两个点的坐标(即经纬度),求两点的实际距离。uniapp实现(实现语法js)
- 如何避免IE浏览器自动升级到版本11
- 2020 IJCAI 接受论文 list 分类排列(一)
- 轻轻松松背单词软件测试,十款背单词软件测评报告(转载)
- 刨根究底字符编码之二——关键术语解释(下)
- MPC-BE 1.6.0.6370 Beta 经典多媒体播放器
- 钉钉机器人V1使用说明
- 汇编语言(第三版)第十章 CALL 和 RET 指令 笔记
- Java与PHP之争