C#实现WinForm窗口最小化到系统托盘

简介:
 

C#编写最小化时隐藏为任务栏图标的 Window appllication.  

1.设置WinForm窗体属性showinTask=false

2.加notifyicon控件notifyIcon1,为控件notifyIcon1的属性Icon添加一个icon图标。

3.添加窗体最小化事件(首先需要添加事件引用):

this.SizeChanged += new System.EventHandler(this.Form1_SizeChanged);

//上面一行是主窗体InitializeComponent()方法中需要添加的引用

private void Form1_SizeChanged(object sender, EventArgs e)
{
    if(this.WindowState == FormWindowState.Minimized)
    {
       this.Hide();
       this.notifyIcon1.Visible=true;
    }
}

4.添加点击图标事件(首先需要添加事件引用):

private void notifyIcon1_Click(object sender, EventArgs e)
{
    this.Visible = true;
    this.WindowState = FormWindowState.Normal;
    this.notifyIcon1.Visible = false;
}


5.可以给notifyIcon添加右键菜单:

主窗体中拖入一个ContextMenu控件NicontextMenu,点中控件,在上下文菜单中添加菜单,notifyIcon1的ContextMenu行为中选中NicontextMenu 作为上下文菜单。

this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);
this.NicontextMenu = new System.Windows.Forms.ContextMenu();
this.menuItem_Hide = new System.Windows.Forms.MenuItem();
this.menuItem_Show = new System.Windows.Forms.MenuItem();
this.menuItem_Aubot = new System.Windows.Forms.MenuItem();
this.menuItem_Exit = new System.Windows.Forms.MenuItem();  


this.notifyIcon1.ContextMenu = this.NicontextMenu;
this.notifyIcon1.Icon = ((System.Drawing.Icon)(resources.GetObject( "NotifyIcon.Icon ")));
this.notifyIcon1.Text = " ";
this.notifyIcon1.Visible = true;
this.notifyIcon1.DoubleClick += new System.EventHandler(this.notifyIcon1_DoubleClick);
this.notifyIcon1.Click += new System.EventHandler(this.notifyIcon1_Click);


this.NicontextMenu.MenuItems.AddRange(

    new System.Windows.Forms.MenuItem[]
    {
          this.menuItem_Hide,
          this.menuItem_Show,
          this.menuItem_Aubot,
          this.menuItem_Exit
    }
);

//    
//       menuItem_Hide
//    
this.menuItem_Hide.Index = 0;
this.menuItem_Hide.Text = "隐藏 ";
this.menuItem_Hide.Click += new System.EventHandler(this.menuItem_Hide_Click);
//    
//       menuItem_Show
//    
this.menuItem_Show.Index = 1;
this.menuItem_Show.Text = "显示 ";
this.menuItem_Show.Click += new System.EventHandler(this.menuItem_Show_Click);
//    
//       menuItem_Aubot
//    
this.menuItem_Aubot.Index = 2;
this.menuItem_Aubot.Text = "关于 ";
this.menuItem_Aubot.Click += new System.EventHandler(this.menuItem_Aubot_Click);
//    
//       menuItem_Exit
//    
this.menuItem_Exit.Index = 3;
this.menuItem_Exit.Text = "退出 ";
this.menuItem_Exit.Click += new System.EventHandler(this.menuItem_Exit_Click);

protected  override  void  OnClosing(CancelEventArgs e)
{
   this.ShowInTaskbar = false;
   this.WindowState = FormWindowState.Minimized;
   e.Cancel = true;    
}
protected  override  void  OnClosing(CancelEventArgs e)
{
   //this.ShowInTaskbar = false;
   this.WindowState = FormWindowState.Minimized;
   e.Cancel = true;    
}

private  void  CloseCtiServer()
{
   timer.Enabled = false;
   DJ160API.DisableCard();
   this.NotifyIcon.Visible = false;
   this.Close();
   this.Dispose();
   Application.Exit();
}

private  void  HideCtiServer()
{
   this.Hide();
}

private  void  ShowCtiServer()
{
   this.Show();
   this.WindowState = FormWindowState.Normal;
   this.Activate();

}
private  void  CtiManiForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
    this.CloseCtiServer();
}

private  void  menuItem_Show_Click(object sender, System.EventArgs e)
{
    this.ShowCtiServer();
}

private  void  menuItem_Aubot_Click(object sender, System.EventArgs e)
{

}

private  void  menuItem_Exit_Click(object sender, System.EventArgs e)
{
    this.CloseCtiServer();
}

private  void  menuItem_Hide_Click(object sender, System.EventArgs e)
{
    this.HideCtiServer();
}

private  void  CtiManiForm_SizeChanged(object sender, System.EventArgs e)
{
   if(this.WindowState == FormWindowState.Minimized)
   {
      this.HideCtiServer();
   }
}

private  void  notifyIcon1_DoubleClick(object sender,System.EventArgs e)
{                                                                
    this.ShowCtiServer();
}

目录
相关文章
|
5月前
|
供应链 JavaScript 测试技术
硬件产品研发管理工具实战指南
本文深入剖析硬件研发中的三大核心挑战:需求传递失真、BOM管理混乱与测试验证低效,并结合学术研究与实战案例,提出可落地的解决方案框架。通过“需求穿透管理”、“BOM智能管控”和“测试自动化闭环”等方法,帮助企业提升研发效率与产品质量。同时推荐多款实用工具,并提供三步启动升级路径,助力硬件团队实现管理升级,降低项目风险,提升交付质量。
|
缓存 数据安全/隐私保护 JavaScript
【HarmonyOS 5】鸿蒙页面和组件生命周期函数
【HarmonyOS 5】鸿蒙页面和组件生命周期函数
312 0
|
11月前
|
存储 缓存 运维
“网”罗天下,一键搞定:netsh命令的花式玩法与超实用攻略
`netsh`是Windows系统中强大的网络配置和管理工具,支持本地或远程修改网络设置。常用功能包括:显示和配置网络接口、无线网络管理、防火墙规则设置、网络配置备份与还原、远程管理等。通过`netsh`命令,用户可以轻松管理IP地址、启用/禁用网络接口、添加或删除无线网络配置文件、配置防火墙规则,并进行网络故障排查。掌握这些命令能大幅提升网络管理和维护效率。
1238 11
|
6月前
|
存储 API 容器
47.[HarmonyOS NEXT RelativeContainer案例四] 构建动态进度条:相对布局实现精确控制与动画效果
进度条是应用程序中常见的UI元素,用于直观地展示任务完成进度或数据加载状态。在HarmonyOS NEXT中,使用RelativeContainer可以轻松实现功能丰富、视觉精美的进度条。本教程将详细讲解如何利用RelativeContainer的锚点系统构建动态进度条,帮助你掌握这一实用技术。
166 5
|
存储 自然语言处理 搜索推荐
外汇CRM系统的关键特点及功能解析
Zoho CRM外汇系统提供全面客户管理,涵盖信息记录、交易历史等,提升个性化服务水平。系统界面直观易用,支持自定义,数据分析实时,助决策精准。具备高安全性,多系统整合能力强,自动化功能提高效率,支持多语言,适用于全球市场,配备专业客户支持与培训,助力外汇企业优化流程,增强客户满意度,在竞争中领先。
273 1
|
10月前
|
数据挖掘 OLAP 云计算
[直播预约]StarRocks 2025 Roadmap 全面解读
2月19日19:00-20:30,StarRocks TSC Member赵恒、康凯森将解读2025 Roadmap,并邀请多位专家分享最新进展。欢迎参与交流!
|
监控 网络协议 数据库
OSPF中的区域划分详解
OSPF中的区域划分详解
1101 0
|
Java 编译器 Go
QT软件开发:基于libVLC内核设计视频播放器
QT软件开发:基于libVLC内核设计视频播放器
1337 0
QT软件开发:基于libVLC内核设计视频播放器
|
SQL 数据库
详解BaseMapper
详解BaseMapper
752 0