Previously used under the WinForm NotifyIcon, to WPF can not find, under WPF or directly with the WinForm in the NotifyIcon to achieve the smallest to the system tray
Define a NotifyIcon member:
null;
WindowState ws; Record form Status
Initialize NotifyIcon when loading a form:
This. NotifyIcon =NewNotifyIcon (); This. Notifyicon.balloontiptext ="Hello, File Monitor";//set the text to display when the program starts This. Notifyicon.text ="File Monitor";//text that is displayed when the mouse is clicked when minimized to the pallet This. Notifyicon.icon =NewSystem.Drawing.Icon (@"B.ico");//program icon This. notifyicon.visible =true; Notifyicon.mousedoubleclick+=Onnotifyicondoubleclick; This. Notifyicon.showballoontip ( +);
Add a right-click menu at the same time:
System.Windows.Forms.MenuItem M1 =NewSystem.Windows.Forms.MenuItem ("Open"); M1. Click+=M1_click; System.Windows.Forms.MenuItem m2=NewSystem.Windows.Forms.MenuItem ("Close"); M2. Click+=M2_click; System.windows.forms.menuitem[] M=Newsystem.windows.forms.menuitem[] {m1, M2}; This. Notifyicon.contextmenu =NewSystem.Windows.Forms.ContextMenu (m);
The events are:
voidM2_click (Objectsender, EventArgs e) { if(System.Windows.MessageBox.Show ("sure to exit?", "Application", Messageboxbutton.yesno, Messageboximage.question, messageboxresult.no)==messageboxresult.yes) {System.Windows.Application.Current.Shutdown (); } } voidM1_click (Objectsender, EventArgs e) { This. Show (); This. Activate (); }
To add an event to a form:
this. SizeChanged + = mainwindow_sizechanged ; this. Closing + = mainwindow_closing;
voidMainwindow_sizechanged (Objectsender, Sizechangedeventargs e) { if(WS = =windowstate.minimized) { This. Hide (); This. notifyicon.visible =true; } } voidMainwindow_closing (Objectsender, System.ComponentModel.CancelEventArgs e) {E.cancel=true; This. WindowState =windowstate.minimized; WS=windowstate.minimized; This. notifyicon.visible =true; This. Notifyicon.showballoontip ( -,"Note","Hello everyone, this is an example", Tooltipicon.info); }
Double-click the Drag tray popup form
Private void Onnotifyicondoubleclick (object sender, EventArgs e) { if (ws = = windowstate.minimized) { this. WindowState = windowstate.normal; This false ; } }
Well, maybe that's enough, and finally a complete code:
Public Partial classMainwindow:window { PublicMainWindow () {InitializeComponent (); icon (); //ensure that the form appears above. WSL =WindowState; This. SizeChanged + =mainwindow_sizechanged; This. Closing + =mainwindow_closing; } voidMainwindow_sizechanged (Objectsender, Sizechangedeventargs e) { if(WS = =windowstate.minimized) { This. Hide (); This. notifyicon.visible =true; } } voidMainwindow_closing (Objectsender, System.ComponentModel.CancelEventArgs e) {E.cancel=true; This. WindowState =windowstate.minimized; WS=windowstate.minimized; This. notifyicon.visible =true; This. Notifyicon.showballoontip ( -,"Note","Hello everyone, this is an example", Tooltipicon.info); } WindowState ws; WindowState WSL; NotifyIcon NotifyIcon=NULL; Private voidicon () { This. NotifyIcon =NewNotifyIcon (); This. Notifyicon.balloontiptext ="Hello, File Monitor";//set the text to display when the program starts This. Notifyicon.text ="File Monitor";//text that is displayed when the mouse is clicked when minimized to the pallet This. Notifyicon.icon =NewSystem.Drawing.Icon (@"B.ico");//program icon This. notifyicon.visible =true; Notifyicon.mousedoubleclick+=Onnotifyicondoubleclick; This. Notifyicon.showballoontip ( +); System.Windows.Forms.MenuItem M1=NewSystem.Windows.Forms.MenuItem ("Open"); M1. Click+=M1_click; System.Windows.Forms.MenuItem m2=NewSystem.Windows.Forms.MenuItem ("Close"); M2. Click+=M2_click; System.windows.forms.menuitem[] M=Newsystem.windows.forms.menuitem[] {m1, M2}; This. Notifyicon.contextmenu =NewSystem.Windows.Forms.ContextMenu (m); } voidM2_click (Objectsender, EventArgs e) { if(System.Windows.MessageBox.Show ("sure to exit?", "Application", Messageboxbutton.yesno, Messageboximage.question, messageboxresult.no)==messageboxresult.yes) {System.Windows.Application.Current.Shutdown (); } } voidM1_click (Objectsender, EventArgs e) { This. Show (); This. Activate (); } Private voidOnnotifyicondoubleclick (Objectsender, EventArgs e) { if(WS = =windowstate.minimized) { This. WindowState =Windowstate.normal; This. notifyicon.visible =false; } } Private voidWindow_statechanged (Objectsender, EventArgs e) {ws=WindowState; if(WS = =windowstate.minimized) { This. notifyicon.visible =true; } } Private voidButton_Click (Objectsender, RoutedEventArgs e) { This. Notifyicon.balloontiptext ="have new information"; } }
Using NotifyIcon under WPF