1. 程式人生 > >C# 托盤程式 例項 雙擊顯示窗體,最小化到托盤

C# 托盤程式 例項 雙擊顯示窗體,最小化到托盤

原文:http://blog.csdn.net/lan_liang/article/details/7697742

單擊工作列  顯示-隱藏切換,右鍵選單,捕捉關閉窗體事件

[csharp] view plaincopyprint?
  1. public partial class frmMain : Form  
  2.    {  
  3.        public frmMain()  
  4.        {  
  5.            InitializeComponent();  
  6.        }  
  7.        #region 登出
  8.        publicvoid Logout()  
  9.        {  
  10.            if (MessageBox.Show("確認要退出嗎?""提示", MessageBoxButtons.OKCancel) == DialogResult.OK)  
  11.            {  
  12.                notifyIcon1.Visible = false;  
  13.                FormCollection fc = Application.OpenForms;  
  14.                if (fc != null && fc.Count > 0)  
  15.                {  
  16.                    foreach
     (Form window in fc)  
  17.                    {  
  18.                        window.Hide();  
  19.                    }  
  20.                }  
  21.                CacheHelper.CurrentUsrName = "";  
  22.                CacheHelper.CurrentRoleId = 0;  
  23.                frmLogin fl = new frmLogin();  
  24.                fl.Show();  
  25.            }  
  26.        }  
  27.        privatevoid 登出登陸ToolStripMenuItem_Click(object sender, EventArgs e)  
  28.        {  
  29.            Logout();  
  30.        }  
  31.        #endregion
  32.        #region 修改密碼
  33.        privatevoid 修改密碼ToolStripMenuItem_Click(object sender, EventArgs e)  
  34.        {  
  35.            frmChangePwd fcp = new frmChangePwd();  
  36.            fcp.Show();  
  37.        }  
  38.        #endregion
  39.        privatevoid frmMain_Load(object sender, EventArgs e)  
  40.        {  
  41.        }  
  42.        privatevoid frmMain_FormClosing(object sender, FormClosingEventArgs e)  
  43.        {  
  44.            if (MessageBox.Show("確認要退出嗎?""提示", MessageBoxButtons.OKCancel) == DialogResult.OK)  
  45.            {  
  46.                Logout();  
  47.            }  
  48.            else
  49.            {  
  50.                e.Cancel = true;  
  51.            }  
  52.        }  
  53.        privatevoid 最大化ToolStripMenuItem_Click(object sender, EventArgs e)  
  54.        {  
  55.            ShowWin();  
  56.        }  
  57.        privatevoid 退出ToolStripMenuItem_Click(object sender, EventArgs e)  
  58.        {  
  59.            Logout();  
  60.        }  
  61.        privatevoid frmMain_SizeChanged(object sender, EventArgs e)  
  62.        {  
  63.            if (this.WindowState == FormWindowState.Minimized)  
  64.            {  
  65.                HideWin();  
  66.            }  
  67.        }  
  68.        privatevoid notifyIcon1_MouseClick(object sender, MouseEventArgs e)  
  69.        {  
  70.            if (e.Button == MouseButtons.Right)  
  71.            {  
  72.                contextMenuStrip1.Show(MousePosition.X, MousePosition.Y);  
  73.            }  
  74.            else
  75.            {  
  76.                ShowWin();  
  77.            }  
  78.        }  
  79.        #region 隱藏顯示窗體
  80.        /// <summary>
  81.        /// 隱藏窗體
  82.        /// </summary>
  83.        privatevoid HideWin()  
  84.        {  
  85.            this.notifyIcon1.Visible = true;  
  86.            this.Hide();  
  87.        }  
  88.        /// <summary>
  89.        /// 顯示主窗體
  90.        /// </summary>
  91.        privatevoid ShowWin()  
  92.        {  
  93.            if (Visible)  
  94.            {  
  95.                HideWin();  
  96.            }  
  97.            else
  98.            {  
  99.                /////這裡注意順序很重要,先show 後設置state
  100.                Show();  
  101.                WindowState = FormWindowState.Normal;  
  102.            }  
  103.        }  
  104.        #endregion
  105.    }