Sage.WindowsMouse
1.0.0.1
dotnet add package Sage.WindowsMouse --version 1.0.0.1
NuGet\Install-Package Sage.WindowsMouse -Version 1.0.0.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Sage.WindowsMouse" Version="1.0.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Sage.WindowsMouse" Version="1.0.0.1" />
<PackageReference Include="Sage.WindowsMouse" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Sage.WindowsMouse --version 1.0.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Sage.WindowsMouse, 1.0.0.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Sage.WindowsMouse@1.0.0.1
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Sage.WindowsMouse&version=1.0.0.1
#tool nuget:?package=Sage.WindowsMouse&version=1.0.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Sage.WindowsMouse
简介
Sage.WindowsMouse 是一个简单易用的 Windows 鼠标设置管理库,提供了对 Windows 系统鼠标设置的读取和修改功能。该库封装了 Win32 API,使开发者能够轻松地在 .NET 应用程序中管理鼠标设置。
主要功能
- 鼠标速度设置和获取
- 鼠标双击时间设置和获取
- 滚轮滚动行数设置和获取
- 鼠标悬停相关设置
- 鼠标轨迹设置
- 增强指针精度设置
- 完全兼容 AOT 编译
安装
通过 NuGet 安装
Install-Package Sage.WindowsMouse
或者使用 .NET CLI:
dotnet add package Sage.WindowsMouse
使用方法
引入命名空间
using Sage.WindowsMouse;
using Sage.WindowsMouse.Models;
获取鼠标设置
获取单个设置
// 获取当前鼠标速度(1-20)
int mouseSpeed = MouseSettingsService.GetMouseSpeed();
Console.WriteLine($"当前鼠标速度:{mouseSpeed}");
// 获取当前双击时间(毫秒)
int doubleClickTime = MouseSettingsService.GetDoubleClickTime();
Console.WriteLine($"当前双击时间:{doubleClickTime}毫秒");
// 获取滚轮滚动行数
int wheelScrollLines = MouseSettingsService.GetWheelScrollLines();
Console.WriteLine($"当前滚轮滚动行数:{wheelScrollLines}");
// 获取鼠标悬停时间
int hoverTime = MouseSettingsService.GetHoverTime();
Console.WriteLine($"当前鼠标悬停时间:{hoverTime}毫秒");
// 获取鼠标悬停区域宽度
int hoverWidth = MouseSettingsService.GetHoverWidth();
Console.WriteLine($"当前鼠标悬停区域宽度:{hoverWidth}像素");
// 获取鼠标悬停区域高度
int hoverHeight = MouseSettingsService.GetHoverHeight();
Console.WriteLine($"当前鼠标悬停区域高度:{hoverHeight}像素");
// 获取鼠标轨迹设置(0-9)
int mouseTrails = MouseSettingsService.GetMouseTrails();
Console.WriteLine($"当前鼠标轨迹设置:{mouseTrails}");
// 获取增强指针精度设置
bool enhancePointerPrecision = MouseSettingsService.GetEnhancePointerPrecision();
Console.WriteLine($"当前增强指针精度设置:{(enhancePointerPrecision ? "开启" : "关闭")}");
获取所有设置
// 获取当前所有鼠标设置
MouseSettings settings = MouseSettingsService.GetCurrentSettings();
Console.WriteLine($"鼠标速度:{settings.Speed}");
Console.WriteLine($"双击时间:{settings.DoubleClickTime}毫秒");
Console.WriteLine($"滚轮滚动行数:{settings.WheelScrollLines}");
Console.WriteLine($"鼠标悬停时间:{settings.HoverTime}毫秒");
Console.WriteLine($"鼠标悬停区域宽度:{settings.HoverWidth}像素");
Console.WriteLine($"鼠标悬停区域高度:{settings.HoverHeight}像素");
Console.WriteLine($"鼠标轨迹设置:{settings.MouseTrails}");
Console.WriteLine($"增强指针精度设置:{(settings.EnhancePointerPrecision == true ? "开启" : "关闭")}");
修改鼠标设置
修改单个设置
try
{
// 设置鼠标速度(1-20)
MouseSettingsService.SetMouseSpeed(10);
// 设置双击时间(100-5000毫秒)
MouseSettingsService.SetDoubleClickTime(500);
// 设置滚轮滚动行数
MouseSettingsService.SetWheelScrollLines(3);
// 设置鼠标悬停时间
MouseSettingsService.SetHoverTime(400);
// 设置鼠标悬停区域宽度
MouseSettingsService.SetHoverWidth(4);
// 设置鼠标悬停区域高度
MouseSettingsService.SetHoverHeight(4);
// 设置鼠标轨迹(0-9,0表示禁用)
MouseSettingsService.SetMouseTrails(0);
// 设置增强指针精度
MouseSettingsService.SetEnhancePointerPrecision(true);
Console.WriteLine("鼠标设置已成功更新");
}
catch (Exception ex)
{
Console.WriteLine($"更新鼠标设置时出错:{ex.Message}");
}
批量修改设置
// 创建新的鼠标设置对象
var newSettings = new MouseSettings
{
Speed = 10, // 鼠标速度
DoubleClickTime = 500, // 双击时间
WheelScrollLines = 3, // 滚轮滚动行数
HoverTime = 400, // 悬停时间
HoverWidth = 4, // 悬停区域宽度
HoverHeight = 4, // 悬停区域高度
MouseTrails = 0, // 鼠标轨迹
EnhancePointerPrecision = true // 增强指针精度
};
try
{
// 分别应用各项设置
if (newSettings.Speed != -1)
MouseSettingsService.SetMouseSpeed(newSettings.Speed);
if (newSettings.DoubleClickTime != -1)
MouseSettingsService.SetDoubleClickTime(newSettings.DoubleClickTime);
if (newSettings.WheelScrollLines != -1)
MouseSettingsService.SetWheelScrollLines(newSettings.WheelScrollLines);
if (newSettings.HoverTime != -1)
MouseSettingsService.SetHoverTime(newSettings.HoverTime);
if (newSettings.HoverWidth != -1)
MouseSettingsService.SetHoverWidth(newSettings.HoverWidth);
if (newSettings.HoverHeight != -1)
MouseSettingsService.SetHoverHeight(newSettings.HoverHeight);
if (newSettings.MouseTrails != -1)
MouseSettingsService.SetMouseTrails(newSettings.MouseTrails);
if (newSettings.EnhancePointerPrecision.HasValue)
MouseSettingsService.SetEnhancePointerPrecision(newSettings.EnhancePointerPrecision.Value);
Console.WriteLine("所有鼠标设置已成功更新");
}
catch (Exception ex)
{
Console.WriteLine($"更新鼠标设置时出错:{ex.Message}");
}
异常处理
该库使用 MouseSettingsException
类来处理鼠标设置操作中可能出现的异常。建议在使用该库时,使用 try-catch 块来捕获和处理这些异常。
try
{
MouseSettingsService.SetMouseSpeed(25); // 超出有效范围(1-20)
}
catch (ArgumentOutOfRangeException ex)
{
Console.WriteLine($"参数错误:{ex.Message}");
}
catch (MouseSettingsException ex)
{
Console.WriteLine($"鼠标设置错误:{ex.Message}");
}
catch (Exception ex)
{
Console.WriteLine($"未知错误:{ex.Message}");
}
注意事项
- 该库需要适当的权限才能修改系统设置。在某些环境下,可能需要管理员权限。
- 部分设置的修改可能需要重新启动应用程序或系统才能完全生效。
- 该库仅适用于 Windows 操作系统。
- 该库需要 .NET 9.0 或更高版本。
许可证
本项目采用 Apache 2.0 许可证。详情请参阅 LICENSE 文件。
贡献
欢迎提交问题报告和改进建议。如果您想贡献代码,请提交拉取请求。
作者
- LiuPengLai - 甲壳虫科技 欢迎提交问题和功能请求。 QQ Group: 1054304346
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net9.0-windows7.0 is compatible. net10.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net9.0-windows7.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.