wyrd
Senior Contributor
For the life of me I cannot figure out how to start up the main application form as invisible. There has to be something I'm missing, because I cannot imagine this being a difficult thing to do.
public class Form1 : System.Windows.Forms.Form
{
static bool PreProcess()
{
// Do something here - keep in mind this is a static method
//so you cant refer to instance variables
}
[STAThread]
static void Main()
{
if (Form1.PreProcess())
Application.Run(new Form1());
}
}
public class FormVar : object{};
public class Form1 : System.Windows.Forms.Form
{
static bool PreProcess(FormVar vars)
{
// Do something to vars here
}
public Form1(FormVar vars):base()
{
// Use vars to initialize the form;
}
[STAThread]
static void Main()
{
FormVar vars = new FormVar();
if (Form1.PreProcess(vars))
Application.Run(new Form1(vars));
}
}
NotifyIcon ico = new NotifyIcon(); //create the icon
ico.ContextMenu = new ContextMenu(); //add some context menu to it
ico.Icon = new Icon(Application.StartupPath + @"\App.ico"); //set the icon
ico.Visible = true; //and finally make it visible
Application.Run(); //start a message loop for your application without a form
//so the app doesnt exit when the Main is done.
//Then of course you add some handlers and such to make your form appear
mutant said:If you don't want a form shown until the user clicks an option from the notify icon you can do this:
Then you will have to manually terminate the application using Application.Exit since no form is associated with the message loop.C#:NotifyIcon ico = new NotifyIcon(); //create the icon ico.ContextMenu = new ContextMenu(); //add some context menu to it ico.Icon = new Icon(Application.StartupPath + @"\App.ico"); //set the icon ico.Visible = true; //and finally make it visible Application.Run(); //start a message loop for your application without a form //so the app doesnt exit when the Main is done. //Then of course you add some handlers and such to make your form appear
Application.Run();
// Program will not get here until Exit() is called
PostProcess();
// In app main entry point.
YourForm form = new YourForm();
form.Visible = false;
Application.Run();
wyrd said:Hmm, interesting. Nothing ever flashes on my screen? And I'm only running an 800mhz athlon. Also, isn't a form created as hidden by default? (the form.visible = false should be unnecessary).
I looked at Jay1b's code, his solution is identical to mine (unless I missed something).