Creating a background application with WPF

Creating a background application with WPF

Sometimes you need to have an application running in the background. Then you don’t want your MainWindow to be visible all the time. Instead you just want to have an icon in the notification area that allows the user to open up the MainWindow and to exit the application. In WPF you can do this easily with the help of Windows Forms’ NotifyIcon-class.

Just add references to System.Windows.Forms and System.Drawing to your WPF-project. Add also an icon-file (.ico) to your project resources. To do this just open the Resources.resx-file in the Properties-folder of your project. Select the Icon-resource and click the Add Resource button. I’ve named it MyIcon.

AddIconToResources

Then go to the App.xaml.cs and implement it like below. The trick is to never close the MainWindow, as a closed Window cannot be shown again. Instead cancel the closing and just hide it.

Now go in addition to the App.xaml and remove the Startup-Uri, so that when you start the application, only the NotifyIcon is added to the notification area

Done

Download the sample-project here: Background application

Share this post

Comments (2)

  • Apyn AOM

    Thanks again ! Very fast and easy solution
    Personnaly, I use this code to set an icon :
    _notifyIcon.Icon = Propeties.Resources.MyIcon;
    After having add the icon in Project Propeties, tab Resources ;)

    August 22, 2015 at 12:47 pm
    • Thomas Claudius Huber

      Jep, that’s more the Windows Forms way and great. Thanks. I’ve adjusted it in the Blog-post. :-)

      August 22, 2015 at 1:00 pm

Comments are closed.