|
C# NotifyIcon的使用
一、winform 中添加 timer 、contextMenuStrip、notifyIcon 控件。
二、notifyIcon属性中设置Icon图标(否则无法看见效果),ContextMenuStrip属性中选取 contextMenuStrip1。
三、contextMenuStrip中添加列表项
四、闪烁效果的实现。闪烁效果是利用时间控件,还有两张icon实现的,一张必须是空白的icon图片。
五、效果如下:
六、Form.cs中的代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TestNotifyIcon
{
public partial class Form1 : Form
{
private Icon blank = Properties.Resources.blank;
private Icon normal = Properties.Resources.upload;
private bool _status = true;
private bool _isBlank = false;
public Form1()
{
InitializeComponent();
}
private void 显示ToolStripMenuItem1_Click(object sender, EventArgs e)
{
this.Visible = true;
}
private void 隐藏ToolStripMenuItem1_Click(object sender, EventArgs e)
{
this.Visible = false;
}
private void 退出ToolStripMenuItem1_Click(object sender, EventArgs e)
{
Application.Exit();
}
//右键菜单空值图标是不是显示
private void 闪烁ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (_isBlank == false)
{
_isBlank = true;
timer1.Enabled = true;
timer1.Start();
}
else
{
_isBlank = false;
timer1.Stop();
//气泡提示
notifyIcon1.ShowBalloonTip(5000,"提示","关闭闪烁效果!",ToolTipIcon.Info);
}
}
//定时器中修改图标的状态,定时反转图标
private void timer1_Tick(object sender, EventArgs e)
{
if (_status)
{
notifyIcon1.Icon = normal;
}
else
{
notifyIcon1.Icon = blank;
}
_status = !_status;
}
}
}
七、案例项目:
链接:https://pan.baidu.com/s/1FuvO5kyKmSOmWxtSipQurw
密码:t6ky
|