Winfrom窗体应用程序___程序最小化至托盘

程序最小化至托盘包含以下:

最小化-显示托盘-双击托盘-程序显示

1.首先新建托盘控件,设置图标和名称:

 

双击控件代码如下:

/// <summary>
        /// 添加双击托盘图标事件(双击显示窗口)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (WindowState == FormWindowState.Minimized)
            {
                //还原窗体显示    
                WindowState = FormWindowState.Normal;
                //激活窗体并给予它焦点
                this.Activate();
                //任务栏区显示图标
                this.ShowInTaskbar = true;
                //托盘区图标隐藏
                notifyIcon1.Visible = false;
            }
        }

在主窗体的事件点击:SizeChanged &&  FormClosing  //大小改变和窗体关闭

代码如下:

 1 /// <summary>
 2         /// 判断是否最小化,然后显示托盘
 3         /// </summary>
 4         /// <param name="sender"></param>
 5         /// <param name="e"></param>
 6         private void Form1_SizeChanged(object sender, EventArgs e)
 7         {
 8             //判断是否选择的是最小化按钮
 9             if (WindowState == FormWindowState.Minimized)
10             {
11                 //隐藏任务栏区图标
12                 this.ShowInTaskbar = false;
13                 //图标显示在托盘区
14                 notifyIcon1.Visible = true;
15             }
16         }
17 
18         /// <summary>
19         /// 确认是否退出
20         /// </summary>
21         /// <param name="sender"></param>
22         /// <param name="e"></param>
23         private void Form1_FormClosing(object sender, FormClosingEventArgs e)
24         {
25             if (MessageBox.Show("是否退出程序?\n确定则退出程序,取消则最小化至托盘", "退出", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
26             {
27                 // 关闭所有的线程
28                 this.Dispose();
29                 this.Close();
30             }
31             else
32             {
33                 e.Cancel = true;
34                 this.WindowState = FormWindowState.Minimized;
35             } 
36         }

 

 Over!

 

posted on 2017-10-27 11:00  梦林``ysl  阅读(555)  评论(0编辑  收藏  举报

导航