VS最小化程序到托盘

一、添加Notifylcon控件

二、调整控件属性

1.Icon:最小化托盘显示的图标(一定要设置,否则最小化后无法显示)

2.Text:最小化托盘图标显示的文字

3.Visible:True是会一直在托盘显示图标,这个自行选择即可

三、控件事件代码

private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
{
   if (e.Button == MouseButtons.Left)
   {
      if (this.WindowState == FormWindowState.Minimized)
      {
        //还原窗体
        his.WindowState = FormWindowState.Normal;
        //系统任务栏显示图标
        this.ShowInTaskbar = true;
      }
      //激活窗体并获取焦点
      this.Activate();
   }
}

private void MyService_FormClosin(object sender, FormClosingEventArgs e)
{
  // 注意判断关闭事件reason来源于窗体按钮,否则用菜单退出时无法退出!
  if (e.CloseReason == CloseReason.UserClosing)
  {
     //取消"关闭窗口"事件
     e.Cancel = true;
     //使关闭时窗口向右下角缩小的效果
     this.WindowState = FormWindowState.Minimized;
     this.notifyIcon1.Visible = true;
                this.Hide();               
                return;
            }
        }
发布了104 篇原创文章 · 获赞 36 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/lifuchao784533/article/details/95003840