Friday, November 05, 2010

Using .Net to create a System Tray app for Windows 7 - One Man’s Practical Approach and Guide

Simple-Talk - Creating Tray Applications in .NET: A Practical Guide

“This article is intended to be of interest to you in two different ways. If you just wish a practical guide on creating a system tray, then head straight for the section ‘The Tray Application Framework’, but if you’re more interested in actually using the application that we provide to illustrate the article, then click on ‘The Tray Application Framework‘. If you enjoy, and learn best by, following the whole process of how a particular system tray application was designed and built, then read on!

Contents

Introducing HostSwitcher: A Tray App for Some of You

  • Requirements
  • Instrumenting Your Hosts File
  • Usage
  • Features
  • Host Details View
  • Running on Windows 7
  • Execution Has Its Privileges

The Tray Application Framework

  • The Secret of the Tray
  • The Master Controller of a Tray App: The NotifyIcon
  • Tailoring Your Program Entry Point: The ApplicationContext
  • Rounding Out the ApplicationContext
  • Customizing WinForm Connections
  • WPF Can Play, Too!

Ensuring Only One Instance Executes: Mutual Exclusion

  • Technique 1: Native .NET Support
  • Technique 2: The Process Table
  • Technique 3: The Mutex Primitive
  • Strengths and Weaknesses of Mutex Techniques

If you are a .NET developer, you will probably be used to creating several different types of application. You’ll be familiar with either WPF or WinForms as the primary technology for desktop applications with a graphical user interface (GUI). You may also have required a console application for special needs such as batch processing or automated workflow. Finally, for more advanced applications, you may have needed to create a Windows service application, a type that, technically, does not even have a user interface.

There is one interesting type remaining: the system tray application.

This type of application is a kind of hybrid: it acts like a service, in that it sits in the background until you give it focus, then it acts like a GUI, allowing you to interact with it like a WinForms or WPF application.

You can find quite a number of articles on tray applications, just as I did when I needed to create my first one. I hit two problems during my self-edification: first, I did not find any single article that had all the necessary details. I collected bits and pieces from several to achieve a useful and usable solution. Second, many of these articles provide a kludge disguised as a solution: it is workable but certainly not the best way to create a system tray application.

In this article I’ll describe: the best practice for creating a tray application, complete with a tray application framework in C# that you can put to immediate use.

Because I didn’t like being told by one source that tray applications could exist only in WinForms, but not WPF, I’ll show you how you may choose to use either WinForms, WPF or both together with this framework. For those WPF purists, it turns out that there are independently developed libraries created by industrious developers that allow you to create strictly a WPF solution.

Conclusion

When I first had the thought “I need a small application to manage my host routing” I had little notion of the nuances and trade-offs involved in creating a well-designed tray app. And, just like the oft-stated comment that “If one person has a question, probably many others in the audience do, too,” I believe that if one developer realizes “Oh, that’s how it is done,” then there are probably many other developers who may appreciate the knowledge as well.

…”

It’s been a while since I‘ve seen a good walk-through/guide/example/etc of creating a System Tray based app…

No comments: