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

}

}

相关推荐
勘察加熊人6 小时前
wpf+c#路径迷宫鼠标绘制
开发语言·c#·wpf
OneByOneDotNet6 小时前
WPF 教程:给 TreeView 添加 SelectedItem 双向绑定支持(MVVM-Friendly)
wpf
qq_3404740221 小时前
5.02 WPF的 Combox、ListBox,slider、ProgressBar使用
wpf
fkdw1 天前
.net farmework 4.8 类库中添加 wpf 窗体
.net·wpf
八股文领域大手子1 天前
Spring多数据源环境下的事务与数据源切换
java·数据库·后端·mysql·spring·wpf
勘察加熊人1 天前
c#使用wpf实现helloworld和login登录
开发语言·c#·wpf
她说彩礼65万1 天前
WPF 自定义路由事件
wpf
qq_340474021 天前
5.2.1 WPF 通过ItemControl自己做柱状图
java·开发语言·wpf
蓝点lilac1 天前
C# 窗口过程消息处理 WndProc
wpf·winform·winapi
她说彩礼65万2 天前
WPF 依赖项属性
wpf