WPF 最小化到系统托盘

只需要在MainWindow的后台复制下方代码即可

最小化托盘 导一个系统自带System.Drawing

public partial class MainWindow : Window

{

private NotifyIcon trayIcon;

public MainWindow()

{

InitializeComponent();

trayIcon = new NotifyIcon();

trayIcon.Icon = System.Drawing.Icon.ExtractAssociatedIcon(System.Windows.Forms.Application.ExecutablePath);

trayIcon.Visible = true;

trayIcon.ContextMenu = new System.Windows.Forms.ContextMenu(new System.Windows.Forms.MenuItem[]

{

new System.Windows.Forms.MenuItem("恢复", OnRestoreClick),

new System.Windows.Forms.MenuItem("退出", OnExitClick)

});

this.StateChanged += MainWindow_StateChanged;

}

private void MainWindow_StateChanged(object sender, EventArgs e)

{

if (this.WindowState == WindowState.Minimized)

{

this.Hide();

trayIcon.Visible = true;

}

}

private void OnRestoreClick(object sender, EventArgs e)

{

this.Show();

this.WindowState = WindowState.Normal;

trayIcon.Visible = false;

}

// 点击托盘图标的退出操作

private void OnExitClick(object sender, EventArgs e)

{

trayIcon.Visible = false;

System.Windows.Application.Current.Shutdown();

}

// 处理窗口关闭事件

protected override void OnClosed(EventArgs e)

{

base.OnClosed(e);

trayIcon.Dispose();

}

}

相关推荐
武藤一雄10 小时前
深入拆解.NET内存管理:从GC机制到高性能内存优化
windows·microsoft·c#·.net·wpf·.netcore·内存管理
武藤一雄2 天前
WPF/C# 应对消息洪峰与数据抖动的 8 种“抗压”策略
windows·微软·c#·wpf·.netcore·防抖·鲁棒性
武藤一雄3 天前
WPF深度解析Behavior
windows·c#·.net·wpf·.netcore
Maybe_ch3 天前
WPF的STA线程模型、APM与TAP:从线程约束到现代异步
c#·.net·wpf
FuckPatience3 天前
WPF 实现windows文件压缩文件解压过程动画
wpf
会飞的大可4 天前
Spring Cloud Alibaba全景:Nacos、Sentinel、Seata整合实战
sentinel·wpf
baivfhpwxf20234 天前
DataGrid 中增加选择列 功能实现
ui·wpf
czhc11400756634 天前
winform 330 跨线程 异步
wpf·线程·winform
想你依然心痛4 天前
HarmonyOS 5.0教育行业解决方案:基于分布式能力的沉浸式智慧课堂系统
分布式·wpf·harmonyos
Maybe_ch4 天前
深度解析 WPF 线程模型:告别 UI 卡死,掌握 Dispatcher 核心机制
ui·wpf