C # - Minimize to tray at system startup

In my application, I have the ability to run the application when Windows starts. This works great. I also use it so that, while minimizing, the application is minimized in the system tray. Is there a way that I could automatically minimize automatically when starting at the same time as Windows? The only way I could think of is to get the time the system was on and use this data to decide if the machine was recently started. Obviously, there are many flaws in this theory. Anyone have any other ideas on how to do this?

+4
source share
4 answers

Insert a command line switch in your program that causes your program to flush to the tray. When you run the program at Windows startup, just turn on the switch.

http://msdn.microsoft.com/en-us/library/acy3edy3.aspx

+7
source

In your Properties form in WindowState, change to Minimize or in code:

//After this: InitializeComponent(); //Place this line: WindowState = FormWindowState.Minimized; 

Hope this help!

+3
source

Use a command line argument, for example. / Startminimised. In the application, check for the presence of this switch (using Environment.GetCommandLineArgs ) when the application starts, and automatically hide if the switch is present.

Then, in the "run at startup" option, make sure that the application is launched using this switch, for example. set the Run Registry myapp.exe /startminimised or Run Group option to myapp.exe /startminimised .

However, when the user launches your application, he will not (usually!) Indicate the switch, so the application will appear as a window.

+1
source

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(); //This function will set your app in the registry to run on startup. I'll explain this function below. MinimizeApp(parameter); } 

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.

0
source

All Articles