How do I not have a Window displayed on launch? [duplicate]
Вопрос
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)?
Решение
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:
- http://msdn.microsoft.com/en-us/library/system.windows.forms.form.setvisiblecore.aspx
- https://stackoverflow.com/a/1732294/847363
- http://msdn.microsoft.com/en-us/library/system.windows.forms.form.showintaskbar.aspx
- http://msdn.microsoft.com/en-us/library/system.windows.forms.applicationcontext.aspx
- https://stackoverflow.com/a/3288810/847363
Другие советы
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,..