Monday, October 23, 2006

Creating System Tray Applications in Windows Forms .NET 2.0

This simple article explains how to implement the common features and functionalities required for a notification-based application in Windows Forms: starting up a Windows Forms application without showing a Form, using the NotifyIcon, bringing an existing Form to the front of the desktop, and closing the Form on minimize.

Create Icons At Runtime For The System Tray in .NET

This simple way of creating Icons for system tray at runtime using .NET. Here is the code sample for creating the Icon for sytem tray.

String TaskBarLetter;
// Create a graphics instance that draws to a bitmap
Bitmap bitmap = new Bitmap(16, 16);
SolidBrush brush = new SolidBrush(fontDialog1.Color);
Graphics graphics = Graphics.FromImage(bitmap);
// Draw then number to the bitmap using the user selected font
if (i != -1)
TaskBarLetter = i.ToString();
else
TaskBarLetter = "V";
graphics.DrawString(TaskBarLetter, fontDialog1.Font, brush, 0, 0);
// Convert the bitmap into an icon and use it for the system tray icon
IntPtr hIcon = bitmap.GetHicon();
Icon icon = Icon.FromHandle(hIcon);
notifyIcon1.Icon = icon;
//GetHicon creates an unmanaged handle which must be manually destroyed
DestroyIcon(hIcon);


For this application, we’ll need to create a new NotifyIcon, ContextMenuStrip, and a few MenuItems for the ContextMenuStrip. The icon is set, and the NotifyIcon is set to Visible = true. The NotifyIcon.Text property is used in order to display a ToolTip on mouse hover. when you want to get the application out of the System Tray you have to use a Context Menu. First you must associate both the form and the Notify Icon with this context Menu. Just go into the properties for both of those items and set the ContextMenuStrip property to the name of your ContextMenuStrip(in this case contextMenuStrip1).



Download The Sample Sourcecode>>

No comments:

ASP.NET MVC - Sport Facility Booking system

  The project relies on some external service providers. Thus accessing them via their API requires authentication. An API Key need to be su...