1、向窗体上添加如下控件:MenuStrip menuStrip1, NotifyIcon ni_frmMain,Timer timer1, ContentMenuStrip cms_notify。其中notify中包含显示、退出等。

2、实现的代码:

  1. #region 托盘相关代码
  2.  
  3. #region 私有方法 处理窗体的 显示 隐藏 关闭(退出)
  4.  
  5. /// <summary>
  6. /// 显示
  7. /// </summary>
  8. private void ShowMainForm()
  9. {
  10. this.Show();
  11. this.WindowState = this.lastFormState;
  12. this.Activate();
  13.  
  14. this.ShowInTaskbar = true; //任务栏中显示窗体图标
  15. //this.ni_frmMain.Visible = false;//图盘中隐藏图标
  16. }
  17.  
  18. /// <summary>
  19. /// 隐藏
  20. /// </summary>
  21. private void HideMainForm()
  22. {
  23. this.Hide();
  24.  
  25. this.ShowInTaskbar = false; //任务栏中显示窗体图标
  26. //this.ni_frmMain.Visible = true;//图盘中隐藏图标
  27. }
  28.  
  29. /// <summary>
  30. /// 关闭(退出)
  31. /// </summary>
  32. private void ExitMainForm()
  33. {
  34. if (MessageBox.Show("您确定要退出主程序吗?", "确认退出", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK)
  35. {
  36. this.ni_frmMain.Visible = false;
  37. this.Close();
  38. this.Dispose();
  39. Application.Exit();
  40. }
  41. }
  42.  
  43. #endregion
  44.  
  45. #region 右键菜单处理,显示 隐藏 退出
  46.  
  47. /// <summary>
  48. /// 显示
  49. /// </summary>
  50. /// <param name="sender"></param>
  51. /// <param name="e"></param>
  52. private void tsmi_notifyShow_Click(object sender, EventArgs e)
  53. {
  54. ShowMainForm();
  55. }
  56.  
  57. /// <summary>
  58. /// 隐藏
  59. /// </summary>
  60. /// <param name="sender"></param>
  61. /// <param name="e"></param>
  62. private void tsmi_notifyHide_Click(object sender, EventArgs e)
  63. {
  64. HideMainForm();
  65. }
  66.  
  67. /// <summary>
  68. /// 退出
  69. /// </summary>
  70. /// <param name="sender"></param>
  71. /// <param name="e"></param>
  72. private void tsmi_notifyExit_Click(object sender, EventArgs e)
  73. {
  74. ExitMainForm();
  75. }
  76.  
  77. #endregion
  78.  
  79. #region 私有方法 双击托盘上图标时,显示窗体
  80.  
  81. private void ni_frmMain_DoubleClick(object sender, EventArgs e)
  82. {
  83. if (this.WindowState == FormWindowState.Minimized)
  84. {
  85. ShowMainForm();
  86. }
  87. else
  88. {
  89. this.WindowState = FormWindowState.Minimized;
  90. HideMainForm();
  91. }
  92. }
  93.  
  94. #endregion
  95.  
  96. #region 点最小化按钮时,最小化到托盘
  97.  
  98. private void frmMain_SizeChanged(object sender, EventArgs e)
  99. {
  100. if (this.WindowState != FormWindowState.Minimized)
  101. {
  102. this.ShowInTaskbar = true; //任务栏中窗体图标显示
  103. //this.ni_frmMain.Visible = false;
  104. this.lastFormState = this.WindowState;
  105. }
  106. else
  107. {
  108. HideMainForm();
  109. }
  110. }
  111.  
  112. #endregion
  113.  
  114. #region 窗体关闭时最小化到托盘
  115. private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
  116. {
  117. //e.Cancel = true;
  118. //this.ShowInTaskbar = false; //任务栏中窗体图标消失
  119. //this.ni_frmMain.Visible = true;
  120. //HideTipForm();
  121. }
  122.  
  123. #endregion
  124.  
  125. #region 图标闪烁 开启 关闭
  126.  
  127. /// <summary>
  128. /// 开启
  129. /// </summary>
  130. private void StartFlicker()
  131. {
  132. this.timer1.Enabled = true;
  133. this.timer1.Start();
  134. }
  135.  
  136. /// <summary>
  137. /// 关闭
  138. /// </summary>
  139. private void CloseFlicker()
  140. {
  141. this.timer1.Stop();
  142. this.timer1.Enabled = false;
  143. this.ni_frmMain.Icon = Truelore.Fare.Client.Properties.Resources.truelore0;
  144. }
  145.  
  146. private int i = ;
  147. private void timer1_Tick(object sender, EventArgs e)
  148. {
  149. if (i < )
  150. {
  151. this.ni_frmMain.Icon = Truelore.Fare.Client.Properties.Resources.truelore0;
  152. i++;
  153. return;
  154. }
  155. else
  156. {
  157. this.ni_frmMain.Icon = Truelore.Fare.Client.Properties.Resources.truelore1;
  158. i = ;
  159. }
  160. }
  161. #endregion
  162.  
  163. #endregion

C#:实现托盘的更多相关文章

  1. C++ 最小化到托盘

    #define WM_SHOWTASK (WM_USER + 1) void CTestDlg::OnSysCommand(UINT nID, LPARAM lParam) { if ((nID &a ...

  2. delphi 实现最小化系统托盘

    1.new -->application 2.在form1中加入一个tPopMenu 命名为pm1 3.uses ShellAPI; 4.定义一个常量在 const WM_TRAYMSG = W ...

  3. WPF-系统托盘

    WPFSystemTray.cs public class WPFSystemTray { /// <summary> /// 设置系统托盘 /// </summary> // ...

  4. WinForm最小化到托盘以及托盘右键菜单

    首先,先拖一个NotifyIcon到主窗体,然后设置NotifyIcon的图标,不然等下最小化后,都找不到那个程序了,还有那个Text也是,不写名字,就默认是NotifyIcon了..如下图: 然后双 ...

  5. 小菜学习Winform(七)系统托盘

    前言 有些程序在关闭或最小化的时候会隐藏在系统托盘中,双击或右击会重新显示,winform实现其功能很简单,这边就简单的介绍下. 实现 在winform实现托盘使用notifyIcon控件,如果加菜单 ...

  6. 将 VMware 最小化到系统托盘

    1, 下载 Trayconizer官网地址: http://www.whitsoftdev.com/trayconizer/下载地址: http://www.whitsoftdev.com/files ...

  7. C#编写WIN32系统托盘程序

    基本功能概述: 程序运行后驻留系统托盘,左键呼出,右键退出.后续可加右键菜单. 注册系统案件WIN+F10,呼出程序. 重写系统消息,最小化和关闭按钮隐藏程序 using System; using ...

  8. VC++ 最小化到托盘、恢复

    所谓的“托盘”,在Windows系统界面中,指的就是下面任务条右侧,有系统时间等等的标志的那一部分.在程序最小化或挂起时,但有不希望占据任务栏的时候,就可以把程序放到托盘区. 一.托盘编程相关函数   ...

  9. WP7系统托盘和应用程序栏

    (一)系统托盘和应用程序栏系统托盘(1)显示系统级别的状态信息(2)Apps能隐藏和显示系统托盘Micosoft.Phone.Shell.SystemTray.IsVisible=true;应用程序栏 ...

  10. QQ揭秘:如何实现托盘闪动消息提醒?【低调赠送:QQ高仿版GG 4.1 最新源码】

    当QQ收到好友的消息时,托盘的图标会变成好友的头像,并闪动起来,点击托盘,就会弹出与好友的聊天框,随即,托盘恢复成QQ的图标,不再闪动.当然,如果还有其它的好友的消息没有提取,托盘的图标会变成另一个好 ...

随机推荐

  1. ASP.NET MVC Template

    http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-1-introduction.html http://st ...

  2. Oracle 监听器日志文件过大导致监听异常

    Oracle 监听器日志文件过大导致监听异常 db版本:11.2.0.1 os版本:windows2008 现象: 应用异常,无法连接数据库.登陆数据库服务器,查看监听已经断掉.尝试重启监听,重启失败 ...

  3. Function call process

    摘自自博客网址 http://www.cnblogs.com/bangerlee/archive/2012/05/22/2508772.html ax(accumulator): 可用于存放函数返回值 ...

  4. ps 简介

    1. ps 简介ps 命令就是最根本相应情况下也是相当强大地进程查看命令.运用该命令可以确定有哪些进程正在运行和运行地状态. 进程 是否结束.进程有没有僵死.哪些进程占用了过多地资源等等.总之大部分信 ...

  5. [转]JVM内幕:Java虚拟机详解

    本文由 ImportNew - 挖坑的张师傅 翻译自 jamesdbloom.欢迎加入翻译小组.转载请见文末要求. 这篇文章解释了Java 虚拟机(JVM)的内部架构.下图显示了遵守Java SE 7 ...

  6. B/S与C/S区别

    B/S (Brower/Server)-->浏览器/服务器 程序完全部署在服务器上,用户通过浏览器访问应用程序,它是基于internet产物(在应用服务器中部署运行程序) c/s(Client/ ...

  7. css3:与背景的相关样式

    1. (1)background-origin : border-box | padding-box | content-box;(设置元素背景图片的原始起始位置.) //需要注意的是,如果背景不是n ...

  8. mac tomcat

    alampsdeMacBook-Pro:bin alamps$ ./startup.sh Using CATALINA_BASE: /Users/alamps/Library/apache-tomca ...

  9. C语言 单引号 双引号问题

    C语言中的单引号和双引号含义迥异,用单引号引起的一个字符实际上代表一个整数,整数值对应于该字符在编译器采用的字符集中的序列值,因此,采用ASCII字符集的编译器而言,‘a’的含义与0141或97严格一 ...

  10. spark history-server的使用

    为什么需要historyServer? 在运行Spark Application的时候,Spark会提供一个WEBUI列出应用程序的运行时信息:但该WEBUI随着Application的完成(成功/失 ...