在C#中,NotifyIcon类用于在系统托盘中显示一个图标,并允许您与用户交互。以下是NotifyIcon类的一些常见用法:
NotifyIcon notifyIcon = new NotifyIcon();
notifyIcon.Icon = new Icon("icon.ico"); // 设置图标
notifyIcon.Text = "Notification"; // 设置文本
notifyIcon.Visible = true;
notifyIcon.MouseClick += NotifyIcon_MouseClick;
private void NotifyIcon_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
// 左键单击事件处理
}
else if (e.Button == MouseButtons.Right)
{
// 右键单击事件处理
}
}
ContextMenu contextMenu = new ContextMenu();
MenuItem menuItem = new MenuItem("Exit");
menuItem.Click += MenuItem_Click;
contextMenu.MenuItems.Add(menuItem);
notifyIcon.ContextMenu = contextMenu;
private void MenuItem_Click(object sender, EventArgs e)
{
// 退出应用程序
Application.Exit();
}
notifyIcon.BalloonTipText = "Hello, World!";
notifyIcon.BalloonTipTitle = "Notification";
notifyIcon.ShowBalloonTip(5000); // 显示气球提示,持续5秒
notifyIcon.Visible = false;
notifyIcon.Dispose();
辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读: C#静态变量的并发访问控制技术