WPF: Placing your WPF Application on the system tray March 9, 2009
Posted by tippu in : WPF , trackbackHi Guys,
After getting good response on one of my article on “Tray Notification”, I am now going to show you how to do the same thing on the Windows Presentation Framework (WPF). Adding It is straight forward as we do in the regular Windows Forms Application. To add the Tray notification to WPF, you just need to add the “System.Windows.Forms” assembly reference to your WPF project. Once you add the reference, you can create new instance of the “NotifyIcon” class and use it in your WPF application. Lets me show you how to do this in step by step.
Step 1: Create a blank WPF Application project
Step 2: Add reference to “System.Windows.Forms”
Step 3: Declare a global variable for NotifyIcon, ContextMenuStrip and ToolStripMenuItem
private System.Windows.Forms.NotifyIcon TippuTrayNotify;
private System.Windows.Forms.ContextMenuStrip ctxTrayMenu;
private System.Windows.Forms.ToolStripMenuItem mnuExit;
NotofyIcon – This is the class which is used to show the application icon on the system tray.
ContentMenuStrip – This is the class which is used to show the context menu when the user right clicks on the system tray icon.
ToolStripMenuItem – This is the class which is used to show the menu item where the user performs the action on the context menu.
Usually in windows forms application, we used to do this by dragging and dropping the icons for the above mentioned classed on the form itself and assign property to it. Here in WPF world, its little different. We need to do some of the stuff manually, so lets continue our WPF tray notification application.
Step 4:
Add the ToolstripMenuItem object to the ContextMenuStrip and then add the ContextMenuStrip to the NotifyIcon object. Assign the events for the menu item as shown in the sample code.
Enjoy WFP Programming…
- Tippu (Mubarak Musthafa)
Source Code:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace NotifyIconWPFSampl
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
private System.Windows.Forms.NotifyIcon TippuTrayNotify;
private System.Windows.Forms.ContextMenuStrip ctxTrayMenu;
private System.Windows.Forms.ToolStripMenuItem mnuExit;
private bool isAppExiting = false;
public Window1()
{
InitializeComponent();
//Create an instance of the NotifyIcon Class
TippuTrayNotify = new System.Windows.Forms.NotifyIcon();
// This icon file needs to be in the bin folder of the application
TippuTrayNotify.Icon = new System.Drawing.Icon(“Application.ico”);
//show the Tray Notify Icon
TippuTrayNotify.Visible = true;
//Create a object for the context menu
ctxTrayMenu = new System.Windows.Forms.ContextMenuStrip();
//Add the Menu Item to the context menu
System.Windows.Forms.ToolStripMenuItem mnuExit = new System.Windows.Forms.ToolStripMenuItem();
mnuExit.Text = “Exit”;
mnuExit.Click += new EventHandler(mnuExit_Click);
ctxTrayMenu.Items.Add(mnuExit);
//Add the Context menu to the Notify Icon Object
TippuTrayNotify.ContextMenuStrip = ctxTrayMenu;
}
void mnuExit_Click(object sender, EventArgs e)
{
isAppExiting = true;
this.Close();
TippuTrayNotify.Visible = false;
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
//When the application is closed, check wether the application is
//exiting from menu or forms close button
if (!isAppExiting)
{
//if the forms close button is triggered, cancel the event and hide the form
//then show the notification ballon tip
e.Cancel = true;
this.Hide();
TippuTrayNotify.BalloonTipIcon = System.Windows.Forms.ToolTipIcon.Info;
TippuTrayNotify.BalloonTipTitle = “Tippu Tray Notify”;
TippuTrayNotify.BalloonTipText = “Tippu Tray Notify has been minimized to the system tray. To open the application, double-click the icon in the system tray.”;
TippuTrayNotify.ShowBalloonTip(400);
}
}
}
}
|
|
|

Comments
The error occurs when you are tryig to hide the windows in the CLosing event.
Any idea, how to fix this bug!
I’d rather dispose the NotifyIcon when closing than just setting the Visibility property. This might be a source for a host of issues…
Cheers,
Philipp