WPF minimum to System Tray

Source: Internet
Author: User

Minimize the number of WPF applications to the system tray? You can call system. Windows. Forms. policyicon. The following is the sample code:

View code

Public partial class mainwindow: window {private notifyicon; Public mainwindow () {initializecomponent (); this. policyicon = new policyicon (); this. policyicon. balloontiptext = "in system monitoring ...... "; this. policyicon. showballoontip (2000); this. policyicon. TEXT = "in system monitoring ...... "; this. policyicon. icon = new system. drawing. icon (@ "appicon. ICO "); this. policyicon. visible = true; // open the menu item system. windows. forms. menuitem open = new system. windows. forms. menuitem ("open"); open. click + = new eventhandler (show); // exit the menu item system. windows. forms. menuitem exit = new system. windows. forms. menuitem ("exit"); exit. click + = new eventhandler (close); // associate the tray control system. windows. forms. menuitem [] childen = new system. windows. forms. menuitem [] {open, exit}; policyicon. contextmenu = new system. windows. forms. contextmenu (childen); this. policyicon. mousedoubleclick + = new system. windows. forms. mouseeventhandler (o, e) => {If (E. button = mousebuttons. left) This. show (o, e) ;}) ;}private void show (Object sender, eventargs e) {This. visibility = system. windows. visibility. visible; this. showintaskbar = true; this. activate ();} private void hide (Object sender, eventargs e) {This. showintaskbar = false; this. visibility = system. windows. visibility. hidden;} private void close (Object sender, eventargs e) {system. windows. application. current. shutdown ();}}

During running, it is found that the program must be able to find the icon. Otherwise, an error is reported and the icon is not included in the program. An additional icon is required for the tray icon. Of course, this can be solved:

this.notifyIcon.Icon = new System.Drawing.Icon(@"AppIcon.ico");

Replace the preceding sentence with the following content, which means to read the program icon and use it as the tray icon.

this.notifyIcon.Icon = System.Drawing.Icon.ExtractAssociatedIcon(System.Windows.Forms.Application.ExecutablePath);

Run the program.

 

 

 

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.