Taskbar Application in .NET
In this article I will explain how to remove main form dependency in .net applications and loading your program into the taskbar - like MSN Messenger and the other applications that you usually use in your daily life.
The first thing we will do to create a Taskbar Application is to modify the application so that it no longer depends upon the main form, but rather creates a notify icon when it starts. It is better to create a separate class for the startup code. In this class we'll be checking for multiple instances, creating the notify icon, etc.
Let's start off by checking for multiple copies of your application. A typical solution is to use Mutex to prevent running multiple copies in the first place. What happens in this solution is that the first instance of the application creates a lock on the mutex and keeps it locked as long as the application runs. Subsequent copies attempt to acquire the lock, fail and exit. Thus no other instances of the application will start an instant is running.
Public
Shared Sub
Main()
Dim
appSingleton As New
System.Threading.Mutex(False,
"TaskbarApplication")
If appSingleton.WaitOne(0,
False) Then
Application.Run()
appSingleton.Close()
End
Sub
That covered, let me quickly cover themes. When you start the application from a class you'll need to enable Windows XP style theme for your forms. Otherwise they will have a classic windows look and feel. The .Net Framework makes this really simple; all you need to is call EnableVisualStyle in the Application.
Application.EnableVisualStyles()
After checking for multiple instances and setting visual styles, we will create the notify icon and then run the main message loop. Running an application without a form is almost the same as running it with a main window - only 2 changes are required. Instead of making the call Application.Run(FormName) we make a call to Application.Run(), i.e. without any form parameter, starting the message loop without any main form.
The second change is to handle the SessionEnded event. When the user logs off Windows while the application is running, Windows will request that the application to exit by raising the SessionEnded event. If we don't handle this event or don't respond to the event, Windows will assume that the application crashed. This will result in a dialog to the users telling them that the application is not responding and will request to terminate it.
So in order to avoid getting that message, you need to close your application when this event is raised.
AddHandler
Microsoft.Win32.SystemEvents.SessionEnded, _
New
Microsoft.Win32.SessionEndedEventHandler( _
AddressOf
AppMain.SystemEvents_SessionEnded)
The code above will attach the event handler.
Private
Shared Sub
SystemEvents_SessionEnded(_,_)
If (Not
MainWindow Is Nothing)
Then
MainWindow.Close()
End If
AppIcon.Visible = False
Application.Exit()
End
Sub
In the code above, MainWindow is a local instance of the form that is being currently displayed. Since this is a TaskBar application, we have to check if a form is being displayed, and if it exists, we close it. AppIcon is the Notify Icon. We remove that from the Taskbar and close the application calling Application.Exit.
Let us now create our Notification Icon. Add an object of type NotifyIcon. This is found in System.Windows.Forms. Name it as AppIcon (as used above in the SessionEnded event handler).
Add an icon to the project and then write a method to initialize the notify icon and call that method from the main before starting the application message loop.
Private
Shared Sub
InitializeNotifyIcon(ByVal iconLocation
As String)
AppMain.AppIcon = New NotifyIcon
AppMain.AppIcon.Icon = Global.TaskbarApplication.My.Resources.Icon
AppMain.AppIcon.Visible = True
Dim menu As
New ContextMenu
Dim itemOpen As
New MenuItem("Open")
itemOpen.DefaultItem = True
AddHandler itemOpen.Click, _
New
EventHandler(AddressOf ApplicationMain.OpenClick)
menu.MenuItems.Add(itemOpen)
Dim item As
New MenuItem("-")
menu.MenuItems.Add(item)
Dim itemExit As
New MenuItem("Exit")
AddHandler itemExit.Click, _
New
EventHandler(AddressOf ApplicationMain.ExitClick)
menu.MenuItems.Add(itemExit)
AppMain.AppIcon.ContextMenu = menu
AppIcon.Text = "Taskbar Application"
AddHandler AppMain.AppIcon.DoubleClick, _
New
EventHandler(AddressOf
ApplicationMain.IconDoubleClick)
End
Sub
We pass in the Icon Name to the method, which initializes our AppIcon object and sets the icon to the one we set, and makes it visible.
The important part is adding the context menu to the notification icon and handling mouse events. In the code above we add to MenuItems; Open and Exit, and add respective event handlers. You would also notice that there is another MenuItem "-" that is added between Open and Exit. This is a non-clickable line that'll appear in the context menu.
After adding the context menu to the AppIcon object, we set our taskbar icon's text - that'll appear when the user hovers the mouse over it, and add an event handler to handle double click on the icon.
The next step is to create our Event Handlers. Let us write a method to show the main application form.
Private
Shared Sub
ShowApplication()
If
AppMain.MainWindow Is
Nothing Then
AppMain.MainWindow = New frmMain
AppMain.MainWindow.Show()
End If
AppMain.MainWindow.Visible = True
End
Sub
This function checks to see if we've initialized our main form previously, if we've done so, then it simply sets its visible property to True, otherwise it initialized the main form and loads it using Form.Show() method.
We will make a call to ShowApplication from our event handlers for the Open command on the Context Menu as well as in the Event Handler for the Double Click on the notify icon. The code for the Exit command is the same as that used in SessionEnded event handler.
That's all there is to it! So let's test our application.

The icon to the left is our Taskbar Application.

Right-clicking opens our Context-Menu.

Click Open to open our application form.
Using Timer control you can periodically check various things and display more notification icons; like when outlook checks e-mails and displays a message notification in the taskbar when a new mail arrives. Furthermore you can add more items to the Context Menu allowing user full customizability from the taskbar. This is all for now. I hope you enjoyed the article as much as I did explaining it. You can download sample Taskbar Application Visual Studio .NET project, used in this tutorial.
Related articles:
1. Make Your Own Web Browser!
2. Introduction to Microsoft Visual Studio Express Editions
3. Notifying Users in Taskbar Application
4. Taskbar Notification with Fade In & Fade Out Effect
5. Taskbar Notification with Irregular Shaped Forms
6. Common Dialog Classes
7. Menus And MDI Applications















