C#winform中怎么将程序最小化到系统托盘?
各位高手教教我!

解决方案 »

  1.   

    首先需要在窗体中增加一个NotifyIcon控件,并给控件指定图片。/// <summary>
    /// 显示隐藏窗口
    /// </summary>
    /// <param name="showWindow"></param>
    private void ShowHideWindow(bool showWindow)
    {
    if(showWindow == true)//显示
    {
    if(this.ShowInTaskbar==false)
    {
    this.ShowInTaskbar = true;
    this.Visible = true;
    //this.Show();
    }
    if(this.WindowState == FormWindowState.Minimized)
    {
    this.WindowState = FormWindowState.Normal;
    }
    this.Activate();
    }
    else//隐藏
    {
    if(this.WindowState == FormWindowState.Minimized)
    {
    this.WindowState = FormWindowState.Normal;
    }
    if(this.ShowInTaskbar == true)
    {
    this.ShowInTaskbar = false;
    this.Visible = false;
    //this.Hide();

    }
    }
    }
      

  2.   

    在form中添加一个NotifyIcon控件
    this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);
    notifyIcon1.Icon = new Icon("app.ico");//指定一个图标
    notifyIcon1.Visible = false;
    notifyIcon1.Click += new System.EventHandler(this.notifyIcon1_Click);
    this.SizeChanged += new System.EventHandler(this.Form1_SizeChanged);private void Form1_SizeChanged(object sender, EventArgs e)
    {
       if (this.WindowState==FormWindowState.Minimized)//最小化
       {
             his.ShowInTaskbar = false;
             this.notifyIcon1.Visible=true;
       }
    }private void notifyIcon1_Click(object Sender, EventArgs e)
    //单击系统栏图标激活窗体
    {
         if (this.WindowState == FormWindowState.Minimized)
             this.WindowState = FormWindowState.Normal;
         this.Activate();
         this.notifyIcon1.Visible = false;
         this.ShowInTaskbar = true;
    }