wpf 让图标显示在系统托盘

本文介绍如何在WPF应用程序中实现系统托盘图标,并提供了一个示例类GWindow,展示了如何设置托盘图标的属性,包括图标、提示文本、可见性和右键菜单。

转载地址: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>

程序运行图片

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值