1)新创建一个项目,修改Form1的Text属性为testNotifyIcon;
(2)向Form1窗口中添加一个NotifyIcon控件,修改其Name属性为 MyNotifyIcon。修改其Text属性为
  testNotifyIcon,与应用程序的标题相同。这是因为NotifyIcon显示在系统托盘中时,Text属性中保存的
  文本为鼠标移动到程序图标上时的提示信息。修改Icon属性,为NotifyIcon添加一个图标;
(3)在Form1的属性窗口上,把Icon属性也选择为与NotifyIcon相同的图标。修改ShowInTaskBar属性值为
    False,这样,应用程序在运行时就不会出现在任务栏中;
(4)添加双击NotifyIcon时的事件处理代码(DoubleClick)
  private void MynotifyIcon_DoubleClick(object sender, System.EventArgs e)
  {
    if(this.Visible)
      {
        this.Hide();
      }else
          {
            this.Show();
          }
      }
(5)在使用NotifyIcon控件时,经常需要用到快捷菜单(ContextMenu控件),在NotifyIcon的ContextMenu属性中
  选择一个快捷菜单。这样在图标上可以通过快捷菜单执行程序的各种功能。 
例如:给快捷菜单添加两个功能,一个为显示表单,一个为隐藏表单
在功能的单击事件中代码如下:
private void menuItem1_Click(object sender, System.EventArgs e)
  {
    this.Show();//显示表单
  }
  private void menuItem2_Click(object sender, System.EventArgs e)
  {
      this.Hide();//隐藏表单
  }
public Form1()
        {
            InitializeComponent();
            this.FormClosing += new FormClosingEventHandler(Form1_FormClosing);  //2.0
        }
void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MessageBox.Show("Are you sure?", "Application", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.No)
            {
                e.Cancel = true;
                this.Hide();
                this.MyNotifyIcon.Visible = true; //托盘图标可见
            }
        }
posted on 2011-02-12 14:11  Berthing  阅读(336)  评论(0)    收藏  举报