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 小时前
C#队列深度剖析:解锁高效编程的FIFO密码
visualstudio·c#·wpf·多线程·winfrom·队列·queue
lhrimperial7 小时前
微服务架构深度解析-微服务理论基础(一)
微服务·架构·wpf
艾斯比的日常9 小时前
XXL-Job 核心原理深度解析
wpf
gAlAxy...1 天前
MyBatis 缓存深度解析(一级 / 二级缓存原理 + 实战 + 避坑)
wpf
泉飒1 天前
WinForm与WPF的异同点
wpf·winform
fireworkseasycold2 天前
wpf 基于 JSON 的扩展配置 (Extended Config)” 功能
oracle·json·wpf
脩衜者2 天前
极其灵活且敏捷的WPF组态控件ConPipe 2026
前端·物联网·ui·wpf
张人玉2 天前
西门子 S7 PLC 通信 WPF 应用分析笔记
笔记·c#·wpf·plc
张人玉2 天前
整合 Sugar ORM 连接 SQLite 数据库到 WPF 折线图项目
数据库·sqlite·c#·wpf
bugcome_com3 天前
WPF 中控件样式定义的三种方式详解
wpf