C#实现点击窗体关闭按钮时到系统托盘,右键系统托盘弹出菜单点击退出退出整个程序

一,在工具箱添加notifyIcon控件,contextMenustrip控件

二,设置属性Icon ,Text ,visable(true),contextMenustrip,

三,添加窗体Closing()事件,加入代码:

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)//当用户点击窗体右上角X按钮或(Alt + F4)时 发生
            {
                e.Cancel = true;
                this.ShowInTaskbar = false;
                this.Hide();

            }
        }

四,添加contextMenu点击事件:

        private void 显示ToolStripMenuItem_Click(object sender, EventArgs e)//实现重新显示窗体
        {
            if (this.Visible == false)
                this.Visible = true;

        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)//实现关闭整个窗体
        {
            Application.Exit();//通过Application类的静态方法Exit()退出应用程序
        }

五,实现鼠标双击任务栏图标显示程序界面:

            private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
            {
                if (this.Visible == false)
                    this.Visible = true;
            }

你可能感兴趣的:(C++,c,C#)