C# NotifyIcon的使用

avatar
作者
筋斗云
阅读量:3

在C#中,NotifyIcon类用于在系统托盘中显示一个图标,并允许您与用户交互。以下是NotifyIcon类的一些常见用法:

  1. 创建NotifyIcon对象:
NotifyIcon notifyIcon = new NotifyIcon(); 
  1. 设置NotifyIcon的图标和文本:
notifyIcon.Icon = new Icon("icon.ico"); // 设置图标 notifyIcon.Text = "Notification"; // 设置文本 
  1. 将NotifyIcon添加到系统托盘:
notifyIcon.Visible = true; 
  1. 处理NotifyIcon的鼠标事件:
notifyIcon.MouseClick += NotifyIcon_MouseClick; private void NotifyIcon_MouseClick(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { // 左键单击事件处理 } else if (e.Button == MouseButtons.Right) { // 右键单击事件处理 } } 
  1. 显示菜单项:
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(); } 
  1. 显示气球提示:
notifyIcon.BalloonTipText = "Hello, World!"; notifyIcon.BalloonTipTitle = "Notification"; notifyIcon.ShowBalloonTip(5000); // 显示气球提示,持续5秒 
  1. 销毁NotifyIcon对象:
notifyIcon.Visible = false; notifyIcon.Dispose(); 

广告一刻

为您即时展示最新活动产品广告消息,让您随时掌握产品活动新动态!