上次做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>
程序運行圖片
源碼已全部貼出,就不另設下載地址了