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




本文转自钢钢博客园博客,原文链接:http://www.cnblogs.com/xugang/archive/2007/12/19/1006005.html,如需转载请自行联系原作者
相关文章
|
2月前
|
Linux C# iOS开发
开源GTKSystem.Windows.Forms框架让C# Winform支持跨平台运行
开源GTKSystem.Windows.Forms框架让C# Winform支持跨平台运行
68 12
|
2月前
|
存储 监控 算法
企业内网监控系统中基于哈希表的 C# 算法解析
在企业内网监控系统中,哈希表作为一种高效的数据结构,能够快速处理大量网络连接和用户操作记录,确保网络安全与效率。通过C#代码示例展示了如何使用哈希表存储和管理用户的登录时间、访问IP及操作行为等信息,实现快速的查找、插入和删除操作。哈希表的应用显著提升了系统的实时性和准确性,尽管存在哈希冲突等问题,但通过合理设计哈希函数和冲突解决策略,可以确保系统稳定运行,为企业提供有力的安全保障。
|
4月前
|
存储 开发框架 .NET
C#语言如何搭建分布式文件存储系统
C#语言如何搭建分布式文件存储系统
112 2
|
4月前
|
设计模式 程序员 C#
C# 使用 WinForm MDI 模式管理多个子窗体程序的详细步骤
WinForm MDI 模式就像是有超能力一般,让多个子窗体井然有序地排列在一个主窗体之下,既美观又实用。不过,也要小心管理好子窗体们的生命周期哦,否则一不小心就会出现一些意想不到的小bug
395 0
|
4月前
|
API C# Windows
【C#】在winform中如何实现嵌入第三方软件窗体
【C#】在winform中如何实现嵌入第三方软件窗体
233 0
|
4月前
|
存储 分布式计算 监控
C# 创建一个分布式文件存储系统需要怎么设计??
C# 创建一个分布式文件存储系统需要怎么设计??
64 0
|
3月前
|
C# 开发者
C# 一分钟浅谈:Code Contracts 与契约编程
【10月更文挑战第26天】本文介绍了 C# 中的 Code Contracts,这是一个强大的工具,用于通过契约编程增强代码的健壮性和可维护性。文章从基本概念入手,详细讲解了前置条件、后置条件和对象不变量的使用方法,并通过具体代码示例进行了说明。同时,文章还探讨了常见的问题和易错点,如忘记启用静态检查、过度依赖契约和性能影响,并提供了相应的解决建议。希望读者能通过本文更好地理解和应用 Code Contracts。
66 3
|
2月前
|
存储 安全 编译器
学懂C#编程:属性(Property)的概念定义及使用详解
通过深入理解和使用C#的属性,可以编写更清晰、简洁和高效的代码,为开发高质量的应用程序奠定基础。
122 12
|
3月前
|
设计模式 C# 图形学
Unity 游戏引擎 C# 编程:一分钟浅谈
本文介绍了在 Unity 游戏开发中使用 C# 的基础知识和常见问题。从 `MonoBehavior` 类的基础用法,到变量和属性的管理,再到空引用异常、资源管理和性能优化等常见问题的解决方法。文章还探讨了单例模式、事件系统和数据持久化等高级话题,旨在帮助开发者避免常见错误,提升游戏开发效率。
138 4
|
5月前
|
API C#
C# 一分钟浅谈:文件系统编程
在软件开发中,文件系统操作至关重要。本文将带你快速掌握C#中文件系统编程的基础知识,涵盖基本概念、常见问题及解决方法。文章详细介绍了`System.IO`命名空间下的关键类库,并通过示例代码展示了路径处理、异常处理、并发访问等技巧,还提供了异步API和流压缩等高级技巧,帮助你写出更健壮的代码。
72 2