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

HaloPixelToolBox

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

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

XFEstudio/HaloPixelToolBox

添加部分类

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

代码差异

10 个文件 +380 -38
Modified HaloPixelToolBox/HaloPixelToolBox/App.xaml.cs +40 -14
@@ -1,6 +1,9 @@
1 using HaloPixelToolBox.Profiles.CrossVersionProfiles;
1 using HaloPixelToolBox.Interface.Services;
2 using HaloPixelToolBox.Profiles.CrossVersionProfiles;
2 3 using HaloPixelToolBox.Utilities;
3 using XFEExtension.NetCore.FileExtension;
4 using HaloPixelToolBox.Utilities.Helpers;
5 using Microsoft.UI.Dispatching;
6 using System.Runtime.InteropServices;
4 7 using XFEExtension.NetCore.WinUIHelper.Interface.Services;
5 8 using XFEExtension.NetCore.WinUIHelper.Utilities;
6 9 using XFEExtension.NetCore.WinUIHelper.Utilities.Helper;
@@ -13,6 +16,7 @@ namespace HaloPixelToolBox
13 16 /// </summary>
14 17 public partial class App : Application
15 18 {
19 public ITrayIconService TrayIconService { get; } = ServiceManager.GetService<ITrayIconService>();
16 20 /// <summary>
17 21 /// 主页窗口
18 22 /// </summary>
@@ -35,6 +39,37 @@ namespace HaloPixelToolBox
35 39 PageManager.RegisterPage(typeof(MainPage));
36 40 PageManager.RegisterPage(typeof(SettingPage));
37 41 UnhandledException += App_UnhandledException;
42 AppDomain.CurrentDomain.ProcessExit += CurrentDomain_ProcessExit;
43 AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
44 }
45
46 private void CurrentDomain_UnhandledException(object sender, System.UnhandledExceptionEventArgs e)
47 {
48 if (e.ExceptionObject is Exception ex)
49 {
50 Console.WriteLine($"[ERROR]{ex.Message}");
51 Console.WriteLine($"[TRACE]{ex.StackTrace}");
52 }
53 else
54 {
55 Console.WriteLine("[ERROR]发生未处理的异常,但无法获取异常信息");
56 Console.WriteLine(e.ExceptionObject.ToString());
57 }
58 }
59
60 private void CurrentDomain_ProcessExit(object? sender, EventArgs e)
61 {
62 Console.WriteLine("正在退出...");
63 Console.WriteLine("正在保存日志...");
64 var logs = Directory.GetFiles(AppPath.LogDictionary);
65 if (logs.Length > 10)
66 {
67 foreach (var log in logs.OrderByDescending(x => x).Skip(10))
68 {
69 File.Delete(log);
70 }
71 }
72 Console.WriteLine("日志保存成功");
38 73 }
39 74
40 75 private void App_UnhandledException(object sender, Microsoft.UI.Xaml.UnhandledExceptionEventArgs e)
@@ -54,6 +89,7 @@ namespace HaloPixelToolBox
54 89 /// <param name="args">Details about the launch request and process.</param>
55 90 protected override void OnLaunched(LaunchActivatedEventArgs args)
56 91 {
92 TrayIconService.Initilize(DispatcherQueue.GetForCurrentThread());
57 93 MainWindow.Closed += MainWindow_Closed;
58 94 MainWindow.Content = new AppShellPage();
59 95 MainWindow.Activate();
@@ -62,18 +98,8 @@ namespace HaloPixelToolBox
62 98
63 99 private void MainWindow_Closed(object sender, WindowEventArgs args)
64 100 {
65 Console.WriteLine("正在退出...");
66 Console.WriteLine("正在保存日志...");
67 var logs = Directory.GetFiles(AppPath.LogDictionary);
68 if (logs.Length > 10)
69 {
70 foreach (var log in logs.OrderByDescending(x => x).Skip(10))
71 {
72 File.Delete(log);
73 }
74 }
75 Console.WriteLine("日志保存成功");
76 Current.Exit();
101 ShowWindow(WindowHelper.GetHwndForCurrentWindow(), SW_HIDE);
102 args.Handled = true;
77 103 }
78 104 }
79 105 }
Modified HaloPixelToolBox/HaloPixelToolBox/HaloPixelToolBox.csproj +42 -10
@@ -9,7 +9,7 @@
9 9 <RuntimeIdentifiers>win-x86;win-x64;win-arm64</RuntimeIdentifiers>
10 10 <PublishProfile>win-$(Platform).pubxml</PublishProfile>
11 11 <UseWinUI>true</UseWinUI>
12 <Version>1.1.3</Version>
12 <Version>1.2.0</Version>
13 13 <EnableMsixTooling>true</EnableMsixTooling>
14 14 <WindowsPackageType>None</WindowsPackageType>
15 15 <WindowsAppSDKSelfContained>true</WindowsAppSDKSelfContained>
@@ -17,19 +17,39 @@
17 17 </PropertyGroup>
18 18 <ItemGroup>
19 19 <None Remove="Styles\ConverterResources.xaml" />
20 <None Remove="TrayMenuWindow.xaml" />
20 21 <None Remove="Views\MainPage.xaml" />
22 <None Remove="Views\TrayMenuPage.xaml" />
21 23 </ItemGroup>
22 24
23 25 <ItemGroup>
24 <Content Include="Assets\SplashScreen.scale-200.png" />
25 <Content Include="Assets\LockScreenLogo.scale-200.png" />
26 <Content Include="Assets\Square150x150Logo.scale-200.png" />
27 <Content Include="Assets\Square44x44Logo.scale-200.png" />
28 <Content Include="Assets\Square44x44Logo.targetsize-24_altform-unplated.png" />
29 <Content Include="Assets\StoreLogo.png" />
30 <Content Include="Assets\Wide310x150Logo.scale-200.png" />
31 <Content Include="Assets\appicon.ico" />
32 <Content Include="Assets\icon.png" />
26 <Content Include="Assets\SplashScreen.scale-200.png">
27 <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
28 </Content>
29 <Content Include="Assets\LockScreenLogo.scale-200.png">
30 <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
31 </Content>
32 <Content Include="Assets\Square150x150Logo.scale-200.png">
33 <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
34 </Content>
35 <Content Include="Assets\Square44x44Logo.scale-200.png">
36 <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
37 </Content>
38 <Content Include="Assets\Square44x44Logo.targetsize-24_altform-unplated.png">
39 <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
40 </Content>
41 <Content Include="Assets\StoreLogo.png">
42 <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
43 </Content>
44 <Content Include="Assets\Wide310x150Logo.scale-200.png">
45 <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
46 </Content>
47 <Content Include="Assets\appicon.ico">
48 <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
49 </Content>
50 <Content Include="Assets\icon.png">
51 <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
52 </Content>
33 53 </ItemGroup>
34 54
35 55 <ItemGroup>
@@ -52,12 +72,14 @@
52 72 <ProjectCapability Include="Msix" />
53 73 </ItemGroup>
54 74 <ItemGroup>
75 <FrameworkReference Include="Microsoft.WindowsDesktop.App.WindowsForms" />
55 76 <PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.26100.*" />
56 77 <PackageReference Include="Microsoft.WindowsAppSDK" Version="1.8.251106002" />
57 78 <PackageReference Include="CommunityToolkit.Mvvm" Version="8.4.0" />
58 79 <PackageReference Include="CommunityToolkit.WinUI.Controls.SettingsControls" Version="8.2.251219" />
59 80 <PackageReference Include="XFEExtension.NetCore" Version="3.3.*" />
60 81 <PackageReference Include="XFEExtension.NetCore.AutoConfig" Version="2.0.*" />
82 <PackageReference Include="XFEExtension.NetCore.InputSimulator" Version="2.3.0" />
61 83 <PackageReference Include="XFEExtension.NetCore.UpgradeHelper" Version="1.0.1" />
62 84 <PackageReference Include="XFEExtension.NetCore.WinUIHelper" Version="1.2.*" />
63 85 <PackageReference Include="XFEExtension.NetCore.XFEConsole" Version="1.2.1" />
@@ -70,6 +92,16 @@
70 92 <Folder Include="Models\" />
71 93 <Folder Include="Utilities\Selector\" />
72 94 </ItemGroup>
95 <ItemGroup>
96 <Page Update="Views\TrayMenuPage.xaml">
97 <Generator>MSBuild:Compile</Generator>
98 </Page>
99 </ItemGroup>
100 <ItemGroup>
101 <Page Update="TrayMenuWindow.xaml">
102 <Generator>MSBuild:Compile</Generator>
103 </Page>
104 </ItemGroup>
73 105 <ItemGroup>
74 106 <Page Update="Styles\ConverterResources.xaml">
75 107 <Generator>MSBuild:Compile</Generator>
Added HaloPixelToolBox/HaloPixelToolBox/Implements/Services/TrayIconService.cs +80 -0
@@ -0,0 +1,80 @@
1 using HaloPixelToolBox.Interface.Services;
2 using Microsoft.UI.Dispatching;
3 using Microsoft.UI.Windowing;
4 using System.Drawing;
5 using System.Windows.Forms;
6 using XFEExtension.NetCore.WinUIHelper.Implements.Services;
7
8 namespace HaloPixelToolBox.Implements.Services;
9
10 public partial class TrayIconService : GlobalServiceBase, ITrayIconService
11 {
12 private readonly NotifyIcon _notifyIcon;
13 private DispatcherQueue? _dispatcher;
14
15 public TrayIconService()
16 {
17 _notifyIcon = new NotifyIcon
18 {
19 Text = "花再音响工具箱",
20 Icon = new Icon("Assets/appicon.ico"), // 一定要是 .ico
21 Visible = true
22 };
23
24 _notifyIcon.MouseClick += (_, e) =>
25 {
26 if (e.Button == MouseButtons.Right)
27 {
28 ShowTrayMenu();
29 }
30 else
31 {
32 ShowWindow();
33 }
34 };
35
36 _notifyIcon.Visible = true;
37 }
38
39 public void Initilize(DispatcherQueue dispatcherQueue)
40 {
41 _dispatcher = dispatcherQueue;
42 }
43
44 public void ShowWindow()
45 {
46 _dispatcher?.TryEnqueue(() =>
47 {
48 App.MainWindow.Activate();
49 });
50 }
51
52 private void ShowTrayMenu()
53 {
54 _dispatcher?.TryEnqueue(() =>
55 {
56 var menu = new TrayMenuWindow();
57 menu.AppWindow.MoveInZOrderAtTop();
58 menu.AppWindow.SetPresenter(AppWindowPresenterKind.Overlapped);
59 if (menu.AppWindow.Presenter is OverlappedPresenter presenter)
60 {
61 presenter.SetBorderAndTitleBar(true, false);
62 presenter.IsResizable = false;
63 presenter.IsMaximizable = false;
64 presenter.IsMinimizable = false;
65 presenter.IsAlwaysOnTop = true;
66 }
67 var displayArea = DisplayArea.GetFromPoint(new Windows.Graphics.PointInt32(Cursor.Position.X, Cursor.Position.Y), DisplayAreaFallback.Primary);
68 menu.Activate();
69 });
70 }
71
72
73 public void ExitApp()
74 {
75 _notifyIcon.Visible = false;
76 Environment.Exit(0);
77 }
78
79 public void Dispose() => GC.SuppressFinalize(this);
80 }
Added HaloPixelToolBox/HaloPixelToolBox/Interface/Services/ITrayIconService.cs +11 -0
@@ -0,0 +1,11 @@
1 using Microsoft.UI.Dispatching;
2 using XFEExtension.NetCore.WinUIHelper.Interface.Services;
3
4 namespace HaloPixelToolBox.Interface.Services;
5
6 public interface ITrayIconService : IDisposable, IGlobalService
7 {
8 void Initilize(DispatcherQueue dispatcherQueue);
9 void ExitApp();
10 void ShowWindow();
11 }
Modified HaloPixelToolBox/HaloPixelToolBox/MainWindow.xaml.cs +13 -14
@@ -1,21 +1,20 @@
1 1 using Windows.UI.ViewManagement;
2 2 using XFEExtension.NetCore.WinUIHelper.Utilities.Helper;
3 3
4 namespace HaloPixelToolBox
4 namespace HaloPixelToolBox;
5
6 /// <summary>
7 /// 主窗口
8 /// </summary>
9 public sealed partial class MainWindow : Window
5 10 {
6 /// <summary>
7 /// ������
8 /// </summary>
9 public sealed partial class MainWindow : Window
11 public static UISettings UISettings { get; set; } = new UISettings();
12 public MainWindow()
10 13 {
11 public static UISettings UISettings { get; set; } = new UISettings();
12 public MainWindow()
13 {
14 this.InitializeComponent();
15 ExtendsContentIntoTitleBar = true;
16 AppWindow.SetIcon(Path.Combine(AppContext.BaseDirectory, "Assets/icon.png"));
17 AppWindow.TitleBar.PreferredHeightOption = Microsoft.UI.Windowing.TitleBarHeightOption.Tall;
18 UISettings.ColorValuesChanged += (_, _) => DispatcherQueue.TryEnqueue(() => AppThemeHelper.ChangeTheme(AppThemeHelper.Theme));
19 }
14 this.InitializeComponent();
15 ExtendsContentIntoTitleBar = true;
16 AppWindow.SetIcon(Path.Combine(AppContext.BaseDirectory, "Assets/icon.png"));
17 AppWindow.TitleBar.PreferredHeightOption = Microsoft.UI.Windowing.TitleBarHeightOption.Tall;
18 UISettings.ColorValuesChanged += (_, _) => DispatcherQueue.TryEnqueue(() => AppThemeHelper.ChangeTheme(AppThemeHelper.Theme));
20 19 }
21 20 }
Added HaloPixelToolBox/HaloPixelToolBox/TrayMenuWindow.xaml +16 -0
@@ -0,0 +1,16 @@
1 <?xml version="1.0" encoding="utf-8"?>
2 <Window
3 x:Class="HaloPixelToolBox.TrayMenuWindow"
4 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
5 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
6 xmlns:local="using:HaloPixelToolBox"
7 xmlns:views="using:HaloPixelToolBox.Views"
8 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
9 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
10 mc:Ignorable="d"
11 Title="TrayMenuWindow">
12
13 <Window.SystemBackdrop>
14 <MicaBackdrop />
15 </Window.SystemBackdrop>
16 </Window>
Added HaloPixelToolBox/HaloPixelToolBox/TrayMenuWindow.xaml.cs +13 -0
@@ -0,0 +1,13 @@
1 namespace HaloPixelToolBox;
2
3 /// <summary>
4 /// An empty window that can be used on its own or navigated to within a Frame.
5 /// </summary>
6 public sealed partial class TrayMenuWindow : Window
7 {
8 public TrayMenuWindow()
9 {
10 InitializeComponent();
11 Content = new TrayMenuPage(this);
12 }
13 }
Added HaloPixelToolBox/HaloPixelToolBox/Utilities/Helpers/Win32Helper.cs +55 -0
@@ -0,0 +1,55 @@
1 using HaloPixelToolBox.Utilities.Helpers.Win32;
2 using System.Runtime.InteropServices;
3
4 namespace HaloPixelToolBox.Utilities.Helpers;
5
6 public static partial class Win32Helper
7 {
8
9 public const int SW_HIDE = 0;
10 public const int SW_SHOW = 5;
11
12 public const int WH_MOUSE_LL = 14;
13 public const int WM_LBUTTONDOWN = 0x0201;
14
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 [LibraryImport("user32.dll")]
39 [return: MarshalAs(UnmanagedType.Bool)]
40 public static partial bool ShowWindow(IntPtr hWnd, int nCmdShow);
41
42 [LibraryImport("user32.dll")]
43 public static partial IntPtr SetWindowsHookEx(int idHook, LowLevelMouseProc lpfn, IntPtr hMod, uint dwThreadId);
44
45 [LibraryImport("user32.dll")]
46 [return: MarshalAs(UnmanagedType.Bool)]
47 public static partial bool UnhookWindowsHookEx(IntPtr hhk);
48
49 [LibraryImport("user32.dll")]
50 private static partial IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
51
52 [LibraryImport("user32.dll")]
53 [return: MarshalAs(UnmanagedType.Bool)]
54 public static partial bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
55 }
Added HaloPixelToolBox/HaloPixelToolBox/Views/TrayMenuPage.xaml +19 -0
@@ -0,0 +1,19 @@
1 <?xml version="1.0" encoding="utf-8"?>
2 <Page
3 x:Class="HaloPixelToolBox.Views.TrayMenuPage"
4 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
5 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
6 xmlns:local="using:HaloPixelToolBox.Views"
7 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
8 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
9 mc:Ignorable="d">
10
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>
18 </StackPanel>
19 </Page>
Added HaloPixelToolBox/HaloPixelToolBox/Views/TrayMenuPage.xaml.cs +91 -0
@@ -0,0 +1,91 @@
1 using HaloPixelToolBox.Utilities.Helpers;
2 using HaloPixelToolBox.Utilities.Helpers.Win32;
3 using System.Runtime.InteropServices;
4 using System.Windows.Forms;
5 using Windows.Graphics;
6
7 namespace HaloPixelToolBox.Views;
8
9 /// <summary>
10 /// An empty page that can be used on its own or navigated to within a Frame.
11 /// </summary>
12 public sealed partial class TrayMenuPage : Page
13 {
14 IntPtr _mouseHook = new();
15 LowLevelMouseProc? _proc;
16 public Window MenuWindow { get; set; }
17
18 public TrayMenuPage(Window window)
19 {
20 MenuWindow = window;
21 InitializeComponent();
22 this.Loaded += (_, __) =>
23 {
24 DispatcherQueue.TryEnqueue(() =>
25 {
26 ResizeToContent();
27 });
28 };
29 InstallMouseHook();
30 }
31
32 void ResizeToContent()
33 {
34 double width = panel.ActualWidth;
35 double height = panel.ActualHeight;
36
37 if (width <= 0 || height <= 0)
38 return;
39
40 double scale = Content.XamlRoot.RasterizationScale;
41
42 int w = (int)Math.Ceiling(width * scale);
43 int h = (int)Math.Ceiling(height * scale);
44
45 MenuWindow.AppWindow.Resize(new SizeInt32(w, h));
46 MenuWindow.AppWindow.Move(new PointInt32(Cursor.Position.X + 5, Cursor.Position.Y - h + 10));
47 }
48
49 private void Show_Click(object sender, RoutedEventArgs e)
50 {
51 App.Tray?.ShowWindow();
52 MenuWindow.Close();
53 }
54
55 private void Exit_Click(object sender, RoutedEventArgs e)
56 {
57 App.Tray?.ExitApp();
58 }
59
60 void InstallMouseHook()
61 {
62 _proc = HookCallback;
63 _mouseHook = Win32Helper.SetWindowsHookEx(Win32Helper.WH_MOUSE_LL, _proc, IntPtr.Zero, 0);
64 }
65
66 IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
67 {
68 if (nCode >= 0 && wParam == Win32Helper.WM_LBUTTONDOWN)
69 {
70 var hook = Marshal.PtrToStructure<Win32Helper.MSLLHOOKSTRUCT>(lParam);
71 var pt = hook.pt;
72
73 var hwnd = WinRT.Interop.WindowNative.GetWindowHandle(MenuWindow);
74 GetWindowRect(hwnd, out RECT rect);
75
76 bool inside =
77 pt.x >= rect.Left && pt.x <= rect.Right &&
78 pt.y >= rect.Top && pt.y <= rect.Bottom;
79
80 if (!inside)
81 {
82 DispatcherQueue.TryEnqueue(() =>
83 {
84 MenuWindow.Close();
85 });
86 }
87 }
88
89 return CallNextHookEx(_mouseHook, nCode, wParam, lParam);
90 }
91 }