1

I want to launch an application minimized or not depending on it was launched by system on startup (Windows) or not minimized if it was launched by user (double clicked on it).

I've made a converter program so far which will open in windows start up. I've used this to success.

RegistryKey rk = Registry.CurrentUser.OpenSubKey 
("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true); 
rk.SetValue("MyApplicationName", Application.ExecutablePath);
rk.Close();
rk.Dispose();

I've only put these codes to startup. I don't check if it's already in regedit because its not adding if it's already there. After I've set these codes to my program I've restart my computer and my program came at startup but it come in center of my screen like normal startup. Can I check if my program is started by windows on startup and set it to startup? Normally my program hides itself to system tray if I click "x" on top right corner. I've to right click->exit to actually close my program.

My question is: Is there any way to check how was launched by the system (Windows) or by the user?

Gonzo345
  • 1,133
  • 3
  • 20
  • 42
  • Create a shortcut to your program, a .lnk file. Which lets you set the Run property to "Minimized". Start that .lnk file in the registry key instead. Using a command line argument would be another way. – Hans Passant Jun 22 '18 at 09:05
  • For getting a parameter into the registry key you should take a look at: https://stackoverflow.com/questions/15123421/start-application-with-parameters-on-windows-start – Oliver Jun 22 '18 at 13:21

2 Answers2

1

As they are both started with the same user it is going to be hard to detect. As an alternative, you could have a parameter in your application telling it to start reduced or not. By default it opens the window and if the parameter is set you do not show it. You will just have to add this parameter in the execution command stored in the registry.

Here are some code:

Program.cs, you look if the -minimized argument is provided and you pass this information to your Form class

using System;
using System.Linq;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    static class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1(args.Contains("-minimized")));
        }
    }
}

Form1.cs: if the boolean minimized is true, start minimized

using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1(bool minimized)
        {
            InitializeComponent();
            if (minimized)
            {
                WindowState = FormWindowState.Minimized;
            }
        }
    }
}

Then you can start your program providing it the -minimized argument:

.\WindowsFormsApp1.exe -minimized or not .\WindowsFormsApp1.exe

asidis
  • 1,374
  • 14
  • 24
  • I'll be honest. I'm still junior developer so I've no clue what you have just told me :) while I read your answer and also Gonzo's I realized maybe I can use a second exe and set the second one on start up? But it wont be effective .. – ShinoLexTazy Jun 22 '18 at 09:26
  • @ShinoLexTazy being junior (I'm) is not a excuse for not trying, no worries! 2 exes is not an option since it will be duplicated but the launching method. Try passing a parameter on the constructor (for example). Give a try having a look at Program.cs -> Main. Those are the arguments that the exe is receiving. – Gonzo345 Jun 22 '18 at 09:40
  • Sorry for late answer. I tried this method and its worked like charm. I'll save both codes in case I need. The checkbox method is good too since user can pick if he wants to open the program at start or not and also if he want the program come minimized or not at start. I've updated my code with the way you show for this program. – ShinoLexTazy Jul 02 '18 at 08:58
0

If I understood correctly, you want to launch your application minimized, right?

In that case, you might need to add this code:

If you're developing a WinForms app

this.WindowState = FormWindowState.Minimized;

If you're developing a WPF app

this.WindowState = WindowState.Minimized;
piet.t
  • 11,718
  • 21
  • 43
  • 52
Gonzo345
  • 1,133
  • 3
  • 20
  • 42
  • Yes that is right but "only" on startup. I mean if the user double click to my exe and want to open it he has to see the form. But if my program is opened by windows on windows start up I wan't it to put on system tray. As example take One Drive. It's opening as default on start up to system tray and if you click on it , it's opening normally. I need something similar to this I develop in WinForms – ShinoLexTazy Jun 22 '18 at 09:25
  • @ShinoLexTazy looks like this answer could help you https://stackoverflow.com/questions/3527555/how-can-you-determine-how-a-console-application-was-launched – Gonzo345 Jun 22 '18 at 09:31
  • problem solved. I've put a checkbox on my settings form and saved this value to regedit. On windows startup basicly I check this value and if its 1 I start up my notify icon. If it's unchecked I open up my program normally. – ShinoLexTazy Jun 22 '18 at 12:13
  • @ShinoLexTazy if you want your application to start normally when user starts it and minimized if it was started by windows your checkbox will not help. I updated my answer with some piece of code to show you how you can handle it with a parameter – asidis Jun 26 '18 at 11:20