WinForm 程序托盘及右键退出

//code by:博客园-曹永思

  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;
            }
        }

        /// <summary>
        /// 双击托盘打开主菜单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.Visible = true;
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.WindowState = FormWindowState.Normal;
            }
        }

        /// <summary>
        /// 退出程序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void contextMenuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            this.notifyIcon1.Visible = false;
            this.Close();
            this.Dispose();
            Application.Exit();
        }

/////没有这句的话,右键托盘图标,菜单是出不来的=============================
            notifyIcon1.ContextMenuStrip = contextMenuStrip1;

 欢迎转载,转载请注明出处,希望帮到更多人。

.net URL重写例子

原文地址:https://www.cnblogs.com/yonsy/p/2766331.html