WPF 程序最小化到托盘

文章介绍了如何在WPF程序中使用WinForms的NotifyIcon控件将程序窗口最小化到系统托盘。主要步骤包括添加WinForms引用,创建NotifyIcon对象,设置菜单项以及处理鼠标双击和菜单项点击事件,确保程序窗口能够在托盘图标上显示和关闭。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

WPF 程序最小化到托盘

一.使用的是WinForms的NotifyIcon控件
  1. 添加WinForm引用

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Qqm4lvzY-1677652141280)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20230301141302314.png)]

二.使用NotifyIcon控件
  1. 添加引用

    using WinForms = System.Windows.Forms;
    
  2. 全局引用

    private WinForms.NotifyIcon MyNotifyIcon;
    private WinForms.ContextMenu NotifyMenu;
    private WinForms.MenuItem ItemWindow;
    private WinForms.MenuItem ItemClose;
    
  3. 在软件开始启动NotifyIcon

    NotifyMenu = new WinForms.ContextMenu();
    			//可自定义Item,这边只做了主界面和关闭两个选项
                ItemWindow = new WinForms.MenuItem() { Text = "主界面" };
                ItemClose = new WinForms.MenuItem() { Text = "关闭" };
                WinForms.MenuItem[] menuItems = new WinForms.MenuItem[] { ItemWindow, ItemClose };
                NotifyMenu.MenuItems.AddRange(menuItems);
    
                ItemWindow.Click += _openWindow_Click;
                ItemClose.Click += _closeApp_Click;
    
                MyNotifyIcon = new WinForms.NotifyIcon(new System.ComponentModel.Container());
                //注意这里要写对图片的路径地址
                MyNotifyIcon.Icon = new System.Drawing.Icon(@"../../logo.ico");
                MyNotifyIcon.Text = "托盘测试程序";
                MyNotifyIcon.Visible = true;
                MyNotifyIcon.MouseDoubleClick += _notifyIcon_MouseDoubleClick;
                MyNotifyIcon.ContextMenu = NotifyMenu;
    
    • 响应回调代码

      private void _notifyIcon_MouseDoubleClick(object sender, WinForms.MouseEventArgs e)
              {
                  this.Show();
              }
      
              private void _openWindow_Click(object sender, EventArgs e)
              {
                  this.Show();
              }
      
              private void _closeApp_Click(object sender, EventArgs e)
              {
                  closeApp();
              }
      
              private void closeApp()
              {
                  IsCloseApp = true;
                  MyNotifyIcon.Dispose();
      
                  this.Close();
                  System.Windows.Application current = System.Windows.Application.Current;
                  if (current != null) current.Shutdown();
              }
      
三.监控关闭界面
  1. 界面的关闭回调

    this.Closed += Window_Closed;
    this.Closing += Window_Closing;
    
  2. 回调代码

    private void Window_Closed(object sender, EventArgs e)
            {
                if (!IsCloseApp)
                {
                    MyNotifyIcon.Dispose();
                }
            }
    
            private void Window_Closing(object sender, CancelEventArgs e)
            {
                if (!IsCloseApp)
                {
                    this.Hide();
                    e.Cancel = true;
                }
            }
    

{
this.Hide();
e.Cancel = true;
}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值