CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2005
    Posts
    4

    System Tray Application

    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?

  2. #2
    Join Date
    Mar 2005
    Location
    Netherlands, Assendelft
    Posts
    19

    Re: System Tray Application

    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...

    Code:
    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:

    Code:
    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;
    }

  3. #3
    Join Date
    Jul 2004
    Location
    Holy Land
    Posts
    306

    Re: System Tray Application

    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???
    Rate this post if you found it useful!
    10X, gilly914

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured