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

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


单击任务栏  显示-隐藏切换,右键菜单,捕捉关闭窗体事件


[csharp]  view plain copy print ?
  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.    }  

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值