From 54f7aab45cde3a323a14523e751ad3de200c5066 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=BE=89=E9=B8=AD=E8=9B=8B?= Date: Tue, 24 Dec 2024 00:08:34 +0800 Subject: [PATCH] setting --- .../Helpers/Device/MouseSettings.cs | 51 +++++++++++++++++++ .../Helpers/Device/MouseSpeedSettings.cs | 27 ++++++++++ .../Helpers/Device/SystemSettingsManager.cs | 35 +++++++++++++ .../ViewModel/MainWindowViewModel.cs | 3 ++ .../Pages/KeyMouseRecordPageViewModel.cs | 8 +++ 5 files changed, 124 insertions(+) create mode 100644 BetterGenshinImpact/Helpers/Device/MouseSettings.cs create mode 100644 BetterGenshinImpact/Helpers/Device/MouseSpeedSettings.cs create mode 100644 BetterGenshinImpact/Helpers/Device/SystemSettingsManager.cs diff --git a/BetterGenshinImpact/Helpers/Device/MouseSettings.cs b/BetterGenshinImpact/Helpers/Device/MouseSettings.cs new file mode 100644 index 00000000..94d30cdd --- /dev/null +++ b/BetterGenshinImpact/Helpers/Device/MouseSettings.cs @@ -0,0 +1,51 @@ +using System; +using System.Runtime.InteropServices; + +namespace BetterGenshinImpact.Helpers.Device; + +public class MouseSettings +{ + // Import the SystemParametersInfo function from the user32.dll + [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] + private static extern bool SystemParametersInfo( + uint uiAction, uint uiParam, ref uint pvParam, uint fWinIni); + + // Constants for SystemParametersInfo function + private const uint SPI_GETMOUSESPEED = 0x0070; + private const uint SPI_SETMOUSESPEED = 0x0071; + private const uint SPI_GETWHEELSCROLLLINES = 0x0068; + private const uint SPI_SETWHEELSCROLLLINES = 0x0069; + + // Get the current mouse speed + public static uint GetMouseSpeed() + { + uint mouseSpeed = 0; + SystemParametersInfo(SPI_GETMOUSESPEED, 0, ref mouseSpeed, 0); + return mouseSpeed; + } + + // Set the mouse speed + public static void SetMouseSpeed(uint speed) + { + if (speed < 1 || speed > 20) + { + throw new ArgumentOutOfRangeException(nameof(speed), "Mouse speed must be between 1 and 20."); + } + + SystemParametersInfo(SPI_SETMOUSESPEED, speed, ref speed, 0); + } + + // Get the current mouse wheel scroll lines + public static uint GetMouseWheelScrollLines() + { + uint scrollLines = 0; + SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0, ref scrollLines, 0); + return scrollLines; + } + + // Set the mouse wheel scroll lines + public static void SetMouseWheelScrollLines(uint lines) + { + SystemParametersInfo(SPI_SETWHEELSCROLLLINES, lines, ref lines, 0); + } +} \ No newline at end of file diff --git a/BetterGenshinImpact/Helpers/Device/MouseSpeedSettings.cs b/BetterGenshinImpact/Helpers/Device/MouseSpeedSettings.cs new file mode 100644 index 00000000..11050a39 --- /dev/null +++ b/BetterGenshinImpact/Helpers/Device/MouseSpeedSettings.cs @@ -0,0 +1,27 @@ +using System; +using System.Runtime.InteropServices; + +namespace BetterGenshinImpact.Helpers.Device; + +public class MouseSpeedSettings +{ + // Import the SystemParametersInfo function from the user32.dll + [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] + private static extern bool SystemParametersInfo( + uint uiAction, uint uiParam, uint pvParam, uint fWinIni); + + // Constants for SystemParametersInfo function + private const uint SPI_GETMOUSESPEED = 0x0070; + private const uint SPI_SETMOUSESPEED = 0x0071; + + + public static void SetMouseSpeed(uint speed) + { + if (speed < 1 || speed > 20) + { + throw new ArgumentOutOfRangeException(nameof(speed), "Mouse speed must be between 1 and 20."); + } + + SystemParametersInfo(SPI_SETMOUSESPEED, 0, speed, 0); + } +} \ No newline at end of file diff --git a/BetterGenshinImpact/Helpers/Device/SystemSettingsManager.cs b/BetterGenshinImpact/Helpers/Device/SystemSettingsManager.cs new file mode 100644 index 00000000..e673a3a0 --- /dev/null +++ b/BetterGenshinImpact/Helpers/Device/SystemSettingsManager.cs @@ -0,0 +1,35 @@ +using BetterGenshinImpact.GameTask.Common; +using Microsoft.Extensions.Logging; + +namespace BetterGenshinImpact.Helpers.Device; + +public class SystemSettingsManager +{ + // 1. 在启动后强制设置成鼠标灵敏度设置:10, 滚轮一次滚动行数:3;结束后还原 + private static uint _mouseSpeed = 10; + private static uint _wheelScrollLines = 3; + + public static void GetSystemSettings() + { + _mouseSpeed = EnvironmentUtil.GetMouseSpeed(); // 获取鼠标灵敏度 + _wheelScrollLines = EnvironmentUtil.GetWheelScrollLines(); // 获取滚轮滚动行数 + TaskControl.Logger.LogDebug("当前鼠标灵敏度:{M1}, 滚轮滚动行数 {M2}", _mouseSpeed, _wheelScrollLines); + } + + public static void SetSystemSettings() + { + MouseSpeedSettings.SetMouseSpeed(10); // 设置鼠标灵敏度 + MouseSettings.SetMouseWheelScrollLines(3); // 设置滚轮滚动行数 + + TaskControl.Logger.LogDebug("强制设置后,当前鼠标灵敏度:{M1}, 滚轮滚动行数 {M2}", EnvironmentUtil.GetMouseSpeed(), EnvironmentUtil.GetWheelScrollLines()); + } + + public static void RestoreSystemSettings() + { + MouseSpeedSettings.SetMouseSpeed(_mouseSpeed); // 设置鼠标灵敏度 + MouseSettings.SetMouseWheelScrollLines(_wheelScrollLines); // 设置滚轮滚动行数 + + TaskControl.Logger.LogDebug("还原设置后,当前鼠标灵敏度:{M1}, 滚轮滚动行数 {M2}", EnvironmentUtil.GetMouseSpeed(), EnvironmentUtil.GetWheelScrollLines()); + + } +} \ No newline at end of file diff --git a/BetterGenshinImpact/ViewModel/MainWindowViewModel.cs b/BetterGenshinImpact/ViewModel/MainWindowViewModel.cs index 9c8a4890..4e202902 100644 --- a/BetterGenshinImpact/ViewModel/MainWindowViewModel.cs +++ b/BetterGenshinImpact/ViewModel/MainWindowViewModel.cs @@ -18,6 +18,7 @@ using System.Net.Http; using System.Net.Http.Json; using System.Threading.Tasks; using System.Windows; +using BetterGenshinImpact.Helpers.Device; using Wpf.Ui; namespace BetterGenshinImpact.ViewModel; @@ -119,7 +120,9 @@ public partial class MainWindowViewModel : ObservableObject, IViewModel // 更新仓库 // ScriptRepoUpdater.Instance.AutoUpdate(); + EnvironmentUtil.PrintMouseSettings(); + } private async Task GetNewestInfoAsync() diff --git a/BetterGenshinImpact/ViewModel/Pages/KeyMouseRecordPageViewModel.cs b/BetterGenshinImpact/ViewModel/Pages/KeyMouseRecordPageViewModel.cs index e5937908..abf98127 100644 --- a/BetterGenshinImpact/ViewModel/Pages/KeyMouseRecordPageViewModel.cs +++ b/BetterGenshinImpact/ViewModel/Pages/KeyMouseRecordPageViewModel.cs @@ -19,6 +19,7 @@ using System.Threading.Tasks; using System.Windows; using BetterGenshinImpact.Core.Recorder.Model; using BetterGenshinImpact.Helpers; +using BetterGenshinImpact.Helpers.Device; using Wpf.Ui; using Wpf.Ui.Controls; using Wpf.Ui.Violeta.Controls; @@ -122,10 +123,16 @@ public partial class KeyMouseRecordPageViewModel : ObservableObject, INavigation return; } } + + if (!IsRecording) { IsRecording = true; + SystemSettingsManager.GetSystemSettings(); + SystemSettingsManager.SetSystemSettings(); + + fileName = $"{DateTime.Now:yyyy_MM_dd_HH_mm_ss}"; await GlobalKeyMouseRecord.Instance.StartRecord(fileName); } @@ -150,6 +157,7 @@ public partial class KeyMouseRecordPageViewModel : ObservableObject, INavigation _logger.LogDebug(e, "停止录制时发生异常"); _logger.LogWarning(e.Message); } + SystemSettingsManager.RestoreSystemSettings(); } }