轉載位址: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>
程式運作圖檔
源碼已全部貼出,就不另設下載下傳位址了