using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace WindowsFormsApplication6
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }        private void Form1_Load(object sender, EventArgs e)
        {
            this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);
            notifyIcon1.Icon = new Icon("2d.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)//最小化      
            {
                this.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;        }    }   }我用上面的代码,但是却没法实现。点最小化的时候也没有最小化到托盘,点托盘的小图标的时候也不显示窗体。希望达人能告知原因。

解决方案 »

  1.   

      private void Form1_SizeChanged(object sender, EventArgs e)
       {
       if (this.WindowState == FormWindowState.Minimized)//最小化   
      {
       this.ShowInTaskbar = false;
       this.notifyIcon1.Visible = true;
       }   }
    这个方法你为什么还去判断是否最小化勒
      

  2.   


    private void Form1_SizeChanged(object sender, EventArgs e)
      {
      if (this.WindowState != FormWindowState.Minimized)//最小化   
      {
      this.ShowInTaskbar = false;
      this.notifyIcon1.Visible = true;
      }  }
      

  3.   

    单击托盘图标,触发的是mouseclick事件
      

  4.   

        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;  }
    在上面的方法中添加下面的设置
      this.Visible = true;
      

  5.   

      private void MDIParent1_SizeChanged(object sender, EventArgs e)
            {
                if(this.WindowState==FormWindowState.Minimized)
                {
                    this.ShowInTaskbar = false;
                    notifyIcon1.Visible = true;
                    
                }
            }        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
            {
                this.WindowState=FormWindowState.Normal;
                this.ShowInTaskbar = true;
                this.Show();
                notifyIcon1.Visible = false;
            }这个是可以的 
      

  6.   

    首先,Form1_Load 这里的事件要去掉。
    直接在NotifyIcon控件属性里设置图标和是否可见。
    然后把NotifyIcon控件的notifyIcon1_MouseClick事件实现。刚测试通过。只要不要在Form_Load 里面做这些。
      

  7.   

    给您一个算是比较完全的:
    首先用使用Winform控件 NotifyIcon  常用属性:Ico是显示在托盘时的图标,Text是鼠标停放在托盘显示图标上时显示的文字说明,ContextMenuStrip 是鼠标右击事件的菜单,这个大家应该都知道,其他的属性有兴趣自己查查看吧。
    窗体的SizeChanged事件中如下:
     private void FormMain_SizeChanged(object sender, EventArgs e)
            {
        //如果点击最小化按钮时,窗体隐藏,显示图标
                if (this.WindowState == FormWindowState.Minimized)
                {
                    this.Visible = false;
                    this.notifyIcon1.Visible = true;
                }
            }
    NotifyIcon控件的鼠标双击事件如下(当窗体最小化时,再重新打开窗体):
     private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
            {
                this.Visible = true;//弹出MainForm
                this.WindowState = FormWindowState.Normal;//窗体恢复正常
            }
    这样一个winfrom程序最小化托盘就实现了,还有一些细微的判断自己可以酌情添加。
    下面是我根据自己的需求添加的功能,在关闭窗体的时候提示是否关闭
    private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
            {
                if (MessageBox.Show("确认关闭吗?", "确认关闭", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    this.Dispose();
                    Application.Exit();
                }
                else
                {
                    e.Cancel = true;
                    this.WindowState = FormWindowState.Normal;
                    this.Visible = false;
                    this.notifyIcon1.Visible = true;
                }
            }
    在托盘显示图标加的右键退出菜单
    private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                if (MessageBox.Show("确认退出吗?", "确认退出", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    this.Dispose();
                    Application.Exit();
                }
                else
                {
                    this.WindowState = FormWindowState.Normal;
                    this.Visible = false;
                    this.notifyIcon1.Visible = true;
                }
            }