Tuesday, May 8, 2012

Myth Bookmark 2 : C# - Minimize WPF to System Tray in C#

Google Search: c#  WPF Minimize on Taskbar

The best solution found: http://ebbypeter.wordpress.com/2010/06/28/minimize-a-wpf-application-to-system-tray-in-c/

1. global variable for NotifyIcon

private System.Windows.Forms.NotifyIcon MyNotifyIcon;
2. initiate NotifyIcon. Declare/Initialize event to bring back App from taskbar
public MainWindow()
{
InitializeComponent();
MyNotifyIcon = new System.Windows.Forms.NotifyIcon();
MyNotifyIcon.Icon = new System.Drawing.Icon(
@"C:\Archive\Icon-Archive.ico");
MyNotifyIcon.MouseDoubleClick +=
new System.Windows.Forms.MouseEventHandler
(MyNotifyIcon_MouseDoubleClick);
}

3. Event/Button to minimize
private void btnQuit_Click(object sender, RoutedEventArgs e){   this.WindowState = WindowState.Minimized;}

4. Event bring back /restore App from taskbar
void MyNotifyIcon_MouseDoubleClick(object sender,
System.Windows.Forms.MouseEventArgs e)
{
this.WindowState = WindowState.Normal;

5. Balloon tips (mine not working. haven't fix yet [8/5/12])
private void Window_StateChanged(object sender, EventArgs e)
{
if (this.WindowState == WindowState.Minimized)
{
this.ShowInTaskbar = false;
MyNotifyIcon.BalloonTipTitle = "Minimize Sucessful";
MyNotifyIcon.BalloonTipText = "Minimized the app ";
MyNotifyIcon.ShowBalloonTip(400);
MyNotifyIcon.Visible = true;
}
else if (this.WindowState == WindowState.Normal)
{
MyNotifyIcon.Visible = false;
this.ShowInTaskbar = true;
}
}