0

Possible Duplicate:
How to make the startup form initially invisible or hidden

My application has a System Tray and I don't want the Window appearing on launch.

How can I accomplish this?


This is a default Windows Form application.

I dragged over a notifyIcon and a contexualMenuStrip

When the Form is closed, the application quits (don't want that either). But when the application is launched, the Windows Form is also made visible. So how do I not launch the form on start up (and not quit on closing the window)?

Community
  • 1
  • 1
Cocoa Dev
  • 9,361
  • 31
  • 109
  • 177
  • please show some source code... what have you tried ? what is not working ? – Yahia Feb 09 '12 at 17:10
  • Can't you just set the `Visible` property of the start-up form to `false`? Or have the default window location set to hidden? I remember doing something like this but haven't touched WinForms in a while. – Yuck Feb 09 '12 at 17:15
  • There is no Visible property for the Form. – Cocoa Dev Feb 09 '12 at 17:31

2 Answers2

1

Basically you need to overwrite SetVisibleCore() to achieve that... another option is to set ShowInTaskbar to false and use ApplicationContext in Application.Run.

Some reference links and sample code:

Community
  • 1
  • 1
Yahia
  • 69,653
  • 9
  • 115
  • 144
  • One side effect of this approach is no initialization code is fired. for example: if i have a timer that i start in MainForm_Load it never gets enabled and never fires the Tick event. Ieven tried System.Timers.Timer just to make sure that its not just the Windows.Forms.Timer that behaves this way but results were the same. – Digvijay Feb 10 '12 at 11:57
0

The answer isn't to hide your main form, but not to show it / load it in the first place.

One way is to create a custom ApplicationContext:

public static class Program
{
    public static void Main(string[] arguments)
    {
        var context = new NotifyIconApplicationContext();
        context.Icon.Icon = new Icon(PATH_TO_ICON_FILE_HERE);
        context.Icon.Visible = true;    
        Application.Run(context);
    }
}

class NotifyIconApplicationContext : ApplicationContext
{
    public NotifyIconApplicationContext()
    {
        Icon = new NotifyIcon();
    }
    public NotifyIcon Icon { get; set; }
}

You can then attach handlers to your notification icon prior to the Application.Run(context) line to allow for things like opening your main form, showing a menu, etc,..

Rob
  • 45,296
  • 24
  • 122
  • 150