C# 设置程序最小化到任务栏右下角,鼠标左键单击还原,右键提示关闭程序

 

首先设置程序最小化到任务栏右下角

先给窗口添加一个notifyIcon控件

为notifyIcon控件设置ICO图标(不设置图标将无法在任务栏显示)

给notifyIcon控件添加点击事件

 

然后是最小化到任务栏右下角

                if (this.WindowState == FormWindowState.Normal && this.Visible == true)
                {
                    this.notifyIcon1.Visible = true;//在通知区显示Form的Icon
                    this.WindowState = FormWindowState.Minimized;
                    this.Visible = false;
                    this.ShowInTaskbar = false;//使Form不在任务栏上显示
                    this.Hide();
                }

接下来判断点击的是鼠标的哪个按键

        /// <summary>
        /// 添加双击托盘图标事件(双击显示窗口)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)//判断鼠标的按键
            {
                if (this.WindowState == FormWindowState.Normal)
                {
                    this.WindowState = FormWindowState.Minimized;
                    this.Hide();
                }
                else if (this.WindowState == FormWindowState.Minimized)
                {
                    this.Show();
                    this.WindowState = FormWindowState.Normal;
                    this.Activate();
                }
            }
            else if (e.Button == MouseButtons.Right)
            {
                if (MessageBox.Show("是否需要关闭程序?", "提示:", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation) == DialogResult.OK)//出错提示
                {
                    //鼠标查询进程
                    _MouseStop = false;
                    if (Mouse_Thread != null)
                    {
                        Mouse_Thread.Abort();
                        Mouse_Thread = null;
                    }
                    //关闭窗口
                    DialogResult = DialogResult.No;
                    Dispose();
                    Close();
                }
            }
        }

如果点击的是左键,就判断窗口是不是最小化的,是就还原窗口。不是就隐藏窗口

            if (e.Button == MouseButtons.Left)//判断鼠标的按键
            {
                if (this.WindowState == FormWindowState.Normal)
                {
                    this.WindowState = FormWindowState.Minimized;
                    this.Hide();
                }
                else if (this.WindowState == FormWindowState.Minimized)
                {
                    this.Show();
                    this.WindowState = FormWindowState.Normal;
                    this.Activate();
                }
            }

如果点击的是右键,则弹出提示框,提示用户

            else if (e.Button == MouseButtons.Right)
            {
                if (MessageBox.Show("是否需要关闭程序?", "提示:", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation) == DialogResult.OK)//出错提示
                {
                    //鼠标查询进程
                    _MouseStop = false;
                    if (Mouse_Thread != null)
                    {
                        Mouse_Thread.Abort();
                        Mouse_Thread = null;
                    }
                    //关闭窗口
                    DialogResult = DialogResult.No;
                    Dispose();
                    Close();
                }
            }

作者:逐梦
出处:http://www.cnblogs.com/huanjun/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

posted @ 2017-12-19 18:34  随遇而安(huangjun)  阅读(5898)  评论(0编辑  收藏  举报