当前位置: 首页 > article >正文

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

 }


http://www.kler.cn/a/450044.html

相关文章:

  • Vue3入门(7)
  • SQL语句整理五-StarRocks
  • 后端项目java中字符串、集合、日期时间常用方法
  • Strip Map和Wafer Map的一些小科普
  • Android修行手册 - 移动端几种常用动画方案对比
  • LLMs之PDF:MinerU(将PDF文件转换成Markdown和JSON格式)的简介、安装和使用方法、案例应用之详细攻略
  • Mac iOS、Android、Flutter、React Native开发环境配置
  • 【专题】2024抖音电商母婴行业分析报告汇总PDF洞察(附原数据表)
  • 【ArcGIS Pro】实现一下完美的坐标点标注
  • “年轻科技旗舰”爱玛A7 Plus正式发布,全国售价4999元
  • 【人工智能】探索当下热门视频生成模型
  • Elasticsearch:确保业务规则与语义搜索无缝协作
  • 项目开发实践——基于SpringBoot+Vue3实现的在线考试系统(二)
  • MySQL数据库——复制表数据与结构
  • Zookeeper基本命令解析
  • 设计模式-访问者设计模式
  • linux Sudo权限
  • 《探秘 Qt Creator Manual 4.11.1》
  • 基于Java的秦皇岛旅游景点管理系统
  • 【WRF教程第3.5期】预处理系统 WPS 详解:以4.5版本为例