5,534,119 members and growing! (17,704 online)
Email Password   helpLost your password?
Languages » C# » How To     Intermediate License: The Code Project Open License (CPOL)

Creating a Tasktray Application

By [ICR]

How to create an application that consists primarily of a tasktray icon
C# 3.0, C# 1.0, C# 2.0, C#Windows, .NET, .NET 3.0, .NET 1.1, .NET 2.0, Win2K, WinXP, Win2003, Vista, Visual Studio, Dev

Posted: 6 May 2007
Updated: 23 Sep 2007
Views: 14,495
Bookmarked: 80 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
21 votes for this Article.
Popularity: 5.19 Rating: 3.93 out of 5
1 vote, 4.8%
1
2 votes, 9.5%
2
2 votes, 9.5%
3
7 votes, 33.3%
4
9 votes, 42.9%
5
Screenshot - TaskTrayApp.png

Introduction

Quite often, people find themselves writing small applications that live mainly in the TaskTray as a TaskTray icon. Something that monitors something, updates something, little applications that run in the background but that they want to let the user know it's running.

To date, the method I've seen all around the Internet to achieve this is to create the configuration form (which in turn contains the NotifyIcon) and then hide the configuration form.

This, however, has several drawbacks, the main one being that the configuration form will flicker whenever you start the application. Getting it to start hidden can be a bit of a chore.

Fortunately, there is a better way to start an application as a NotifyIcon: using ApplicationContext.

Using the Code

Start by creating a WinForms project and editing the configuration form to suit your needs. Once you have done this, open up Program.cs. You should find that the last line looks like this:

Application.Run(new Form1());

This tells the Application to load and show Form1, and to keep the Application alive as long as Form1 is alive. But take a look at the overloads for Run. Not only does it accept Forms, but also ApplicationContexts.

Create a new class that inherits from ApplicationContext (we will refer to it as MyApplicationContext). Then replace the above line with:

Application.Run(new MyApplicationContext());

Inside the constructor for MyApplicationContex, you can insert the code to initialize the NotifyIcon. Mine looks something like this:

MenuItem configMenuItem = new MenuItem("Configuration", new EventHandler(ShowConfig));
MenuItem exitMenuItem = new MenuItem("Exit", new EventHandler(Exit));

NotifyIcon notifyIcon = new NotifyIcon();
notifyIcon.Icon = TaskTrayApplication.Properties.Resources.AppIcon;
notifyIcon.ContextMenu = new ContextMenu(new MenuItem[] 
    { configMenuItem, exitMenuItem });
notifyIcon.Visible = true;

You can then create a ShowConfig method to display the configuration window. Mine looks something like this:

Configuration configWindow = new Configuration();
void ShowConfig(object sender, EventArgs e)
{
    // If we are already showing the window, merely focus it.
    if (configWindow.Visible)
    {
        configWindow.Activate();
    }
    else
    {
        configWindow.ShowDialog();
    }
}

And for completeness, my Exit method looks like this:

void Exit(object sender, EventArgs e)
{
    // We must manually tidy up and remove the icon before we exit.
    // Otherwise it will be left behind until the user mouses over.
    notifyIcon.Visible = false;
    Application.Exit();
}

So you should now be able to create applications that start, live and die in the TaskTray without having to hack together a form to host the NotifyIcon.

History

  • 07/05/2007
    • Submitted article
  • 09/23/2007
    • Fixed formatting
    • Changed configWindow.Focus(); to configWindow.Activate();

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

[ICR]



Location: United Kingdom United Kingdom

Other popular C# articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 12 of 12 (Total in Forum: 12) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralThanks! [modified]memberDonMattéo14:47 29 Sep '07  
QuestionLeft Click Context MenumemberSkylinc10:32 27 Sep '07  
AnswerRe: Left Click Context MenumemberSkylinc11:29 27 Sep '07  
QuestionCool, only a gripe leftmemberCarlosMPerez23:27 25 Sep '07  
AnswerRe: Cool, only a gripe leftmember[ICR]22:50 26 Sep '07  
GeneralCoolmemberAndreo4:20 25 Sep '07  
GeneralClean.Simple.Straightforwardmemberdedgod5:58 9 Jul '07  
GeneralAwesome.memberMarc Leger9:43 27 Jun '07  
QuestionVery nice, but can I start a message-loop without any forms?memberdavid zha020:46 20 May '07  
AnswerRe: Very nice, but can I start a message-loop without any forms?member[ICR]23:52 20 May '07  
AnswerRe: Very nice, but can I start a message-loop without any forms?memberdavid zha02:08 21 May '07  
GeneralVery CoolmemberMike DiRenzo10:38 7 May '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 23 Sep 2007
Editor: Deeksha Shenoy
Copyright 2007 by [ICR]
Everything else Copyright © CodeProject, 1999-2008
Web17 | Advertise on the Code Project