You can call your program with a parameter, for example, "-minimized", and then process this parameter in your program:
In program.cs, process the parameter, and then pass this 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 the function with the passed parameter and minimize the application:
public Form1(string parameter) { InitializeComponent(); SetStartup();
For example, when using this function, if you run the application with the -minimized parameter, it will begin to be minimized, a pop-up notification and a bubble will appear on the taskbar saying that the application is running and is 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 in the registry, so it starts when it starts.
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 using the -minimized parameter, for example: "c: /programs/app.exe" is minimized, it will be minimized, and when you restart your computer, it will also be automatically minimized.