欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

WinForm实现最小化到系统托盘方法实例详解

程序员文章站 2023-08-22 22:12:33
本文实例讲述了winform实现最小化到系统托盘方法。分享给大家供大家参考。具体分析如下: 有个叫notifyicon的控件 1、建个winform项目,其它操作略过。...

本文实例讲述了winform实现最小化到系统托盘方法。分享给大家供大家参考。具体分析如下:

有个叫notifyicon的控件

1、建个winform项目,其它操作略过。
2、拉个notifyicon控件,将属性visable设置成false,在text属性上随便填些文件。
3、实现form的sizechanged事件,代码如下:

if(this.windowstate == formwindowstate.minimized) //判断是否最小化
{
this.showintaskbar = false; //不显示在系统任务栏
notifyicon.visible = true; //托盘图标可见
}

4、实现notifyicon控件的doubleclick事件,代码如下:

if(this.windowstate == formwindowstate.minimized)
{
this.showintaskbar = true; //显示在系统任务栏
this.windowstate = formwindowstate.normal; //还原窗体
notifyicon.visible = false; //托盘图标隐藏
}

例题:

private contextmenu notifyiconmnu;
#region 最小化到任务栏
/// <summary>
/// 最小化到任务栏
/// </summary>
private void initializenotifyicon()
{
  //定义一个menuitem数组,并把此数组同时赋值给contextmenu对象 
  menuitem[] mnuitms = new menuitem[3];
  mnuitms[0] = new menuitem();
  mnuitms[0].text = "显示窗口";
  mnuitms[0].click += new system.eventhandler(this.notifyicon1_showfrom);
  mnuitms[1] = new menuitem("-");
  mnuitms[2] = new menuitem();
  mnuitms[2].text = "退出系统";
  mnuitms[2].click += new system.eventhandler(this.exitselect);
  mnuitms[2].defaultitem = true;
  notifyiconmnu = new contextmenu(mnuitms);
  notifyicon1.contextmenu = notifyiconmnu;
  //为托盘程序加入设定好的contextmenu对象 
}
private void notifyicon1_doubleclick(object sender, eventargs e)
{
  if (this.windowstate == formwindowstate.minimized)
  {
    this.show();
    this.showintaskbar = true; 
    this.windowstate = formwindowstate.normal; 
    notifyicon1.visible = false; 
  }
}
public void notifyicon1_showfrom(object sender, system.eventargs e)
{
  if (this.windowstate == formwindowstate.minimized)
  {
    this.show();
    this.showintaskbar = true;
    this.windowstate = formwindowstate.normal;
    notifyicon1.visible = false;
  }
}
public void exitselect(object sender, system.eventargs e)
{
  //隐藏托盘程序中的图标 
  notifyicon1.visible = false;
  //关闭系统 
  this.close();
  this.dispose(true);
}
#endregion
private void form_main_sizechanged(object sender, eventargs e)
{
  if (this.windowstate == formwindowstate.minimized)
  //判断是否最小化
  {
    notifyicon1.visible = true;
    this.hide();
    this.showintaskbar = false;
    initializenotifyicon();
  }
}

希望本文所述对大家的c#程序设计有所帮助。