WPF 程序最小化到托盘

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;
}
}



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WPF最小化托盘(System Tray)的实现可以通过以下步骤来完成: 1. 首先,在XAML文件中创建一个托盘图标。可以使用Rectangle、Ellipse或Image等控件来作为图标,然后设置图标的宽度、高度和颜色等属性。 ```xml <Rectangle Width="16" Height="16" Fill="Red" /> ``` 2. 在MainWindow.xaml.cs文件中,首先添加以下命名空间: ```csharp using System.Windows.Forms; ``` 3. 在MainWindow.xaml.cs文件中,创建一个NotifyIcon对象,并在窗口加载事件中进行初始化,包括设置托盘图标和右键菜单等属性。 ```csharp private NotifyIcon notifyIcon; private void MainWindow_Loaded(object sender, RoutedEventArgs e) { notifyIcon = new NotifyIcon(); notifyIcon.Icon = new System.Drawing.Icon("Icon.ico"); notifyIcon.Visible = true; notifyIcon.Text = "My WPF Application"; ContextMenu menu = new ContextMenu(); // 创建右键菜单 MenuItem showMenuItem = new MenuItem(); showMenuItem.Text = "显示"; showMenuItem.Click += new EventHandler(ShowMenuItem_Click); menu.MenuItems.Add(showMenuItem); MenuItem exitMenuItem = new MenuItem(); exitMenuItem.Text = "退出"; exitMenuItem.Click += new EventHandler(ExitMenuItem_Click); menu.MenuItems.Add(exitMenuItem); notifyIcon.ContextMenu = menu; } ``` 4. 创建点击事件处理方法来处理托盘图标的左键双击事件,以及右键菜单中的"显示"和"退出"事件。 ```csharp private void TrayIcon_DoubleClick(object sender, EventArgs e) { this.Show(); // 双击托盘图标时显示主窗口 this.WindowState = WindowState.Normal; } private void ShowMenuItem_Click(object sender, EventArgs e) { this.Show(); // 右键菜单中点击"显示"时显示主窗口 this.WindowState = WindowState.Normal; } private void ExitMenuItem_Click(object sender, EventArgs e) { this.Close(); // 右键菜单中点击"退出"时关闭应用程序 } ``` 5. 最后,在MainWindow.xaml文件中的Window标签中添加Loaded事件调用MainWindow_Loaded方法。 ```xml <Window x:Class="Demo.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="WPF最小化托盘Demo" Height="450" Width="800" Loaded="MainWindow_Loaded"> <!-- 主窗口内容 --> </Window> ``` 通过以上步骤,我们可以实现一个简单的WPF最小化托盘Demo,点击托盘图标可以显示主窗口,右键菜单中可以选择显示或退出应用程序

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值