forked from poerin/Stroke
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
290 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
using System; | ||
using System.Drawing; | ||
using System.Runtime.InteropServices; | ||
using System.Windows; | ||
|
||
namespace Stroke | ||
{ | ||
public static class Mouse | ||
{ | ||
private static readonly double ratioX, ratioY; //鼠标移动的坐标需要系数缩放 | ||
private static Point savedPoint=new Point(-1,-1); | ||
static Mouse() | ||
{ | ||
Graphics graphics = Graphics.FromHwnd(IntPtr.Zero); | ||
double width = SystemParameters.VirtualScreenWidth * graphics.DpiX / 96; | ||
double height = SystemParameters.VirtualScreenHeight * graphics.DpiY / 96; | ||
//缩放系数=65536/屏幕分辨率(sendinput中的x,y是0到65535之间的数,映射到整个屏幕) | ||
//屏幕分辨率=屏幕像素点*dpi/96 | ||
ratioX = 65536 / width; | ||
ratioY = 65536 / height; | ||
} | ||
|
||
//处理坐标缩放 | ||
private static void HandleCoordinates(ref Point point) | ||
{ | ||
|
||
point.X = (int)Math.Round(point.X * ratioX, 0); | ||
point.Y = (int)Math.Round(point.Y * ratioY, 0); | ||
return; | ||
} | ||
|
||
public enum Button | ||
{ | ||
Left, | ||
Right, | ||
Middle | ||
} | ||
public enum Action : int | ||
{ | ||
Click, | ||
Down, | ||
Up, | ||
Move | ||
} | ||
|
||
//鼠标移动 | ||
private static void Move(Point point) | ||
{ | ||
HandleCoordinates(ref point); | ||
API.INPUT input = new API.INPUT(); | ||
input.type = API.INPUTTYPE.MOUSE; | ||
input.mi.time = 0; | ||
input.mi.dx = point.X; | ||
input.mi.dy = point.Y; | ||
//移动的坐标包括所有显示器而非只有主显示器 | ||
input.mi.dwFlags = API.MOUSEEVENTF.ABSOLUTE | API.MOUSEEVENTF.VIRTUALDESK | API.MOUSEEVENTF.MOVE; | ||
input.mi.mouseData = 0; | ||
input.mi.dwExtraInfo = (UIntPtr)0x7FuL; | ||
API.SendInput(1u, ref input, Marshal.SizeOf(typeof(API.INPUT))); | ||
} | ||
|
||
//鼠标事件 | ||
public static void Event(Point? point = null, Button button = Button.Left, Action action = Action.Click) | ||
{ | ||
//错误时退出 | ||
if ((action == Action.Move && !point.HasValue)//移动时必须有坐标 | ||
|| (point.HasValue&&(point.Value.X < 0 || point.Value.Y < 0))) //坐标非负 | ||
{ | ||
return; | ||
} | ||
//处理移动 | ||
if (point.HasValue) | ||
{ | ||
Move(point.Value); | ||
} | ||
if (action == Action.Move) | ||
{ | ||
return; | ||
} | ||
//处理鼠标按键事件 | ||
API.INPUT input = new API.INPUT(); | ||
input.type = API.INPUTTYPE.MOUSE; | ||
input.mi.time = 0; | ||
input.mi.mouseData = 0; | ||
input.mi.dwExtraInfo = (UIntPtr)0x7FuL; //Stroke规定的消息,附加以后不会被主程序hook回调函数处理 | ||
//处理按键类别 | ||
API.MOUSEEVENTF MOUSEDOWN, MOUSEUP; | ||
MOUSEDOWN = API.MOUSEEVENTF.LEFTDOWN; //默认左键 | ||
MOUSEUP = API.MOUSEEVENTF.LEFTUP; | ||
switch (button) | ||
{ | ||
case Button.Right: | ||
MOUSEDOWN = API.MOUSEEVENTF.RIGHTDOWN; | ||
MOUSEUP = API.MOUSEEVENTF.RIGHTUP; | ||
break; | ||
case Button.Middle: | ||
MOUSEDOWN = API.MOUSEEVENTF.MIDDLEDOWN; | ||
MOUSEUP = API.MOUSEEVENTF.MIDDLEUP; | ||
break; | ||
default: | ||
break; | ||
} | ||
//处理按键动作 | ||
switch (action) | ||
{ | ||
case Action.Click: | ||
input.mi.dwFlags = MOUSEDOWN | MOUSEUP; | ||
break; | ||
case Action.Down: | ||
input.mi.dwFlags = MOUSEDOWN; | ||
break; | ||
case Action.Up: | ||
input.mi.dwFlags = MOUSEUP; | ||
break; | ||
default: | ||
break; | ||
} | ||
//发送按键 | ||
API.SendInput(1u, ref input, Marshal.SizeOf(typeof(API.INPUT))); | ||
return; | ||
} | ||
|
||
//返回一个point对象 | ||
public static Point Point(int x, int y) | ||
{ | ||
return new Point(x, y); | ||
} | ||
|
||
//返回当前鼠标位置 | ||
public static Point Location() | ||
{ | ||
return System.Windows.Forms.Cursor.Position; | ||
} | ||
|
||
//保存当前鼠标位置 | ||
public static void SaveLocation() | ||
{ | ||
Point location = Location(); | ||
savedPoint.X = location.X; | ||
savedPoint.Y = location.Y; | ||
return; | ||
} | ||
|
||
//恢复之前保存的鼠标的位置 | ||
public static void RestoreLocation() | ||
{ | ||
if (savedPoint.X != -1) | ||
{ | ||
Move(savedPoint); | ||
} | ||
return; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> | ||
<PropertyGroup> | ||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> | ||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> | ||
<ProjectGuid>{DABCD339-6CAD-461D-8A4D-E17716AB5634}</ProjectGuid> | ||
<OutputType>Library</OutputType> | ||
<AppDesignerFolder>Properties</AppDesignerFolder> | ||
<RootNamespace>Mouse</RootNamespace> | ||
<AssemblyName>Mouse</AssemblyName> | ||
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion> | ||
<FileAlignment>512</FileAlignment> | ||
<Deterministic>true</Deterministic> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> | ||
<DebugSymbols>true</DebugSymbols> | ||
<DebugType>full</DebugType> | ||
<Optimize>false</Optimize> | ||
<OutputPath>bin\Debug\</OutputPath> | ||
<DefineConstants>DEBUG;TRACE</DefineConstants> | ||
<ErrorReport>prompt</ErrorReport> | ||
<WarningLevel>4</WarningLevel> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> | ||
<DebugType>pdbonly</DebugType> | ||
<Optimize>true</Optimize> | ||
<OutputPath>bin\Release\</OutputPath> | ||
<DefineConstants>TRACE</DefineConstants> | ||
<ErrorReport>prompt</ErrorReport> | ||
<WarningLevel>4</WarningLevel> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Reference Include="PresentationFramework" /> | ||
<Reference Include="System" /> | ||
<Reference Include="System.Core" /> | ||
<Reference Include="System.Drawing" /> | ||
<Reference Include="System.Windows.Forms" /> | ||
<Reference Include="System.Xml.Linq" /> | ||
<Reference Include="System.Data.DataSetExtensions" /> | ||
<Reference Include="Microsoft.CSharp" /> | ||
<Reference Include="System.Data" /> | ||
<Reference Include="System.Net.Http" /> | ||
<Reference Include="System.Xml" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Compile Include="Mouse.cs" /> | ||
<Compile Include="Properties\AssemblyInfo.cs" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\Stroke\Stroke.csproj"> | ||
<Project>{4bf7e1c7-8683-47a3-99ff-76eceda97bb4}</Project> | ||
<Name>Stroke</Name> | ||
</ProjectReference> | ||
</ItemGroup> | ||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using System.Reflection; | ||
using System.Runtime.InteropServices; | ||
|
||
// 有关程序集的一般信息由以下 | ||
// 控制。更改这些特性值可修改 | ||
// 与程序集关联的信息。 | ||
[assembly: AssemblyTitle("Mouse")] | ||
[assembly: AssemblyDescription("")] | ||
[assembly: AssemblyConfiguration("")] | ||
[assembly: AssemblyCompany("")] | ||
[assembly: AssemblyProduct("Mouse")] | ||
[assembly: AssemblyCopyright("Copyright © 2021")] | ||
[assembly: AssemblyTrademark("")] | ||
[assembly: AssemblyCulture("")] | ||
|
||
// 将 ComVisible 设置为 false 会使此程序集中的类型 | ||
//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型 | ||
//请将此类型的 ComVisible 特性设置为 true。 | ||
[assembly: ComVisible(false)] | ||
|
||
// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID | ||
[assembly: Guid("dabcd339-6cad-461d-8a4d-e17716ab5634")] | ||
|
||
// 程序集的版本信息由下列四个值组成: | ||
// | ||
// 主版本 | ||
// 次版本 | ||
// 生成号 | ||
// 修订号 | ||
// | ||
//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值 | ||
//通过使用 "*",如下所示: | ||
// [assembly: AssemblyVersion("1.0.*")] | ||
[assembly: AssemblyVersion("1.0.0.0")] | ||
[assembly: AssemblyFileVersion("1.0.0.0")] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# 基本用法 | ||
|
||
1. 发送鼠标事件 | ||
|
||
```c# | ||
Mouse.Event(point:Mouse.Point(100,100), | ||
button:Mouse.Button.Left, | ||
action:Mouse.Action.Click); | ||
//button枚举值:Left,Right,Middle | ||
//action枚举值:Click,Up,Down | ||
``` | ||
|
||
2. 获得当前鼠标位置 | ||
|
||
```c# | ||
Point p=Mouse.Location(); | ||
``` | ||
|
||
3. 保存当前鼠标位置 | ||
|
||
```c# | ||
Mouse.SaveLocation(); | ||
``` | ||
|
||
4. 恢复之前保存的鼠标位置 | ||
|
||
```c# | ||
Mouse.RestoreLocation(); | ||
``` | ||
|
||
# 备注 | ||
|
||
```c# | ||
//Point 为 System.Drawing.Point 类 | ||
//Mouse 中实现了一个静态方法 | ||
public static Point Mouse.Point(int x,int y); | ||
//可以返回一个 Point 类来实现更优雅的调用。 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters