Skip to content

Commit 429c438

Browse files
author
panwenbo
committed
One.Toolbox
新增最小化到托盘功能;
1 parent 8d0e56b commit 429c438

File tree

9 files changed

+102
-43
lines changed

9 files changed

+102
-43
lines changed

One.Toolbox/App.xaml.cs

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -100,26 +100,6 @@ private void InitDataColelection()
100100
typeof(Analytics), typeof(Crashes));
101101
}
102102

103-
// App Host
104-
105-
// Page resolver service
106-
//services.AddSingleton<IPageService, PageService>();
107-
108-
// Theme manipulation
109-
//services.AddSingleton<IThemeService, ThemeService>();
110-
111-
// TaskBar manipulation
112-
//services.AddSingleton<ITaskBarService, TaskBarService>();
113-
//services.AddSingleton<IContentDialogService, ContentDialogService>();
114-
// Service containing navigation, same as INavigationWindow... but without window
115-
//services.AddSingleton<INavigationService, NavigationService>();
116-
117-
//services.AddSingleton<ISnackbarService, SnackbarService>();
118-
119-
// Main window with navigation
120-
121-
// Configuration
122-
123103
#region Exception
124104

125105
/// <summary> Occurs when an exception is thrown by an application but not handled. </summary>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using System.Windows.Interop;
7+
8+
using Windows.Win32;
9+
10+
namespace One.Toolbox.Helpers
11+
{
12+
public class WindowHelper
13+
{
14+
/// <summary> 让窗口激活作为前台最上层窗口 </summary>
15+
/// <param name="window"> </param>
16+
public static unsafe void SetWindowToForeground(Window window)
17+
{
18+
// [WPF 让窗口激活作为前台最上层窗口的方法 - lindexi - 博客园](https://www.cnblogs.com/lindexi/p/12749671.html)
19+
var interopHelper = new WindowInteropHelper(window);
20+
21+
uint* aa = (uint*)IntPtr.Zero;
22+
var thisWindowThreadId = PInvoke.GetWindowThreadProcessId(new Windows.Win32.Foundation.HWND(interopHelper.Handle), aa);
23+
var currentForegroundWindow = PInvoke.GetForegroundWindow();
24+
var currentForegroundWindowThreadId = PInvoke.GetWindowThreadProcessId(currentForegroundWindow, aa);
25+
26+
// [c# - Bring a window to the front in WPF - Stack Overflow](https://stackoverflow.com/questions/257587/bring-a-window-to-the-front-in-wpf ) [SetForegroundWindow的正确用法 - 子坞 - 博客园](https://www.cnblogs.com/ziwuge/archive/2012/01/06/2315342.html )
27+
/*
28+
1.得到窗口句柄FindWindow
29+
2.切换键盘输入焦点AttachThreadInput
30+
3.显示窗口ShowWindow(有些窗口被最小化/隐藏了)
31+
4.更改窗口的Z Order,SetWindowPos使之最上,为了不影响后续窗口的Z Order,改完之后,再还原
32+
5.最后SetForegroundWindow
33+
*/
34+
35+
PInvoke.AttachThreadInput(currentForegroundWindowThreadId, thisWindowThreadId, true);
36+
37+
window.Show();
38+
window.Activate();
39+
// 去掉和其他线程的输入链接
40+
PInvoke.AttachThreadInput(currentForegroundWindowThreadId, thisWindowThreadId, false);
41+
42+
// 用于踢掉其他的在上层的窗口
43+
if (window.Topmost != true)
44+
{
45+
window.Topmost = true;
46+
window.Topmost = false;
47+
}
48+
}
49+
}
50+
}

One.Toolbox/NativeMethods.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
SetLocalTime
22
SystemParametersInfo
33
CreateWindowExW
4-
PostMessage
4+
PostMessage
5+
6+
GetWindowThreadProcessId
7+
GetForegroundWindow
8+
AttachThreadInput

One.Toolbox/One.Toolbox.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<SelfContained>false</SelfContained>
1010
<UseWPF>true</UseWPF>
1111
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
12-
<Version>2.7.0</Version>
12+
<Version>2.8.0</Version>
1313
<ImplicitUsings>enable</ImplicitUsings>
1414
<SatelliteResourceLanguages>zh-Hans;en-us</SatelliteResourceLanguages>
1515
<ApplicationIcon>Icon.ico</ApplicationIcon>

One.Toolbox/Resources/Languages/en_US.xaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,4 +209,6 @@
209209
<system:String x:Key="Type">Type</system:String>
210210
<system:String x:Key="ShowOnStart">Show On Start</system:String>
211211
<system:String x:Key="ShortTimeInfo">Short Time Info</system:String>
212+
<system:String x:Key="OpenMainWindow">OpenMainWindow</system:String>
213+
<system:String x:Key="ShutdownApp">ShutdownApp</system:String>
212214
</ResourceDictionary>

One.Toolbox/Resources/Languages/zh_CN.xaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,4 +213,7 @@
213213
<system:String x:Key="ShowOnStart">启动时显示</system:String>
214214

215215
<system:String x:Key="ShortTimeInfo">显示短时间</system:String>
216+
<system:String x:Key="OpenMainWindow">打开面板</system:String>
217+
<system:String x:Key="ShutdownApp">关闭程序</system:String>
218+
216219
</ResourceDictionary>

One.Toolbox/ViewModels/MainWindow/MainWindowVM.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
using One.Toolbox.ViewModels.Base;
33
using One.Toolbox.Views.Dashboard;
44
using One.Toolbox.Views.DataProcess;
5-
using One.Toolbox.Views.NetSpeed;
65
using One.Toolbox.Views.Network;
76
using One.Toolbox.Views.Serialport;
87
using One.Toolbox.Views.Settings;
98

109
using System.Collections.ObjectModel;
1110

11+
using WindowHelper = One.Toolbox.Helpers.WindowHelper;
12+
1213
namespace One.Toolbox.ViewModels.MainWindow;
1314

1415
public partial class MainWindowVM : BaseVM
@@ -133,6 +134,22 @@ public MainWindowVM()
133134
CurrentMenuItem = NavigationItems.First();
134135
}
135136

137+
[RelayCommand]
138+
private static void PushMainWindow2Top()
139+
{
140+
if (Application.Current.MainWindow != null && Application.Current.MainWindow.Visibility != Visibility.Visible)
141+
{
142+
Application.Current.MainWindow.Show();
143+
WindowHelper.SetWindowToForeground(Application.Current.MainWindow);
144+
}
145+
}
146+
147+
[RelayCommand]
148+
private static void ExitApp()
149+
{
150+
Application.Current.Shutdown();
151+
}
152+
136153
#region 框架逻辑
137154

138155
partial void OnCurrentMenuItemChanged(MainMenuItemVM? oldValue, MainMenuItemVM newValue)

One.Toolbox/Views/MainWindow.xaml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<hc:Window x:Class="One.Toolbox.Views.MainWindow"
22
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
33
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:behaviors="http://schemas.microsoft.com/xaml/behaviors"
45
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
56
xmlns:hc="https://handyorg.github.io/handycontrol"
67
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
@@ -110,7 +111,19 @@
110111
<!-- 733 548 -->
111112
</Border>
112113

113-
<!--<hc:NotifyIcon Click="NotifyIcon_Click" Text="HandyControl" />-->
114+
<hc:NotifyIcon Text="One.Toolbox">
115+
<behaviors:Interaction.Triggers>
116+
<behaviors:EventTrigger EventName="Click">
117+
<behaviors:InvokeCommandAction Command="{Binding PushMainWindow2TopCommand}" />
118+
</behaviors:EventTrigger>
119+
</behaviors:Interaction.Triggers>
120+
<hc:NotifyIcon.ContextMenu >
121+
<ContextMenu Width="150" >
122+
<MenuItem Command="{Binding PushMainWindow2TopCommand}" Header="{DynamicResource OpenMainWindow}" />
123+
<MenuItem Command="{Binding ExitAppCommand}" Header="{DynamicResource ShutdownApp}" />
124+
</ContextMenu>
125+
</hc:NotifyIcon.ContextMenu>
126+
</hc:NotifyIcon>
114127
</Grid>
115128

116129
</hc:Window>

One.Toolbox/Views/MainWindow.xaml.cs

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using One.Toolbox.ViewModels.MainWindow;
88
using One.Toolbox.Views.Settings;
99

10+
using System.ComponentModel;
1011
using System.Windows.Controls;
1112
using System.Windows.Forms;
1213
using System.Windows.Media;
@@ -25,6 +26,14 @@ public MainWindow()
2526
NonClientAreaContent = new NonClientAreaContent();
2627

2728
InitializeComponent();
29+
30+
Closing += MainWindow_Closing; // 注册Closing事件处理程序
31+
}
32+
33+
private void MainWindow_Closing(object? sender, CancelEventArgs e)
34+
{
35+
e.Cancel = true; // 取消默认的关闭行为
36+
this.Hide(); // 隐藏窗口而不是关闭
2837
}
2938

3039
private void ResizeAndRelocate()
@@ -96,24 +105,5 @@ void OnAnimationCompleted(object _, EventArgs args)
96105
ButtonLeft.Show();
97106
}
98107
}
99-
100-
protected override void OnStateChanged(EventArgs e)
101-
{
102-
base.OnStateChanged(e);
103-
//if (WindowState == WindowState.Minimized)
104-
//{
105-
// ShowInTaskbar = false;
106-
// this.Hide();
107-
//}
108-
}
109-
110-
private void NotifyIcon_Click(object sender, RoutedEventArgs e)
111-
{
112-
//ShowInTaskbar = true;
113-
//this.Show();
114-
//WindowState = WindowState.Normal;
115-
116-
// this.Visibility = Visibility.Visible;
117-
}
118108
}
119109
}

0 commit comments

Comments
 (0)