notifyicon

C#中notifyicon用法是什么

小亿
145
2023-08-03 01:58:38
栏目: 编程语言

在C#中,NotifyIcon类用于在系统托盘中显示一个图标,并提供与图标相关的功能。下面是NotifyIcon类的一些常见用法:

  1. 创建和初始化NotifyIcon对象:
NotifyIcon notifyIcon = new NotifyIcon();
notifyIcon.Icon = new Icon("icon.ico"); // 设置图标
notifyIcon.Text = "Tooltip text"; // 设置鼠标悬停时显示的文本
notifyIcon.Visible = true; // 显示图标
  1. 添加右键菜单:
ContextMenu contextMenu = new ContextMenu();
MenuItem menuItem = new MenuItem("Exit");
menuItem.Click += (sender, e) => { Application.Exit(); }; // 添加点击事件
contextMenu.MenuItems.Add(menuItem);
notifyIcon.ContextMenu = contextMenu;
  1. 处理图标的鼠标事件:
notifyIcon.MouseClick += (sender, e) =>
{
if (e.Button == MouseButtons.Left)
{
// 左键点击事件
}
else if (e.Button == MouseButtons.Right)
{
// 右键点击事件
}
};
  1. 显示气球提示:
notifyIcon.BalloonTipTitle = "Title";
notifyIcon.BalloonTipText = "Message";
notifyIcon.BalloonTipIcon = ToolTipIcon.Info;
notifyIcon.ShowBalloonTip(5000);
  1. 设置双击事件:
notifyIcon.DoubleClick += (sender, e) =>
{
// 双击事件
};
  1. 控制图标的显示和隐藏:
notifyIcon.Visible = true; // 显示图标
notifyIcon.Visible = false; // 隐藏图标
  1. 处理程序退出时的清理工作:
Application.ApplicationExit += (sender, e) =>
{
notifyIcon.Visible = false; // 程序退出时隐藏图标
notifyIcon.Dispose(); // 释放资源
};

这些只是NotifyIcon类的一些常见用法,你可以根据自己的需求进一步扩展和定制。

0
看了该问题的人还看了