用户登录
用户注册

分享至

C# WinForm窗口最小化到系统托盘

  • 作者: 浮生若梦
  • 来源: 51数据库
  • 2021-10-19
1.设置winform窗体属性showintask=false
2.加notifyicon控件notifyicon1,为控件notifyicon1的属性icon添加一个icon图标。
3.添加窗体最小化事件(首先需要添加事件引用):

复制代码 代码如下:

this.sizechanged += new system.eventhandler(this.form1_sizechanged);
//上面一行是主窗体initializecomponent()方法中需要添加的引用
private void form1_sizechanged(object sender, eventargs e)
{
if(this.windowstate == formwindowstate.minimized)
{
this.hide();
this.notifyicon1.visible=true;
}
}

4.添加点击图标事件(首先需要添加事件引用):

private void notifyicon1_click(object sender, eventargs e)
{
this.visible = true;
this.windowstate = formwindowstate.normal;
this.notifyicon1.visible = false;
}
5.可以给notifyicon添加右键菜单:
主窗体中拖入一个contextmenu控件nicontextmenu,点中控件,在上下文菜单中添加菜单,notifyicon1的contextmenu行为中选中nicontextmenu 作为上下文菜单。
复制代码 代码如下:

this.notifyicon1 = new system.windows.forms.notifyicon(this.components);
this.nicontextmenu = new system.windows.forms.contextmenu();
this.menuitem_hide = new system.windows.forms.menuitem();
this.menuitem_show = new system.windows.forms.menuitem();
this.menuitem_aubot = new system.windows.forms.menuitem();
this.menuitem_exit = new system.windows.forms.menuitem();
this.notifyicon1.contextmenu = this.nicontextmenu;
this.notifyicon1.icon = ((system.drawing.icon)(resources.getobject( "notifyicon.icon ")));
this.notifyicon1.text = " ";
this.notifyicon1.visible = true;
this.notifyicon1.doubleclick += new system.eventhandler(this.notifyicon1_doubleclick);
this.notifyicon1.click += new system.eventhandler(this.notifyicon1_click);
this.nicontextmenu.menuitems.addrange(
new system.windows.forms.menuitem[]
{
this.menuitem_hide,
this.menuitem_show,
this.menuitem_aubot,
this.menuitem_exit
}
);
//
// menuitem_hide
//
this.menuitem_hide.index = 0;
this.menuitem_hide.text = "隐藏 ";
this.menuitem_hide.click += new system.eventhandler(this.menuitem_hide_click);
//
// menuitem_show
//
this.menuitem_show.index = 1;
this.menuitem_show.text = "显示 ";
this.menuitem_show.click += new system.eventhandler(this.menuitem_show_click);
//
// menuitem_aubot
//
this.menuitem_aubot.index = 2;
this.menuitem_aubot.text = "关于 ";
this.menuitem_aubot.click += new system.eventhandler(this.menuitem_aubot_click);
//
// menuitem_exit
//
this.menuitem_exit.index = 3;
this.menuitem_exit.text = "退出 ";
this.menuitem_exit.click += new system.eventhandler(this.menuitem_exit_click);
protected override void onclosing(canceleventargs e)
{
this.showintaskbar = false;
this.windowstate = formwindowstate.minimized;
e.cancel = true;
}
protected override void onclosing(canceleventargs e)
{
//this.showintaskbar = false;
this.windowstate = formwindowstate.minimized;
e.cancel = true;
}
private void closectiserver()
{
timer.enabled = false;
dj160api.disablecard();
this.notifyicon.visible = false;
this.close();
this.dispose();
application.exit();
}
private void hidectiserver()
{
this.hide();
}
private void showctiserver()
{
this.show();
this.windowstate = formwindowstate.normal;
this.activate();
}
private void ctimaniform_closing(object sender, system.componentmodel.canceleventargs e)
{
this.closectiserver();
}
private void menuitem_show_click(object sender, system.eventargs e)
{
this.showctiserver();
}
private void menuitem_aubot_click(object sender, system.eventargs e)
{
}
private void menuitem_exit_click(object sender, system.eventargs e)
{
this.closectiserver();
}
private void menuitem_hide_click(object sender, system.eventargs e)
{
this.hidectiserver();
}
private void ctimaniform_sizechanged(object sender, system.eventargs e)
{
if(this.windowstate == formwindowstate.minimized)
{
this.hidectiserver();
}
}
private void notifyicon1_doubleclick(object sender,system.eventargs e)
{
this.showctiserver();
}
软件
前端设计
程序设计
Java相关