WPF控件--NotifyIcon
1.在什么地方找到NotifyIcon
普通的WPF控件基本上都是在该命名空间下:System.Windows.Controls,该命名空间在C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\PresentationFramework.dll下。也就是说是.net framework3.0之后才支持的。
那难道在WPF下就不能使用NotifyIcon了吗?
在MSDN上有以下关于通知图标的示例:http://msdn.microsoft.com/zh-cn/library/aa972170(VS.90).aspx
using System; using System.Windows; using System.Windows.Forms; // NotifyIcon control using System.Drawing; // Icon public partial class MainWindow : Window { NotifyIcon notifyIcon; public MainWindow() { InitializeComponent(); } void click(object sender, RoutedEventArgs e) { // Configure and show a notification icon in the system tray this.notifyIcon = new NotifyIcon(); this.notifyIcon.BalloonTipText = "Hello, NotifyIcon!"; this.notifyIcon.Text = "Hello, NotifyIcon!"; this.notifyIcon.Icon = new System.Drawing.Icon("NotifyIcon.ico"); this.notifyIcon.Visible = true; this.notifyIcon.ShowBalloonTip(1000); } }
其中包含NotifyIcon控件,请注意它的命名空间:System.Windows.Forms; ,该命名空间是在C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Windows.Forms.dll下。所以说此处使用的NotifyIcon控件其实是.net framework 2.0就提供的在winform下面是用的控件。
2.怎么使用NotifyIcon
App.xaml
<Application x:Class="NotifyIconStd.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" ShutdownMode="OnExplicitShutdown" Startup="ApplicationStartup" Exit="ApplicationExit"> <Application.Resources> </Application.Resources> </Application>
App.xaml.cs
using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Linq; using System.Windows; using System.Windows.Forms; using Application = System.Windows.Application; namespace NotifyIconStd { public partial class App : Application { private static NotifyIcon trayIcon; private void ApplicationStartup(object sender, StartupEventArgs e) { RemoveTrayIcon(); AddTrayIcon(); } private void AddTrayIcon() { if (trayIcon != null) { return; } trayIcon = new NotifyIcon { Icon = new System.Drawing.Icon("notifyIcon.ico"), Text = "NotifyIconStd" }; trayIcon.Visible = true; ContextMenu menu = new ContextMenu(); MenuItem closeItem = new MenuItem(); closeItem.Text = "Close"; closeItem.Click += new EventHandler(delegate { this.Shutdown(); }); MenuItem addItem = new MenuItem(); addItem.Text = "Menu"; menu.MenuItems.Add(addItem); menu.MenuItems.Add(closeItem); trayIcon.ContextMenu = menu; //设置NotifyIcon的右键弹出菜单 } private void RemoveTrayIcon() { if (trayIcon != null) { trayIcon.Visible = false; trayIcon.Dispose(); trayIcon = null; } } private void ApplicationExit(object sender, ExitEventArgs e) { RemoveTrayIcon(); } } }
该示例程序程序只是在程序启动时,同时启动了一个NotifyIcon,没有其他主程序界面,可以在MenuItem的事件中添加关于弹出其他窗口的处理。
Celery 标签: WPF
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~