zoukankan      html  css  js  c++  java
  • 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/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

  • 相关阅读:
    git 常用命令 command 1.0(本地 local repository 对远程仓库 remote repository 的操作)
    git 常用命令 command
    git .gitignore 忽略列表
    节点的类型
    Error.name 六种值对应的信息
    js或jquery中的验证
    转载:JavaWeb学习总结(五十三)——Web应用中使用JavaMail发送邮件
    转载:JavaWeb学习总结(五十二)——使用JavaMail创建邮件和发送邮件
    转载:JavaWeb学习总结(五十一)——邮件的发送与接收原理
    转载:JavaWeb学习总结(五十)——文件上传和下载
  • 原文地址:https://www.cnblogs.com/huanjun/p/8067157.html
Copyright © 2011-2022 走看看