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

}

}

相关推荐
SamChan9018 小时前
PDF翻译时页眉页脚总在捣乱?跨页重复文本块的检测与过滤实测
人工智能·python·ai·pdf·wpf
samble8 天前
从零搭建灌装监控系统(四):深色主题与样式系统,ResourceDictionary 统一管理
c#·wpf·mvvm·样式·工业控制
_Jonas8 天前
.net framework开发环境正式环境配置文件的切换
.net·wpf
dalong1010 天前
WPF:Modbus 状态监控
wpf·modbus
He BianGu10 天前
【WPF-VisionMaster】机器视觉通用平台V5.0版本发行说明
opencv·c#·wpf·halcon·机器视觉·visionmaster
fogota14 天前
Emoji与Segoe MDL2 Assets的比较
c#·wpf
fogota14 天前
C# WPF 按钮加载字体图标 / 动物头像
c#·.net·wpf
fogota14 天前
C# WPF 按钮加载系统图标 / 动物头像
c#·.net·wpf
军训猫猫头14 天前
C# 封装 MySQL 自动写入与查询工具类(基于 MySqlConnector)
mysql·c#·.net·wpf
gis开发之家15 天前
Spring Boot 4 定时任务与异步线程池:从 @Scheduled 到高并发任务编排(生产级实战)
java·spring boot·后端·wpf·spring boot4