The last time I did WPF, I wanted to show the icon of the program running in the taskbar, and the result was that the system tray of WPF was different from the WinForm, and the previous method didn't work.
Many of the online search is WinForm information, WPF is very few.
In the end, I am doing a good job of sharing the next, convenient for others, but also convenient for themselves.
The article inevitably some mistakes, welcome correct, the following code
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 {//Pallet NotifyIcon NotifyIcon; Register the Areaicon property, the icon for the tray public static readonly DependencyProperty Areaiconproperty = DEPENDENCYPROPERTY.R Egister ("Areaicon", typeof (ImageSource), typeof (Gwindow)); Registers the Areatext property for the text that is displayed when the mouse slides to the tray icon public static readonly DependencyProperty Areatextproperty = DEPENDENCYP Roperty. Register ("Areatext", typeof (String), typeof (Gwindow)); Register the Areavisibility property to display the hidden tray icon public static readonly DependencyProperty Areavisibilityproperty = Depen Dencyproperty.register ("Areavisibility", typeof (BOOL), typeof (Gwindow)); Register the Areamenuitems property, right-click on the list public s in the order trayTatic readonly DependencyProperty areamenuitemsproperty = Dependencyproperty.register ("Areamenuitems", typeof (L ist<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>) Getvalu E (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); } }}
The code at the front of the call will change the window to Gwindow, and you can set the properties later.
Areamenuitems Set the tray icon in the right-click menu, set your own
<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= "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>
program Run picture
The source code has been all posted, it is not set up another
WPF makes icons appear in the system tray