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.