转载自:https://www.cnblogs.com/celery94/archive/2010/10/26/1861371.html

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; // Iconpublic partial class MainWindow : Window
{NotifyIcon notifyIcon;public MainWindow(){InitializeComponent();}void click(object sender, RoutedEventArgs e){// Configure and show a notification icon in the system traythis.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的事件中添加关于弹出其他窗口的处理。

WPF 使用NotifyIcon控件相关推荐

  1. WPF查找子控件和父控件方法

    原文:WPF查找子控件和父控件方法 public List<T> GetChildObjects<T>(DependencyObject obj, string name) w ...

  2. WPF 动画显示控件

    原文:WPF 动画显示控件 当我们要显示一个控件的时候,不仅仅要显示这个控件,还要有动画的效果. 主要用到了DoubleAnimation类. public static void ShowAnima ...

  3. WPF的Timer控件的使用

    原文:WPF的Timer控件的使用 通过System.Threaing.Timer控件来实现"初始加载页面时为DataGrid的模版列赋初始值" System.Threaing.T ...

  4. Wpf使用Winform控件后Wpf元素被Winform控件遮盖问题的解决

    Wpf使用Winform控件后Wpf元素被Winform控件遮盖问题的解决 参考文章: (1)Wpf使用Winform控件后Wpf元素被Winform控件遮盖问题的解决 (2)https://www. ...

  5. WPF之复杂形状控件

    WPF之复杂形状控件 原文:WPF之复杂形状控件 有的时候想将一张图片变成一个按钮.当然这里不是单纯讲图片作为按钮的背景. 这两者是有区别的: 前者图片即按钮,比如你有一个空心的圆圈,当你点击中间空心 ...

  6. WPF 4 日历控件(Calendar)

    WPF 4 日历控件(Calendar) 原文:WPF 4 日历控件(Calendar) 在之前我已经写过两篇关于WPF 4 任务栏(Taskbar)相关的特性.相信自从VS2010 Beta 版放出 ...

  7. WPF Calendar 日历控件 样式自定义

    原文:WPF Calendar 日历控件 样式自定义 粗略的在代码上做了些注释 blend 生成出来的模版 有的时候 会生成 跟 vs ui界面不兼容的代码 会导致可视化设计界面 报错崩溃掉 但是确不 ...

  8. WPF 曲线图表控件(自制)(二)

    原文:WPF 曲线图表控件(自制)(二) 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/koloumi/article/details/775218 ...

  9. 【WPF】监听WPF的WebBrowser控件弹出新窗口的事件

    WPF中自带一个WebBrowser控件,当我们使用它打开一个网页,例如百度,然后点击它其中的链接时,如果这个链接是会弹出一个新窗口的,那么它会生生的弹出一个IE窗口来,而不是在内部跳到该链接. 如果 ...

最新文章

  1. iphone越狱神器
  2. 微软、华为海思、高通等 50 家公司源代码被泄露!
  3. 【攻防世界003】re-for-50-plz-50
  4. 很多女生都这么干!效果就是可以很快换电脑……
  5. 一顿家庭火锅让本不富裕的家庭雪上加霜......
  6. linux apache certbot,从操作系统软件包安装Certbot
  7. 【连载】如何掌握openGauss数据库核心技术?秘诀四:拿捏事务机制(3)
  8. PyTorch深度学习:60分钟入门(Translation)
  9. 基础学习:高斯混合模型GMM
  10. 拓端tecdat|R语言再保险合同定价案例研究
  11. IIS出现问题报CS0016
  12. 微电网逆变器VF控制simulink仿真模型。 采用解耦的电压电流双闭环控制, 输出电压电流波形质量良好
  13. 下载频道2013上半年超人气精华资源汇总---全都是免积分下载。
  14. [Bilingual]Klein四元群的四个例子Four examples of Klein four-group
  15. linux电子相册程序,基于Linux电子相册的
  16. 看日漫学日语:日漫里常看到的日语100句(建议收藏)
  17. 初识c语言加操作系统
  18. Matlab论文插图绘制模板第33期—等高线填充图(contourf)
  19. 多少秒算长镜头_电影中什么是长镜头画面(9个经典长镜头)
  20. 百度站长推送常见问题汇总,出现4xx,5xx等问题如何解决

热门文章

  1. LDAP命令介绍---ldappasswordmodify口令修改操作
  2. maxcompute 2.0复杂数据类型之array
  3. 127.0.0.1与localhost的区别
  4. 视频通信基础知识之采集
  5. Linux vi 编辑器常见命令的使用
  6. 几种在shell命令行中过滤adb logcat输出的方法
  7. FFmpeg 2.1 试用(新版支持HEVC,VP9)
  8. 初次接触GWT,知识点总括
  9. Android权限Uri.parse总结
  10. java框架mybatis配置文件总结一