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();
}
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();
}
推荐阅读
- C#通过fleck实现wss协议的WebSocket多人Web实时聊天(附源码)
- 团队城市未满足要求:MSBuildTools12.0_x86_Path 存在
- 使用 MSBuild.exe 在发布模式下构建 C# 解决方案
- 当我发布 Web 应用程序时,AfterPublish 脚本不运行
- 构建时 T4 转换的产品仅在下一个构建中使用
- ASP.NET Core Application (.NET Framework) for Windows x64 only error in project.assets.json
- 新的 .csproj 格式 - 如何将整个目录指定为“链接文件"到子目录?
- 如何将条件编译符号(DefineConstants)传递给 msbuild
- MSBuild 支持 Visual Studio 2017 RTM 中的 T4 模板
- NuGet 包还原找不到包,没有源
热点文章
团队城市未满足要求:MSBuildTools12.0_x86_Path 存在
0
使用 MSBuild.exe 在发布模式下构建 C# 解决方案
0
当我发布 Web 应用程序时,AfterPublish 脚本不运行
0
构建时 T4 转换的产品仅在下一个构建中使用
0
ASP.NET Core Application (.NET Framework) for Windows x64 only error in project.assets.json
0
新的 .csproj 格式 - 如何将整个目录指定为“链接文件"到子目录?
0
如何将条件编译符号(DefineConstants)传递给 msbuild
0
MSBuild 支持 Visual Studio 2017 RTM 中的 T4 模板
0
NuGet 包还原找不到包,没有源
0
使用 C# 6.0 功能运行 TFS 构建
0