实现步骤
- 创建 notifyicon 控件并设置属性;
- 编写 notifyicon 响应控制事件;
- 在主窗体的load事件中将 notifyicon 添加到系统托盘;
- 程序退出时,移除系统托盘的 notifyicon;
notifyicon 控件,通常用于在系统托盘中显示图标,通过使用它就可以我们想要的效果。
| 属性 | 描述 |
|---|---|
| icon | 在系统托盘中显示的图标 |
| text | 鼠标悬停在图标时显示的文本 |
| visible | 指定是否可见 |
常用方法
| 方法 | 描述 |
|---|---|
| showcontextmenu | 在系统托盘上下文菜单中显示指定的菜单 |
添加控件(拖拽方式)
将notifyicon和一个contextmenustrip控件。拖到主窗体中可以修改控件名称
notifyicon 托盘图标
contextmenustrip 托盘图标右击弹出的菜单

设置控件
点击 contextmenustrip 右上方的三角图标 -> 编辑项,弹出项信合编辑器
添加右健菜单信息

添加主窗体事件
在最小化或关闭主窗体时,显示在任务栏托盘区域,实现了单击关闭时,不真正关闭程序,而是将主界面隐藏hide掉,同时开始显示托盘菜单。

// 只有form_closing事件中 e.cancel可以用。
// 你的是form_closed事件。 form_closed事件时窗口已关了 ,cancel没用了;
// form_closing是窗口即将关闭时询问你是不是真的关闭才有cancel事件
private void mainwindow_formclosing(object sender, formclosingeventargs e)
{
// 注意判断关闭事件reason来源于窗体按钮,否则用菜单退出时无法退出!
if (e.closereason == closereason.userclosing)
{
//取消"关闭窗口"事件
e.cancel = true; // 取消关闭窗体
//使关闭时窗口向右下角缩小的效果
this.windowstate = formwindowstate.minimized;
this.mainnotifyicon.visible = true;
//this.m_cartoonform.cartoonclose();
this.hide();
return;
}
}
实现双击托盘打开主程序
// 添加托盘程序
// 版本更新自1.0.1
private void mainnotifyicon_mousedoubleclick(object sender, mouseeventargs e)
{
if (this.visible)
{
this.windowstate = formwindowstate.minimized;
this.mainnotifyicon.visible = true;
this.hide();
}
else
{
this.visible = true;
this.windowstate = formwindowstate.normal;
this.activate();
}
}
// 添加托盘程序右键菜单项
// 版本更新自1.0.1
// 退出
// 添加日期 -- 2015-07-29 21:44
private async void toolstripmenuitemquit_click(object sender, eventargs e)
{
if (messagebox.show("你确定要退出?", "系统提示", messageboxbuttons.yesno, messageboxicon.information, messageboxdefaultbutton.button1) == dialogresult.yes)
{
this.mainnotifyicon.visible = false;
this.close();
this.dispose();
system.environment.exit(system.environment.exitcode);
}
}
代码方式添加
using system;
using system.windows.forms;
namespace fountain.winform.notifydemo
{
public partial class formmain : form
{
/// <summary>
/// 通知控件
/// </summary>
private notifyicon notifyicon = new notifyicon();
/// <summary>
/// 通知控件显示菜单
/// </summary>
private contextmenustrip contextmenustrip = new contextmenustrip();
/// <summary>
/// 构造方法
/// </summary>
public formmain()
{
initializecomponent();
}
/// <summary>
/// 窗体加载
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void formmain_load(object sender, eventargs e)
{
this.initializenotifymenu();
this.notifyicon.text = this.text;
this.notifyicon.visible = true;
this.notifyicon.icon = this.icon;
this.notifyicon.contextmenustrip = this.contextmenustrip;
this.notifyicon.doubleclick += notifyicon_doubleclick;
}
/// <summary>
/// 托盘菜单
/// </summary>
private void initializenotifymenu()
{
try
{
contextmenustrip.items.clear();
toolstripmenuitem showmenuitem = new toolstripmenuitem("显示界面");
showmenuitem.tag = "显示";
showmenuitem.click += new eventhandler(showmenuitem_click);
contextmenustrip.items.add(showmenuitem);
toolstripmenuitem sboutmenuitem = new toolstripmenuitem("关于");
sboutmenuitem.tag = "关于";
sboutmenuitem.click += new eventhandler(aboutmenuitem_click);
contextmenustrip.items.add(sboutmenuitem);
toolstripmenuitem exitmenuitem = new toolstripmenuitem("退出");
exitmenuitem.tag = "退出";
exitmenuitem.click += new eventhandler(existmenuitem_click);
contextmenustrip.items.add(exitmenuitem);
}
catch(exception exception)
{
throw new exception(exception.message);
}
}
/// <summary>
/// 右击任务栏图标
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void notifyicon_doubleclick(object sender, eventargs e)
{
try
{
if (this.windowstate == formwindowstate.normal)
{
this.windowstate = formwindowstate.minimized;
this.hide();
}
else if (this.windowstate == formwindowstate.minimized)
{
this.show();
this.windowstate = formwindowstate.normal;
this.activate();
}
}
catch (exception objexception)
{
throw new exception(objexception.message);
}
}
/// <summary>
/// 显示
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void showmenuitem_click(object sender, eventargs e)
{
try
{
this.show();
this.windowstate = formwindowstate.normal;
this.activate();
}
catch (exception objexception)
{
throw new exception(objexception.message);
}
}
/// <summary>
/// 关于
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void aboutmenuitem_click(object sender, eventargs e)
{
try
{
}
catch (exception objexception)
{
messagebox.show(objexception.message);
}
}
/// <summary>
/// 退出
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void existmenuitem_click(object sender, eventargs e)
{
try
{
if (messagebox.show("你确定要退出程序吗?","提示", messageboxbuttons.okcancel, messageboxicon.question, messageboxdefaultbutton.button2) == dialogresult.ok)
{
this.notifyicon.visible = false;
this.notifyicon.dispose();
this.dispose();
application.exit();
}
}
catch (exception objexception)
{
messagebox.show(objexception.message);
}
}
/// <summary>
/// 主窗体关闭
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void formmain_formclosing(object sender, formclosingeventargs e)
{
try
{
e.cancel = true;
this.hide();
this.notifyicon.dispose();
}
catch (exception objexception)
{
messagebox.show(objexception.message);
}
}
/// <summary>
/// 窗体大小变化
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void formmain_resize(object sender, eventargs e)
{
if (this.windowstate == formwindowstate.minimized)
{
this.showintaskbar = false;
this.hide();
this.notifyicon.visible = true;
}
}
}
}
系统开机自启动应用程序
using microsoft.win32;
using system;
using system.diagnostics;
using system.windows.forms;
namespace fountain.winform.notifydemo
{
public partial class formmain : form
{
/// <summary>
/// 构造方法
/// </summary>
public formmain()
{
initializecomponent();
}
/// <summary>
/// 窗体加载
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void formmain_load(object sender, eventargs e)
{
//一般不会放这个事件里去设置,而且设置时需要看下注册表要有没有写入成功有的话可以不用调了,偷懒多调一次也没事
string applictionname = process.getcurrentprocess().mainmodule.modulename;
string applictionpath = process.getcurrentprocess().mainmodule.filename;
#region 当前登陆用户的注册表启动项
registrykey registrykey = registry.currentuser.createsubkey(@"software\microsoft\windows\currentversion\run");
registrykey.setvalue(applictionname, applictionpath);
#endregion
#region 所有用户的注册表启动项
//registrykey registrykey = registry.localmachine.createsubkey(@"software\microsoft\windows\currentversion\run");
//registrykey.setvalue(applictionname, applictionpath);
#endregion
}
}
}到此这篇关于c#实现winform序在系统托盘显示图标和开机自启动的文章就介绍到这了,更多相关c# winform开机自启动内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论