1. Introduction
WPF (Windows Presentation Foundation) is a powerful framework for building desktop applications in Windows. It offers a wide range of controls that can be used to create user interfaces. One of the basic controls provided by WPF is the tray icon control, also known as the system tray or notification area. In this article, we will explore how to implement a tray icon in WPF using example code.
2. Setting up the project
2.1 Creating a new WPF project
To get started, open Visual Studio and create a new WPF project. Go to "File" > "New" > "Project" and select "WPF App (.NET Framework)" as the project template. Choose a suitable name and location for your project and click "Ok" to create it.
2.2 Adding required references
To use the tray icon control in WPF, we need to add a reference to the PresentationFramework
assembly. Right-click on the "References" node in the Solution Explorer, select "Add Reference", and then search for and select "PresentationFramework" from the list of available assemblies. Click "Ok" to add the reference to your project.
3. Implementing the tray icon
3.1 Creating the tray icon
To create the tray icon, we need to use the NotifyIcon
class from the System.Windows.Forms
namespace. Add the following code to the MainWindow.xaml.cs file:
using System.Windows.Forms;
using System.Drawing;
namespace WpfTrayIconExample
{
public partial class MainWindow : Window
{
private NotifyIcon trayIcon;
public MainWindow()
{
InitializeComponent();
InitializeTrayIcon();
}
private void InitializeTrayIcon()
{
trayIcon = new NotifyIcon();
trayIcon.Icon = new Icon("trayIcon.ico");
trayIcon.Visible = true;
trayIcon.Text = "My Tray Icon";
}
}
}
Explanation: In the above code, we create a private field trayIcon
of type NotifyIcon
. In the InitializeTrayIcon
method, we initialize the trayIcon
object by assigning it an icon, setting it to be visible, and setting the text to be displayed when the user hovers over the icon.
3.2 Handling tray icon events
To handle events related to the tray icon, we can use the Click
event and the ContextMenu
property. Add the following code inside the InitializeTrayIcon
method:
ContextMenu contextMenu = new ContextMenu();
MenuItem exitMenuItem = new MenuItem("Exit");
exitMenuItem.Click += (sender, e) =>
{
trayIcon.Dispose();
System.Windows.Application.Current.Shutdown();
};
contextMenu.MenuItems.Add(exitMenuItem);
trayIcon.ContextMenu = contextMenu;
trayIcon.Click += (sender, e) =>
{
// Handle tray icon click event
};
Explanation: The above code creates a context menu for the tray icon, with a single menu item named "Exit". When the "Exit" menu item is clicked, it disposes of the tray icon object and shuts down the application. The context menu is assigned to the ContextMenu
property of the tray icon. Additionally, an empty event handler is added for the Click
event of the tray icon, which can be used to handle the click event.
4. Customizing the tray icon
4.1 Adding a custom icon
To add a custom icon for the tray icon, you need to provide a valid icon file. You can create an icon file using a graphic design tool or find free icon resources online. Once you have the icon file, add it to your project's directory and set its Build Action property to "Content" and Copy to Output Directory to "Copy if newer". Then, update the path to the icon file in the code:
trayIcon.Icon = new Icon("path/to/trayIcon.ico");
Explanation: The code above sets the icon property of the tray icon to the icon file located at the specified path. Make sure to replace "path/to/trayIcon.ico" with the actual path to your icon file.
4.2 Adding tooltips
Tooltips provide additional information when the user hovers over the tray icon. Add the following code inside the InitializeTrayIcon
method:
trayIcon.BalloonTipText = "This is the tooltip text";
trayIcon.ShowBalloonTip(5000);
Explanation: The above code sets the BalloonTipText
property of the tray icon to the specified tooltip text and shows the balloon tip for 5 seconds using the ShowBalloonTip
method. Note that balloon tips are only supported on Windows Vista and later versions.
5. Conclusion
In this article, we have learned how to implement a tray icon in a WPF application using example code. We saw how to create the tray icon, handle events related to the tray icon, and customize it by adding a custom icon and tooltips. The tray icon is a useful component that allows applications to provide quick access and notifications to users without taking up space on the taskbar. By following the steps outlined in this article, you can easily add a tray icon to your WPF application.