新增托盘图标

This commit is contained in:
2026-03-01 10:42:42 +08:00
parent 0ba966cef2
commit e03e1b9766
26 changed files with 582 additions and 72 deletions

View File

@@ -120,6 +120,27 @@
<RowDefinition Height="*" />
<RowDefinition Height="24" />
</Grid.RowDefinitions>
<ms:TrayIcon
x:Name="MyTray"
ClickCommand="{Binding ShowWindowCommand}"
DoubleClickCommand="{Binding ShowWindowCommand}"
IconPath="Resources/Images/authentication.ico"
Tip="我的WPF程序托盘">
<!-- 原生的 WPF 右键菜单 -->
<ms:TrayIcon.ContextMenu>
<ContextMenu>
<!-- 支持普通的 Click 事件 -->
<MenuItem
Click="MenuShow_Click"
FontWeight="Bold"
Header="显示主界面" />
<Separator />
<MenuItem Click="MenuExit_Click" Header="完全退出" />
</ContextMenu>
</ms:TrayIcon.ContextMenu>
</ms:TrayIcon>
<!-- 工具栏 -->
<ToolBarTray Margin="4,4,4,0">
<ToolBar Foreground="{DynamicResource PrimaryFocusedBrush}" Header="工具栏">

View File

@@ -1,11 +1,9 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
@@ -82,6 +80,7 @@ public partial class MainWindow
#region
public ICommand ShowSelectedItemsCommand { get; set; }
public ICommand ToggleDetailsCommand { get; set; }
public ICommand ShowWindowCommand { get; set; }
[RelayCommand]
private void AddArea()
@@ -227,6 +226,7 @@ public partial class MainWindow
{
ShowSelectedItemsCommand = new RelayCommand(ShowSelectedItems);
ToggleDetailsCommand = new RelayCommand<PersonItem>(ToggleDetails);
ShowWindowCommand = new RelayCommand(ShowWindow);
}
#endregion
@@ -456,12 +456,38 @@ public partial class MainWindow
case 2:
MyTutorial.ShowStep(StartBtn, "删除功能", "谨慎使用,这会清除数据。", "2 / 2");
break;
default:
MyTutorial.Close();
break;
}
}
private void ShowWindow()
{
this.Show();
this.WindowState = WindowState.Normal;
this.Activate(); // 激活窗口使其置顶
}
// --- 2. 右键菜单的 Click 事件 (Code-behind 风格) ---
private void MenuShow_Click(object sender, RoutedEventArgs e)
{
ShowWindow();
}
private void MenuExit_Click(object sender, RoutedEventArgs e)
{
// 真正退出程序
Application.Current.Shutdown();
}
// --- 3. 重写关闭事件实现“点X最小化到托盘” ---
protected override void OnClosing(CancelEventArgs e)
{
// 阻止窗口真正销毁
e.Cancel = true;
// 隐藏窗口(此时托盘图标依然在运行)
this.Hide();
}
}
#region

View File

@@ -34,5 +34,10 @@
<ItemGroup>
<Reference Include="System.Management" />
</ItemGroup>
<ItemGroup>
<None Update="Resources\Images\authentication.ico">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB