C# 托盘程序 实例 双击显示窗体,最小化到托盘

来源:互联网 发布:re.sub python 编辑:程序博客网 时间:2024/05/16 07:29

原文: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.  
  8.  
  9.        #region 注销  
  10.   
  11.   
  12.        public void Logout()  
  13.        {  
  14.            if (MessageBox.Show("确认要退出吗?""提示", MessageBoxButtons.OKCancel) == DialogResult.OK)  
  15.            {  
  16.                notifyIcon1.Visible = false;  
  17.                FormCollection fc = Application.OpenForms;  
  18.                if (fc != null && fc.Count > 0)  
  19.                {  
  20.                    foreach (Form window in fc)  
  21.                    {  
  22.                        window.Hide();  
  23.                    }  
  24.                }  
  25.                CacheHelper.CurrentUsrName = "";  
  26.                CacheHelper.CurrentRoleId = 0;  
  27.                frmLogin fl = new frmLogin();  
  28.                fl.Show();  
  29.            }  
  30.   
  31.   
  32.        }  
  33.   
  34.   
  35.        private void 注销登陆ToolStripMenuItem_Click(object sender, EventArgs e)  
  36.        {  
  37.            Logout();  
  38.        }  
  39.  
  40.  
  41.        #endregion  
  42.  
  43.  
  44.        #region 修改密码  
  45.   
  46.   
  47.        private void 修改密码ToolStripMenuItem_Click(object sender, EventArgs e)  
  48.        {  
  49.            frmChangePwd fcp = new frmChangePwd();  
  50.            fcp.Show();  
  51.        }  
  52.  
  53.  
  54.        #endregion  
  55.   
  56.   
  57.        private void frmMain_Load(object sender, EventArgs e)  
  58.        {  
  59.   
  60.   
  61.        }  
  62.   
  63.   
  64.        private void frmMain_FormClosing(object sender, FormClosingEventArgs e)  
  65.        {  
  66.            if (MessageBox.Show("确认要退出吗?""提示", MessageBoxButtons.OKCancel) == DialogResult.OK)  
  67.            {  
  68.                Logout();  
  69.            }  
  70.            else  
  71.            {  
  72.                e.Cancel = true;  
  73.            }  
  74.        }  
  75.   
  76.   
  77.        private void 最大化ToolStripMenuItem_Click(object sender, EventArgs e)  
  78.        {  
  79.            ShowWin();  
  80.        }  
  81.   
  82.   
  83.        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)  
  84.        {  
  85.            Logout();  
  86.        }  
  87.   
  88.   
  89.        private void frmMain_SizeChanged(object sender, EventArgs e)  
  90.        {  
  91.            if (this.WindowState == FormWindowState.Minimized)  
  92.            {  
  93.                HideWin();  
  94.            }  
  95.        }  
  96.   
  97.   
  98.        private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)  
  99.        {  
  100.            if (e.Button == MouseButtons.Right)  
  101.            {  
  102.                contextMenuStrip1.Show(MousePosition.X, MousePosition.Y);  
  103.            }  
  104.            else  
  105.            {  
  106.                ShowWin();  
  107.            }  
  108.        }  
  109.  
  110.  
  111.        #region 隐藏显示窗体  
  112.   
  113.   
  114.        /// <summary>  
  115.        /// 隐藏窗体  
  116.        /// </summary>  
  117.        private void HideWin()  
  118.        {  
  119.            this.notifyIcon1.Visible = true;  
  120.            this.Hide();  
  121.        }  
  122.   
  123.   
  124.        /// <summary>  
  125.        /// 显示主窗体  
  126.        /// </summary>  
  127.        private void ShowWin()  
  128.        {  
  129.            if (Visible)  
  130.            {  
  131.                HideWin();  
  132.            }  
  133.            else  
  134.            {  
  135.                /////这里注意顺序很重要,先show 后设置state  
  136.                Show();  
  137.                WindowState = FormWindowState.Normal;  
  138.            }  
  139.        }  
  140.  
  141.  
  142.        #endregion  
  143.    }  

0 0
原创粉丝点击