Click to See Complete Forum and Search --> : System Tray Application


Barguast
July 21st, 2005, 12:05 PM
I want to create an application that will start off in the system tray without any initial form being displayed. The problem is that I can't seem to create such an application without including a NotifyIcon control on a form.

Isn't there any way to start an application in the system tray without the use of a form? Obviously I could just create an invisible form and have the NotifyIcon control on there but it seems a little unnecessary. Isn't there any other way?

CoffeeMug
July 22nd, 2005, 01:57 AM
hi there,

I'm not sure if this is what you are looking for but if you put this in your Main() method, it should work...


NotifyIcon tray = new NotifyIcon();
// Used to grab the embedded bitmap
System.Reflection.Assembly assembly =
System.Reflection.Assembly.GetCallingAssembly();
// The bitmap
Bitmap trayBitmap = (Bitmap)Bitmap.FromStream
(assembly.GetManifestResourceStream("KeyStroke.MacJournal.ico"));
// Convert Bitmap to Icon
Icon trayIcon = Icon.FromHandle(trayBitmap.GetHicon());

tray.DoubleClick += new EventHandler(tray_DoubleClick);
tray.Icon = trayIcon;
trayIcon = null;
tray.Visible = true;

while(true)
{
Application.DoEvents();
}


You could then use something like a Context Menu to add a menu to your tray application and use the double click event to show the form, which would be something like:


private static void tray_DoubleClick(object sender, EventArgs e)
{
// Fire a new instance main form
Form1 mainForm = new Form1();
mainForm.Show();
// Hide the tray Icon
((NotifyIcon)sender).Visible = false;
}

gilly914
March 3rd, 2008, 03:01 AM
When the form first Loads, is there an alternate way of hiding the form, because
Application.DoEvents();
causes unwanted outcome when responding to user events...

this.Hide();
doesn't work, so
How else can i just hide the form from the users eyes???