C# Run application MINIMIZED at windows startup

后端 未结 5 1939
盖世英雄少女心
盖世英雄少女心 2020-12-10 08:59

I got the following code to run the application at windows startup:

    private void SetStartup(string AppName, bool enable)
    {
        string runKey = \"         


        
5条回答
  •  借酒劲吻你
    2020-12-10 09:50

    I have strugled with the same issue, and found a working solution:

    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();
        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();
        }
    
    }
    

提交回复
热议问题