阅读量:3
在C#中,NotifyIcon类用于在系统托盘中显示一个图标,并允许您与用户交互。以下是NotifyIcon类的一些常见用法:
- 创建NotifyIcon对象:
NotifyIcon notifyIcon = new NotifyIcon();
- 设置NotifyIcon的图标和文本:
notifyIcon.Icon = new Icon("icon.ico"); // 设置图标 notifyIcon.Text = "Notification"; // 设置文本
- 将NotifyIcon添加到系统托盘:
notifyIcon.Visible = true;
- 处理NotifyIcon的鼠标事件:
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对象:
notifyIcon.Visible = false; notifyIcon.Dispose();