XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

HaloPixelToolBox

【WinUI3】基于USB HID通讯的花再音响工具箱

公开
关注 0 Fork 0 Star 0
返回提交历史

XFEstudio/HaloPixelToolBox

完善部分内容

e858bea
XFE工作室室长 <mail@xfegzs.com>
提交于

代码差异

10 个文件 +122 -131
Modified HaloPixelToolBox/HaloPixelToolBox/App.xaml.cs +5 -3
@@ -3,7 +3,6 @@ using HaloPixelToolBox.Profiles.CrossVersionProfiles;
3 3 using HaloPixelToolBox.Utilities;
4 4 using HaloPixelToolBox.Utilities.Helpers;
5 5 using Microsoft.UI.Dispatching;
6 using System.Runtime.InteropServices;
7 6 using XFEExtension.NetCore.WinUIHelper.Interface.Services;
8 7 using XFEExtension.NetCore.WinUIHelper.Utilities;
9 8 using XFEExtension.NetCore.WinUIHelper.Utilities.Helper;
@@ -92,13 +91,16 @@ namespace HaloPixelToolBox
92 91 TrayIconService.Initilize(DispatcherQueue.GetForCurrentThread());
93 92 MainWindow.Closed += MainWindow_Closed;
94 93 MainWindow.Content = new AppShellPage();
95 MainWindow.Activate();
94 if (SystemProfile.MinimizeWhenOpen)
95 MainWindow.AppWindow.Hide();
96 else
97 MainWindow.Activate();
96 98 AppThemeHelper.MainWindow = MainWindow;
97 99 }
98 100
99 101 private void MainWindow_Closed(object sender, WindowEventArgs args)
100 102 {
101 ShowWindow(WindowHelper.GetHwndForCurrentWindow(), SW_HIDE);
103 Win32Helper.ShowWindow(WindowHelper.GetHwndForCurrentWindow(), Win32Helper.SW_HIDE);
102 104 args.Handled = true;
103 105 }
104 106 }
Modified HaloPixelToolBox/HaloPixelToolBox/Profiles/CrossVersionProfiles/SystemProfile.cs +5 -0
@@ -22,6 +22,11 @@ public partial class SystemProfile : XFEProfile
22 22 /// </summary>
23 23 [ProfileProperty]
24 24 private string ignoreVersion = string.Empty;
25 /// <summary>
26 /// 启动时最小化到托盘
27 /// </summary>
28 [ProfileProperty]
29 private bool minimizeWhenOpen = false;
25 30
26 31 static partial void SetThemeProperty(ref ElementTheme value) => AppThemeHelper.ChangeTheme(value);
27 32 }
Modified HaloPixelToolBox/HaloPixelToolBox/Utilities/Helpers/Win32/MSLLHOOKSTRUCT.cs +9 -12
@@ -1,16 +1,13 @@
1 1 using System.Runtime.InteropServices;
2 2
3 namespace HaloPixelToolBox.Utilities.Helpers;
3 namespace HaloPixelToolBox.Utilities.Helpers.Win32;
4 4
5 public static partial class Win32Helper
5 [StructLayout(LayoutKind.Sequential)]
6 public struct MSLLHOOKSTRUCT
6 7 {
7 [StructLayout(LayoutKind.Sequential)]
8 public struct MSLLHOOKSTRUCT
9 {
10 public POINT pt;
11 public uint mouseData;
12 public uint flags;
13 public uint time;
14 public IntPtr dwExtraInfo;
15 }
16 }
8 public POINT pt;
9 public uint mouseData;
10 public uint flags;
11 public uint time;
12 public IntPtr dwExtraInfo;
13 }
Modified HaloPixelToolBox/HaloPixelToolBox/Utilities/Helpers/Win32/POINT.cs +5 -8
@@ -2,12 +2,9 @@
2 2
3 3 namespace HaloPixelToolBox.Utilities.Helpers.Win32;
4 4
5 public static partial class Win32Helper
5 [StructLayout(LayoutKind.Sequential)]
6 public struct POINT
6 7 {
7 [StructLayout(LayoutKind.Sequential)]
8 public struct POINT
9 {
10 public int x;
11 public int y;
12 }
13 }
8 public int x;
9 public int y;
10 }
Modified HaloPixelToolBox/HaloPixelToolBox/Utilities/Helpers/Win32/RECT.cs +4 -7
@@ -2,11 +2,8 @@
2 2
3 3 namespace HaloPixelToolBox.Utilities.Helpers.Win32;
4 4
5 public static partial class Win32Helper
5 [StructLayout(LayoutKind.Sequential)]
6 public struct RECT
6 7 {
7 [StructLayout(LayoutKind.Sequential)]
8 public struct RECT
9 {
10 public int Left, Top, Right, Bottom;
11 }
12 }
8 public int Left, Top, Right, Bottom;
9 }
Modified HaloPixelToolBox/HaloPixelToolBox/Utilities/Helpers/Win32Helper.cs +3 -27
@@ -5,49 +5,25 @@ namespace HaloPixelToolBox.Utilities.Helpers;
5 5
6 6 public static partial class Win32Helper
7 7 {
8
9 8 public const int SW_HIDE = 0;
10 9 public const int SW_SHOW = 5;
11 10
12 11 public const int WH_MOUSE_LL = 14;
13 12 public const int WM_LBUTTONDOWN = 0x0201;
14 13
15 [StructLayout(LayoutKind.Sequential)]
16 public struct POINT
17 {
18 public int x;
19 public int y;
20 }
21
22 [StructLayout(LayoutKind.Sequential)]
23 public struct MSLLHOOKSTRUCT
24 {
25 public POINT pt;
26 public uint mouseData;
27 public uint flags;
28 public uint time;
29 public IntPtr dwExtraInfo;
30 }
31
32 [StructLayout(LayoutKind.Sequential)]
33 public struct RECT
34 {
35 public int Left, Top, Right, Bottom;
36 }
37
38 14 [LibraryImport("user32.dll")]
39 15 [return: MarshalAs(UnmanagedType.Bool)]
40 16 public static partial bool ShowWindow(IntPtr hWnd, int nCmdShow);
41 17
42 [LibraryImport("user32.dll")]
43 public static partial IntPtr SetWindowsHookEx(int idHook, LowLevelMouseProc lpfn, IntPtr hMod, uint dwThreadId);
18 [DllImport("user32.dll")]
19 public static extern IntPtr SetWindowsHookEx(int idHook, LowLevelMouseProc lpfn, IntPtr hMod, uint dwThreadId);
44 20
45 21 [LibraryImport("user32.dll")]
46 22 [return: MarshalAs(UnmanagedType.Bool)]
47 23 public static partial bool UnhookWindowsHookEx(IntPtr hhk);
48 24
49 25 [LibraryImport("user32.dll")]
50 private static partial IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
26 public static partial IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
51 27
52 28 [LibraryImport("user32.dll")]
53 29 [return: MarshalAs(UnmanagedType.Bool)]
Modified HaloPixelToolBox/HaloPixelToolBox/ViewModels/SettingPageViewModel.cs +52 -49
@@ -10,68 +10,71 @@ using XFEExtension.NetCore.WinUIHelper.Interface.Services;
10 10 using XFEExtension.NetCore.WinUIHelper.Utilities;
11 11 using XFEExtension.NetCore.WinUIHelper.Utilities.Helper;
12 12
13 namespace HaloPixelToolBox.ViewModels
13 namespace HaloPixelToolBox.ViewModels;
14
15 public partial class SettingPageViewModel : ViewModelBase
14 16 {
15 public partial class SettingPageViewModel : ViewModelBase
17 [ObservableProperty]
18 bool isAutoStartEnable = SystemProfile.AutoStart;
19 [ObservableProperty]
20 private bool minimizeWhenOpen = SystemProfile.MinimizeWhenOpen;
21 [ObservableProperty]
22 string appCacheDirectory = AppPathHelper.AppCache;
23 [ObservableProperty]
24 string appCacheSize = FileHelper.GetDirectorySize(new(AppPathHelper.AppCache)).FileSize();
25 [ObservableProperty]
26 string appDataDirectory = AppPathHelper.AppLocalData;
27 [ObservableProperty]
28 string appDataSize = FileHelper.GetDirectorySize(new(AppPathHelper.AppLocalData)).FileSize();
29 [ObservableProperty]
30 string appLogDirectory = AppPath.LogDictionary;
31 [ObservableProperty]
32 string appLogSize = FileHelper.GetDirectorySize(new(AppPath.LogDictionary)).FileSize();
33 [ObservableProperty]
34 private string currentVersion = Assembly.GetEntryAssembly()?.GetName().Version is Version version ? version.ToString(3) : "无法获取版本信息";
35 public ISettingService SettingService { get; set; } = ServiceManager.GetService<ISettingService>();
36 public IDialogService DialogService { get; set; } = ServiceManager.GetService<IDialogService>();
37
38 partial void OnIsAutoStartEnableChanged(bool value)
16 39 {
17 [ObservableProperty]
18 bool isAutoStartEnable = SystemProfile.AutoStart;
19 [ObservableProperty]
20 string appCacheDirectory = AppPathHelper.AppCache;
21 [ObservableProperty]
22 string appCacheSize = FileHelper.GetDirectorySize(new(AppPathHelper.AppCache)).FileSize();
23 [ObservableProperty]
24 string appDataDirectory = AppPathHelper.AppLocalData;
25 [ObservableProperty]
26 string appDataSize = FileHelper.GetDirectorySize(new(AppPathHelper.AppLocalData)).FileSize();
27 [ObservableProperty]
28 string appLogDirectory = AppPath.LogDictionary;
29 [ObservableProperty]
30 string appLogSize = FileHelper.GetDirectorySize(new(AppPath.LogDictionary)).FileSize();
31 [ObservableProperty]
32 private string currentVersion = Assembly.GetEntryAssembly()?.GetName().Version is Version version ? version.ToString(3) : "无法获取版本信息";
33 public ISettingService SettingService { get; set; } = ServiceManager.GetService<ISettingService>();
34 public IDialogService DialogService { get; set; } = ServiceManager.GetService<IDialogService>();
40 SystemProfile.AutoStart = value;
41 SetAutoStart(value);
42 }
35 43
36 partial void OnIsAutoStartEnableChanged(bool value)
37 {
38 SystemProfile.AutoStart = value;
39 SetAutoStart(value);
40 }
44 partial void OnMinimizeWhenOpenChanged(bool value) => SystemProfile.MinimizeWhenOpen = value;
41 45
42 private static void SetAutoStart(bool enable) => SetAutoStart(enable, Assembly.GetExecutingAssembly().GetName().Name ?? "HaloPixelToolBox");
46 private static void SetAutoStart(bool enable) => SetAutoStart(enable, Assembly.GetExecutingAssembly().GetName().Name ?? "HaloPixelToolBox");
43 47
44 private static void SetAutoStart(bool enable, string appName, string exePath = "")
48 private static void SetAutoStart(bool enable, string appName, string exePath = "")
49 {
50 string runKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run";
51 using var key = Registry.CurrentUser.OpenSubKey(runKey, true);
52 if (enable)
45 53 {
46 string runKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run";
47 using var key = Registry.CurrentUser.OpenSubKey(runKey, true);
48 if (enable)
49 {
50 if (exePath == string.Empty)
51 exePath = Environment.ProcessPath ?? string.Empty;
54 if (exePath == string.Empty)
55 exePath = Environment.ProcessPath ?? string.Empty;
52 56
53 key?.SetValue(appName, $"\"{exePath}\"");
54 }
55 else
57 key?.SetValue(appName, $"\"{exePath}\"");
58 }
59 else
60 {
61 if (key?.GetValue(appName) != null)
56 62 {
57 if (key?.GetValue(appName) != null)
58 {
59 key.DeleteValue(appName);
60 }
63 key.DeleteValue(appName);
61 64 }
62 65 }
66 }
63 67
64 [RelayCommand]
65 static void OpenPath(string originalPath) => Helper.OpenPath(originalPath);
68 [RelayCommand]
69 static void OpenPath(string originalPath) => Helper.OpenPath(originalPath);
66 70
67 [RelayCommand]
68 async Task ClearCache()
71 [RelayCommand]
72 async Task ClearCache()
73 {
74 if (await DialogService.ShowDialog("cleanCacheContentDialog") == ContentDialogResult.Primary)
69 75 {
70 if (await DialogService.ShowDialog("cleanCacheContentDialog") == ContentDialogResult.Primary)
71 {
72 Directory.Delete(AppPathHelper.AppCache, true);
73 AppCacheSize = FileHelper.GetDirectorySize(new(AppPathHelper.AppCache)).FileSize();
74 }
76 Directory.Delete(AppPathHelper.AppCache, true);
77 AppCacheSize = FileHelper.GetDirectorySize(new(AppPathHelper.AppCache)).FileSize();
75 78 }
76 79 }
77 80 }
Modified HaloPixelToolBox/HaloPixelToolBox/Views/SettingPage.xaml +7 -1
@@ -42,6 +42,12 @@
42 42 </controls:SettingsCard.HeaderIcon>
43 43 <ToggleSwitch x:Name="autoStartToggleSwitch" IsOn="{x:Bind ViewModel.IsAutoStartEnable, Mode=TwoWay}"/>
44 44 </controls:SettingsCard>
45 <controls:SettingsCard Header="自动托盘到右下角" Description="启动软件后自动托盘到右下角">
46 <controls:SettingsCard.HeaderIcon>
47 <FontIcon Glyph="&#xE7E8;"/>
48 </controls:SettingsCard.HeaderIcon>
49 <ToggleSwitch x:Name="minimizeWhenOpenToggleSwitch" IsOn="{x:Bind ViewModel.MinimizeWhenOpen, Mode=TwoWay}"/>
50 </controls:SettingsCard>
45 51 <TextBlock Text="路径 &amp; 存储" Style="{StaticResource SettingsSectionHeaderTextBlockStyle}"/>
46 52 <controls:SettingsCard Header="缓存目录" Description="{x:Bind ViewModel.AppCacheDirectory, Mode=OneWay}" IsClickEnabled="True" Command="{x:Bind ViewModel.OpenPathCommand}" CommandParameter="{x:Bind ViewModel.AppCacheDirectory, Mode=OneWay}">
47 53 <controls:SettingsCard.ActionIcon>
@@ -74,7 +80,7 @@
74 80 </controls:SettingsCard>
75 81 <controls:SettingsCard Header="联系我们" HorizontalContentAlignment="Left" ContentAlignment="Vertical">
76 82 <StackPanel Margin="-12,0,0,0" Orientation="Vertical">
77 <HyperlinkButton Content="加入QQ群" NavigateUri="https://qm.qq.com/q/KTw4AZp4CQ"/>
83 <HyperlinkButton Content="加入QQ群" NavigateUri="https://qm.qq.com/q/VLpyYjWOWe"/>
78 84 <HyperlinkButton Content="Github" NavigateUri="https://github.com/XFEstudio"/>
79 85 <HyperlinkButton Content="Bilibili主页" NavigateUri="https://space.bilibili.com/200494622"/>
80 86 </StackPanel>
Modified HaloPixelToolBox/HaloPixelToolBox/Views/TrayMenuPage.xaml +8 -6
@@ -9,11 +9,13 @@
9 9 mc:Ignorable="d">
10 10
11 11 <StackPanel x:Name="panel" HorizontalAlignment="Center" VerticalAlignment="Center">
12 <MenuFlyoutItem Icon="OpenFile" Text="打开主界面" Click="Show_Click"/>
13 <MenuFlyoutItem Text="退出" Click="Exit_Click">
14 <MenuFlyoutItem.Icon>
15 <FontIcon Glyph="&#xE711;"/>
16 </MenuFlyoutItem.Icon>
17 </MenuFlyoutItem>
12 <StackPanel Orientation="Horizontal" Spacing="10" HorizontalAlignment="Center" Padding="20">
13 <Image Source="/Assets/icon.png" HorizontalAlignment="Left" Width="16" Height="16"/>
14 <TextBlock Text="花再音响工具箱" VerticalAlignment="Center"/>
15 </StackPanel>
16 <MenuFlyoutSeparator Margin="10,0"/>
17 <Button Content="打开主界面" Click="Show_Click" HorizontalAlignment="Stretch" Style="{StaticResource SubtleButtonStyle}"/>
18 <MenuFlyoutSeparator Margin="10,0"/>
19 <Button Content="退出" Click="Exit_Click" HorizontalAlignment="Stretch" Style="{StaticResource SubtleButtonStyle}"/>
18 20 </StackPanel>
19 21 </Page>
Modified HaloPixelToolBox/HaloPixelToolBox/Views/TrayMenuPage.xaml.cs +24 -18
@@ -1,8 +1,10 @@
1 using HaloPixelToolBox.Interface.Services;
1 2 using HaloPixelToolBox.Utilities.Helpers;
2 3 using HaloPixelToolBox.Utilities.Helpers.Win32;
3 4 using System.Runtime.InteropServices;
4 5 using System.Windows.Forms;
5 6 using Windows.Graphics;
7 using XFEExtension.NetCore.WinUIHelper.Utilities;
6 8
7 9 namespace HaloPixelToolBox.Views;
8 10
@@ -46,15 +48,15 @@ public sealed partial class TrayMenuPage : Page
46 48 MenuWindow.AppWindow.Move(new PointInt32(Cursor.Position.X + 5, Cursor.Position.Y - h + 10));
47 49 }
48 50
49 private void Show_Click(object sender, RoutedEventArgs e)
51 private void Show_Click(object _, RoutedEventArgs __)
50 52 {
51 App.Tray?.ShowWindow();
53 ServiceManager.GetGlobalService<ITrayIconService>()?.ShowWindow();
52 54 MenuWindow.Close();
53 55 }
54 56
55 private void Exit_Click(object sender, RoutedEventArgs e)
57 private void Exit_Click(object _, RoutedEventArgs __)
56 58 {
57 App.Tray?.ExitApp();
59 ServiceManager.GetGlobalService<ITrayIconService>()?.ExitApp();
58 60 }
59 61
60 62 void InstallMouseHook()
@@ -65,27 +67,31 @@ public sealed partial class TrayMenuPage : Page
65 67
66 68 IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
67 69 {
68 if (nCode >= 0 && wParam == Win32Helper.WM_LBUTTONDOWN)
70 try
69 71 {
70 var hook = Marshal.PtrToStructure<Win32Helper.MSLLHOOKSTRUCT>(lParam);
71 var pt = hook.pt;
72 if (nCode >= 0 && wParam == Win32Helper.WM_LBUTTONDOWN)
73 {
74 var hook = Marshal.PtrToStructure<MSLLHOOKSTRUCT>(lParam);
75 var pt = hook.pt;
72 76
73 var hwnd = WinRT.Interop.WindowNative.GetWindowHandle(MenuWindow);
74 GetWindowRect(hwnd, out RECT rect);
77 var hwnd = WinRT.Interop.WindowNative.GetWindowHandle(MenuWindow);
78 Win32Helper.GetWindowRect(hwnd, out RECT rect);
75 79
76 bool inside =
77 pt.x >= rect.Left && pt.x <= rect.Right &&
78 pt.y >= rect.Top && pt.y <= rect.Bottom;
80 bool inside =
81 pt.x >= rect.Left && pt.x <= rect.Right &&
82 pt.y >= rect.Top && pt.y <= rect.Bottom;
79 83
80 if (!inside)
81 {
82 DispatcherQueue.TryEnqueue(() =>
84 if (!inside)
83 85 {
84 MenuWindow.Close();
85 });
86 DispatcherQueue.TryEnqueue(() =>
87 {
88 MenuWindow.Close();
89 });
90 }
86 91 }
87 92 }
93 catch { }
88 94
89 return CallNextHookEx(_mouseHook, nCode, wParam, lParam);
95 return Win32Helper.CallNextHookEx(_mouseHook, nCode, wParam, lParam);
90 96 }
91 97 }