C# 程序最小化到系统托盘

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ot512csdn/article/details/82116392

找一个好的ICO图标。

1、放一个notifyIcon1和contextMenuStrip1控件。

2、在notifyIcon1属性中,关联加入contextMenuStrip1控件。

3、notifyIcon1增加事件:notifyIcon1_MouseDoubleClick。

4、contextMenuStrip1在菜单中加一个“退出”,点击它,增加事件:ToolStripMenuItem_Click

5、全部的代码:



        //MIN WINDOWS
        private void ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.notifyIcon1.Visible = false;
            this.Close();
            this.Dispose();
            System.Environment.Exit(0);
        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (this.Visible)
            {
                this.WindowState = FormWindowState.Minimized;
                this.notifyIcon1.Visible = true;
                this.Hide();
            }
            else
            {
                this.Visible = true;
                this.WindowState = FormWindowState.Normal;
                this.Activate();
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            // 注意判断关闭事件reason来源于窗体按钮,否则用菜单退出时无法退出!
            if (e.CloseReason == CloseReason.UserClosing)
            {
                //取消"关闭窗口"事件
                e.Cancel = true;
                //使关闭时窗口向右下角缩小的效果
                this.WindowState = FormWindowState.Minimized;
                this.notifyIcon1.Visible = true;
                this.Hide();
                return;
            }
        }

猜你喜欢

转载自blog.csdn.net/ot512csdn/article/details/82116392