Wpf enables the icon to be displayed on the system tray, and wpf icons on the system tray

Source: Internet
Author: User

Wpf enables the icon to be displayed on the system tray, and wpf icons on the system tray

During the previous wpf operation, I wanted to display the program running icon in the taskbar. The result showed that the system tray of wpf is different from that of winform. The previous method did not work.

Many of the information searched on the internet is winform, and there are few wpf documents.

Finally, I will sort out and share my work to help others and myself.

Some errors are inevitable in the article. please correct me. 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 {// tray policyicon; // register the AreaIcon attribute for the tray icon public static readonly DependencyP Roperty AreaIconProperty = DependencyProperty. register ("AreaIcon", typeof (ImageSource), typeof (GWindow); // Register the AreaText attribute, public static readonly DependencyProperty AreaTextProperty = DependencyProperty. register ("AreaText", typeof (string), typeof (GWindow); // registers the AreaVisibility attribute to display the hidden Tray Icon public static readonly DependencyProperty AreaVisibilityProperty = DependencyProperty. register ("AreaVisibility", typeof (bool), typeof (GWindow); // register the AreaMenuItems attribute, which is used to right-click the tray and choose 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); policyicon = new policyicon (); policyicon. tex T = AreaText; if (! DesignerProperties. GetIsInDesignMode (this) {policyicon. Icon = GetImageSource (AreaIcon);} policyicon. Visible = AreaVisibility; if (this. AreaMenuItems! = Null & this. areaMenuItems. count> 0) {policyicon. 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); policyicon. visible = false; policyicon. 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 used for front-end calling directly replaces Window with GWindow, And you can set properties later.

In AreaMenuItems, right-click the tray icon and choose settings
<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>

 

Program running Image

 

The source code has all been pasted out, so no additional settings will be set.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.