Chinaunix首页 | 论坛 | 博客
  • 博客访问: 448127
  • 博文数量: 62
  • 博客积分: 1312
  • 博客等级: 中尉
  • 技术积分: 1555
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-24 18:10
文章分类

全部博文(62)

文章存档

2014年(1)

2013年(5)

2012年(56)

分类: 嵌入式

2012-03-14 10:54:33

C#处理的窗体最小化到托盘,以及双击恢复窗口代码。

一:添加notifyIcon1控件。添加各种事件响应······

二:右键快捷菜单的添加:
1:添加contextMenuStrip控件,设置各个项
2:添加各个项的处理事件
3:与前面的notifyIcon1控件绑定(通过contextMenuStrip属性设置)

点击(此处)折叠或打开

  1. #region 私有方法 处理窗体的 显示 隐藏 关闭(退出)
  2.         private void ExitMainForm() //关闭(退出)

  3.         {
  4.             if (MessageBox.Show("您确定要退出化本系统吗?", "确认退出", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK)
  5.             {
  6.                 this.notifyIcon1.Visible = false;
  7.                 this.Close();
  8.                 this.Dispose();
  9.                 Application.Exit();
  10.             }
  11.         }

  12.         private void HideMainForm() //隐藏

  13.         {
  14.             this.Hide();
  15.         }

  16.         private void ShowMainForm() //显示主窗口

  17.         {
  18.             this.Show();
  19.             this.WindowState = FormWindowState.Normal;
  20.             this.Activate();
  21.         }
  22.         #endregion

  23.         #region 右键菜单处理,显示 隐藏 退出
  24.         private void menuItem_Show_Click(object sender, EventArgs e)
  25.         {
  26.             ShowMainForm();
  27.         }

  28.         private void menuItem_Hide_Click(object sender, EventArgs e)
  29.         {
  30.             HideMainForm();
  31.         }

  32.         private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
  33.         {
  34.             ExitMainForm();
  35.         }

  36.         private void 还原ToolStripMenuItem_Click(object sender, EventArgs e)
  37.         {
  38.             ShowMainForm();
  39.         }

  40.         #endregion

  41.         #region 双击托盘上图标时,显示窗体
  42.         private void notifyIcon1_DoubleClick_1(object sender, EventArgs e)
  43.         {
  44.             ShowMainForm();
  45.         }
  46.         #endregion

  47.         #region 点最小化按钮时,最小化到托盘
  48.         private void Form_Server_SizeChanged(object sender, EventArgs e)
  49.         {
  50.             if (this.WindowState == FormWindowState.Minimized)
  51.             {
  52.                 HideMainForm();
  53.             }
  54.         }
  55.         #endregion

  56.         #region 窗体关闭时最小化到托盘
  57.         private void Form_Server_FormClosing(object sender, FormClosingEventArgs e)
  58.         {
  59.             e.Cancel = true;

  60.             HideMainForm();
  61.         }
  62.         #endregion


 

阅读(4782) | 评论(0) | 转发(0) |
0

上一篇:Linux 源码目录结构

下一篇:OVERLAPPED

给主人留下些什么吧!~~