以软件【银行业会计人员技能训练系统】为例,如何使用NotifyIcon实现任务栏托盘菜单及气泡提示?
实现系统托盘方法如下:
1、向窗体中添加NotifyIcon控件和ContextMenuStrip控件;
2、为ContextMenuStrip控件添加子项;
3、选择NotifyIcon控件,在其属性窗口中将ContextMenuStrip属性设置为添加到窗体上的ContextMenuStrip控件,并为Icon属性设置图片。
代码如下:private void 显示ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Normal;
this.Visible = true;
}
private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void 设置ToolStripMenuItem_Click(object sender, EventArgs e)
{
shezhi f = new shezhi();
f.Show();
}
private void 更新ToolStripMenuItem_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("http://www.zhating.cn/index.php/post/62.html");
}
实现气泡提示方法如下:
在点击窗体 X 符号时,窗体不关闭,而是隐藏起来。在窗体的formclosing事件中加入如下代码private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (this.WindowState != FormWindowState.Minimized)
{
e.Cancel = true;//不关闭程序
//最小化到托盘的时候显示图标提示信息,提示用户并未关闭程序
notifyIcon1.ShowBalloonTip(3000, "程序最小化提示", "图标已经缩小到托盘,打开窗口请双击图标或者右键【显示】即可。", ToolTipIcon.Info);
this.Visible=false;
this.ShowInTaskbar = false;
notifyIcon1.Visible = true;
}
}
如图
如上,C#如何使用NotifyIcon实现任务栏托盘菜单及气泡提示?