转载地址:http://www.tuicool.com/articles/bUZFfqb

上次做wpf时想把程序运行的图标显示在任务栏,结果发现wpf的系统托盘和winform的不一样,以前的方法不管用了。

网上搜的好多都是winform的资料,wpf的很少。

最后我把我现在做好的整理分享下,方便别人,也方便自己。

文章难免有些错误,欢迎指正,下面代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Threading;
using Drawing = System.Drawing;
using System.Windows.Forms;
namespace WpfWin
{
public class GWindow : Window
{
//托盘
NotifyIcon notifyIcon;
//注册AreaIcon属性,用于托盘的图标
public static readonly DependencyProperty AreaIconProperty =
DependencyProperty.Register("AreaIcon", typeof(ImageSource), typeof(GWindow));
//注册AreaText属性,用于鼠标滑到托盘图标时显示的文字
public static readonly DependencyProperty AreaTextProperty =
DependencyProperty.Register("AreaText", typeof(string), typeof(GWindow));
//注册AreaVisibility属性,用于显示隐藏托盘图标
public static readonly DependencyProperty AreaVisibilityProperty =
DependencyProperty.Register("AreaVisibility", typeof(bool), typeof(GWindow));
//注册AreaMenuItems属性,用于托盘右键在单的列表
public static readonly DependencyProperty AreaMenuItemsProperty =
DependencyProperty.Register("AreaMenuItems", typeof(List<MenuItem>), typeof(GWindow), new PropertyMetadata(new List<MenuItem>()));
protected override void OnInitialized(EventArgs e)
{
base.OnInitialized(e);
notifyIcon = new NotifyIcon();
notifyIcon.Text = AreaText;
if (!DesignerProperties.GetIsInDesignMode(this))
{
notifyIcon.Icon = GetImageSource(AreaIcon);
}
notifyIcon.Visible = AreaVisibility;
if (this.AreaMenuItems != null && this.AreaMenuItems.Count > 0)
{
notifyIcon.ContextMenu = new ContextMenu(this.AreaMenuItems.ToArray());
}
}
public List<MenuItem> AreaMenuItems
{
get { return (List<MenuItem>)GetValue(AreaMenuItemsProperty); }
set { SetValue(AreaMenuItemsProperty, value); }
}
public ImageSource AreaIcon
{
get { return (ImageSource)GetValue(AreaIconProperty); }
set { SetValue(IconProperty, value); }
}
public string AreaText
{
get { return (string)GetValue(AreaTextProperty); }
set { SetValue(AreaTextProperty, value); }
}
public bool AreaVisibility
{
get { return (bool)GetValue(AreaVisibilityProperty); }
set { SetValue(AreaVisibilityProperty, value); }
}
protected override void OnClosing(CancelEventArgs e)
{
base.OnClosing(e);
notifyIcon.Visible = false;
notifyIcon.Dispose();
AreaMenuItems.Clear();
}
private static Drawing.Icon GetImageSource(ImageSource icon)
{
if (icon == null)
{
return null;
}
Uri iconUri = new Uri(icon.ToString());
return new Drawing.Icon(System.Windows.Application.GetResourceStream(iconUri).Stream);
}
}
}

前台调用时的代码,直接将Window  换成  GWindow,在后面就可以设置属性了

AreaMenuItems   中设置托盘图标右键菜单,自己设定
<loacl:GWindow x:Class="WpfWin.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:loacl="clr-namespace:WpfWin"
xmlns:forms="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
Title="MainWindow" Height="350" Width="525" AreaText="MainWindow" ShowInTaskbar="False" AreaVisibility="True"  AreaIcon="Image/clock.ico" Icon="Image/clock.ico">
<loacl:GWindow.AreaMenuItems>
<forms:MenuItem Text="Open" DefaultItem="True" />
<forms:MenuItem Text="Exit" />
</loacl:GWindow.AreaMenuItems>
<Grid/>
</loacl:GWindow>

程序运行图片

源码已全部贴出,就不另设下载地址了

wpf 让图标显示在系统托盘相关推荐

  1. win10系统托盘图标不见了_Win10右下角网络图标不见了|系统托盘中不显示网络图标...

    网络图标是系统托盘中众多图标中的一个,网络状态是否正常,一眼可知,同时也是很方便的网络设置入口,那么网络图标没有显示应该怎么办呢?这篇文章是PE吧给大家带来的解决Win10右下角网络图标不见了问题方法 ...

  2. qt修改程序图标名称_解决Qt应用程序添加icon图标,修改窗口图标以及添加系统托盘问题...

    一.Qt应用程序添加icon图标的方法: 首先,我们需要先准备两个文件,一个是icon图标,另一个是rc文件,我分别命名为"myApp.rc"和"soft.ico&quo ...

  3. C# 窗口最小化到托盘及右键图标显示菜单

    日常开发有时候需要实现窗口最小化到系统托盘,本文就来讲讲该如何实现winfrom最小化到系统托盘,本例子基于VS2019编写. 用C#开发winform桌面程序时,程序启动后,默认是显示在桌面而且在任 ...

  4. 自己动手写类似酷狗播放器(4)_系统托盘的显示

    任何一个应用程序,在开启后都会在任务栏的又下角显示一个图标,用来表示这个应用程序已经开启,比如QQ.那是如何做到 的呢?其实也是很简单,就是填充NOTIFYICONDATA结构体,这时候就需要对该结构 ...

  5. VC6系统托盘类|崩溃自动重建图标|HOOK窗口消息|气泡效果更新SDK

    解决制作托盘气泡提示中遇到的'NIF_INFO' : undeclared identifier VC6下实现托盘气泡提示的关键是要更新SDK,实际上应该就是要更新SHELLAPI.h,SHELL32 ...

  6. PS Tray Factory win7 系统托盘 软件用法

    PS Tray Factory 软件网址: http://www.pssoftlab.com/hide_system_tray_icons.phtml 这个软件是作用是可以 通过 Ctrl +ALT ...

  7. Tauri 应用篇 - 系统托盘

    基本配置 编辑 src-tauri/tauri.conf.json 文件 iconAsTemplate 是一个布尔值,用于确定图像是否表示 macOS 上的模板图像[1](为 true 时图像应仅包含 ...

  8. 让MSN图标显示在Windows 7系统任务栏中

    首先,打开Windows开始菜单找到Windows Live Messenger的快捷方式图标,然后鼠标右键点击选择打开其"属性"窗口.找到其中的"兼容性"标签 ...

  9. 【JavaFx实现系统托盘,去重AWT托盘乱码】

    [JavaFx实现系统托盘,去重AWT托盘乱码] 说明 Javafx实现系统托盘,默认使用的是awt的组件, 但是源码是utf-8,程序运行的时候有中文菜单就产生乱码,即使转码也无效,参考资料说Pop ...

  10. linux mint输入法托盘,linux mint12安装ibus之后,语言栏不跟随光标和系统托盘输入法图标不能显示问题解决...

    安装之后,ibus有两个问题:1,语言栏不能光标跟随:2,系统托盘上不能显示输入法图标 对第一个问题, 我看到这个解决方法,之后便感觉应该是这几个系统变量没有设置好的原因. 用echo 查看了一下这三 ...

最新文章

  1. 《那些年啊,那些事——一个程序员的奋斗史》——72
  2. python--从入门到实践--chapter 12 pygame_Alien_Invasion
  3. vue读取终端硬件信息_自助服务终端机主要特点及规格
  4. Java文件下载中文名不显示乱码
  5. 见过一个一个拉新地推没见过这么多一起推
  6. 20年前的吴恩达,藏在一个数据集里
  7. 如何获取Java用户输入?
  8. matlab求解集合覆盖问题,Set Cover Problem (集合覆盖问题)
  9. window docker TensorFlow环境搭建
  10. STL sort 函数实现详解 ZZ
  11. 图吧工具箱2021最纯净的电脑硬件工具箱
  12. 【信号处理】基于高阶统计量特征的通信系统中微弱信号检测附matlab代码
  13. 苹果logo_苹果 ARM Mac 发布会独特标志 Logo 亮相
  14. 计算机辅助翻译技术包含,计算机辅助翻译方法技术
  15. linux 韦恩图两个,nVenn:绘制广义的准比例维恩图
  16. 网络编程(wireshare抓数据包及分析、三次握手与四次挥手、数据库sqlite3及操作)笔记-day15
  17. 能解开assetbundle格式资源的UnityStudio工具
  18. 历史著作《万历十五年》的优秀读后感范文3200字
  19. i386和i686都是什么意思呢?
  20. 使用SonarTS创建进行typescript代码质量扫描

热门文章

  1. 【VRP】基于matlab改进的模拟退火和遗传算法求解车辆路径规划问题【含Matlab源码 343期】
  2. 人工智能ai应用高管指南_理解人工智能指南
  3. c语言双精度型输出小数位数_C语言的double与float类型最多只能默认输出小数位数都取6位?...
  4. Flutter状态管理学习手册(三)——Bloc
  5. HDU TIANKENG’s rice shop(模拟)
  6. android 中使用svg
  7. JAVA程序员的学习网站(2)
  8. 顶顶顶!! Xcode7.1下用CocoaPods安装Alamofire--swift下的第三方网络库
  9. 使用apache mod_env模块保存php程序敏感信息
  10. JakartaEE 文件的上传和下载