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();

}

}

相关推荐
聆风吟º1 小时前
CANN hccl 深度解析:异构计算集群通信库的跨节点通信与资源管控实现逻辑
人工智能·wpf·transformer·cann
无心水9 小时前
分布式定时任务与SELECT FOR UPDATE:从致命陷阱到优雅解决方案(实战案例+架构演进)
服务器·人工智能·分布式·后端·spring·架构·wpf
LZL_SQ11 小时前
HCCL测试框架中AllReduce边界条件测试设计深度剖析
wpf·cann
User_芊芊君子1 天前
【分布式训练】CANN SHMEM跨设备内存通信库:构建高效多机多卡训练的关键组件
分布式·深度学习·神经网络·wpf
就是有点傻2 天前
WPF按钮走马灯效果
wpf
zuozewei2 天前
虚拟电厂聚合商平台安全技术体系深度解读
安全·wpf
极客智造2 天前
WPF 自定义控件:AutoGrid 实现灵活自动布局的网格控件
wpf
极客智造2 天前
WPF Grid 布局高效扩展:GridHelpers 附加属性工具类全解析
wpf
张人玉2 天前
WPF 多语言实现完整笔记(.NET 4.7.2)
笔记·.net·wpf·多语言实现·多语言适配
暖馒2 天前
深度剖析串口通讯(232/485)
开发语言·c#·wpf·智能硬件