Reputation: 16829
In my application, I have an option to start the application when Windows is started. That works great. I also have it so that, when minimized, the application is minimized to the system tray. Is there a way that I could have it be automatically minimized when started up at the same time as Windows? The only way I could think of, is to retrieve the amount of time that they system has been on and use that data to decide whether the machine had recently started. Obviously there are a lot of flaws with that theory. Anybody have any other ideas as to how this could be done?
Upvotes: 4
Views: 6333
Reputation: 479
You can call your program with a parameter, for example "-minimized" and then handle that parameter in your program:
In your program.cs, handle the parameter, and then pass that parameter to Form1:
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
if (args.Length != 0){
Application.Run(new Form1(args[0]));
}
else
{
Application.Run(new Form1("normalState"));
}
}
In your Form1.cs, you can call a function with the passed parameter and minimize the app:
public Form1(string parameter)
{
InitializeComponent();
SetStartup(); //This function will set your app in the registry to run on startup. I'll explain this function below.
MinimizeApp(parameter);
}
For example, with this function i used, if you start the application with the -minimized parameter, then it will start minimized, a notifyicon pops up in the taskbar and a bubble saying the app is started and running in the background.
public void MinimizeApp(string parameter)
{
if (parameter == "-minimized")
{
this.WindowState = FormWindowState.Minimized;
notifyIcon1.Visible = true;
notifyIcon1.BalloonTipText = "Program is started and running in the background...";
notifyIcon1.ShowBalloonTip(500);
Hide();
}
}
The SetStartup function puts your program into the registry, so it'll run on startup.
private void SetStartup(){
Microsoft.Win32.RegistryKey key;
key = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
key.SetValue(AppName, Application.ExecutablePath.ToString());
string ApplicationPath = "\"" + Application.ExecutablePath.ToString() + "\" -minimized";
key.SetValue("MyApplicationName", ApplicationPath);
key.Close();
}
Right now, when you start your program with -minimized parameter, for example: "c:/programs/app.exe" -minimized then it will start minimized, and when you restart your computer, it also starts automatically minimized.
Upvotes: 0
Reputation: 559
In your Form "Properties" in WindowState change to "Minimized", or in code:
//After this:
InitializeComponent();
//Place this line:
WindowState = FormWindowState.Minimized;
Hope this help!
Upvotes: 3
Reputation: 180788
Implement a command line switch in your program that causes your program to minimize to the tray. When you start the program with Windows startup, just include the switch.
http://msdn.microsoft.com/en-us/library/acy3edy3.aspx
Upvotes: 8
Reputation: 74802
Use a command line argument, e.g. /startminimised. In your app check for the presence of this switch (using Environment.GetCommandLineArgs
) when the app starts, and automatically minimise if the switch is present.
Then in your "run on startup" option, ensure that the app is started with this switch e.g. set the Run registry key or Startup group shortcut to myapp.exe /startminimised
.
When the user runs your app, however, they won't (normally!) specify the switch, so the app will appear as a window.
Upvotes: 1