①在MainForm上托2个控件NotifyIcon和ContextMenuStrip。
②NotifyIcon:用于显示托盘。
a)先Choose Icon,作为托盘显示的图标;
b)设置属性ContextMenuStrip,关联右击后显示的菜单。
c)追加双击事件:

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

        }


③ContextMenuStrip:右击托盘显示的菜单。
a)追加3个Item,最大化,最小化,关闭。
b)追加最大化,最小化,关闭的处理代码。

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
            this.mainNotifyIcon.Visible = true;
            this.Hide();
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;
            this.mainNotifyIcon.Visible = true;
            this.Show();
        }

        private void toolStripMenuItem3_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Do you want to quit?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
            {
                this.mainNotifyIcon.Visible = false;
                this.Close();
                this.Dispose();
                System.Environment.Exit(System.Environment.ExitCode);
            }
        }


③重写主窗口的FormClosing方法,使Main上点关闭时,最小化到托盘。

        private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                e.Cancel = true;

                this.WindowState = FormWindowState.Minimized;
                this.mainNotifyIcon.Visible = true;
                this.Hide();
                return;
            }
        }

 

相关文章:

  • 2021-08-08
  • 2022-12-23
  • 2021-12-06
  • 2022-01-12
  • 2022-12-23
猜你喜欢
  • 2021-06-05
  • 2022-12-23
  • 2022-12-23
  • 2021-07-04
  • 2021-08-16
相关资源
相似解决方案