上次做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>