|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionQuite 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 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 Using the CodeStart 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 Create a new class that inherits from Application.Run(new MyApplicationContext());
Inside the constructor for 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 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 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 History
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||