C#之使用NotifyIcon实现任务栏托盘菜单,图标闪烁效果及气泡提示

2023-06-12,,

很多程序是只需要后台运行的,甚至不需要自己的应用界面。NotifyIcon提供了程序在任务栏的显示功能

程序下载链接如下:

http://download.csdn.net/detail/u010312811/9483473

1.创建一个项目,向窗体中添加NotifyIcon控件和ContextMenuStrip控件;

2.为ContextMenuStrip控件添加子项;

3.选择NotifyIcon控件,在其属性窗口中将ContextMenuStrip属性设置为添加到窗体上的ContextMenuStrip控件,并为Icon属性设置图片。

注:必须为NotifyIcon控件的Icon属性设置图标,否则是看不到的

代码:

         private void 显示ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Visible = true;
} private void 设置ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Visible = false;
} private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}

效果图:

 2.图标的闪烁效果

在图标闪烁中提到,给NotifyIcon赋予一个ICON可以控制使其在任务栏显示,闪烁的效果是加上一个空白的图标,正常图标与空白图标的切换进而实现闪烁的效果。

注:不能使用清除icon的方法,否则图标是在该位置清除,会引起其他图标的移动,空白图标起到占位的作用

代码如下:

说明:property是vs的一个资源管理功能,可以存储系统图标及一些常量

    private Icon blank = Properties.Resources.blank;
private Icon normal = Properties.Resources.normal;
private bool _status = true;
private bool _isBlink = false;

右键菜单控制图标是不是显示

    private void toolStripMenuItem1_Click(object sender, EventArgs e)
{
if (_isBlink == false)
{
_isBlink = true;
timer1.Enabled = true;
timer1.Start();
}
else
{
_isBlink = false;
timer1.Stop(); notifyIcon1.ShowBalloonTip(, "提示", "关闭闪烁效果!", ToolTipIcon.Info);
}
}

定时器中修改图标的状态,定时反转图标

    private void timer1_Tick(object sender, EventArgs e)
{
if (_status)
notifyIcon1.Icon = normal;
else
notifyIcon1.Icon = blank; _status = !_status;
}

气泡提示:

notifyIcon1.ShowBalloonTip(, "提示", "关闭闪烁效果!", ToolTipIcon.Info);

C#之使用NotifyIcon实现任务栏托盘菜单,图标闪烁效果及气泡提示的相关教程结束。

《C#之使用NotifyIcon实现任务栏托盘菜单,图标闪烁效果及气泡提示.doc》

下载本文的Word格式文档,以方便收藏与打印。