0

Microsoft Paint (mspaint.exe) will launch minimized with no problems. When launching a windows form I wrote in c# (myWinForm.exe) the Process.StartInfo.WindowStyle command gets ignored (always launched as normal window). Comments in code below detail this.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        LaunchProcess("mspaint.exe");
        LaunchProcess("myWinForm.exe");   // this process will not acknowledge the StartInfo.WindowStyle command (always normal window)
    }

    private void LaunchProcess(string filename)
    {
        Process myProcess = new Process();
        myProcess.StartInfo.FileName = filename;
        myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;  // how do I get a WinForm I wrote to acknowledge this line?
        myProcess.Start();
    }
}

How do I configure myWinForm to acknowledge ProcessWindowStyle when called from the Process.Start() command?

Zambis
  • 85
  • 1
  • 11

1 Answers1

1

This must be taken care of in the program you launch, it is not automatic. The information is available from the Process.GetCurrentProcess().StartInfo property. It WindowState property contains the requested window state. Modify the project's Program.cs file similar to this:

using System.Diagnostics;
...
    [STAThread]
    static void Main() {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        var main = new Form1();
        switch (Process.GetCurrentProcess().StartInfo.WindowStyle) {
            case ProcessWindowStyle.Minimized: main.WindowState = FormWindowState.Minimized; break;
            case ProcessWindowStyle.Maximized: main.WindowState = FormWindowState.Maximized; break;
        }
        Application.Run(main);
    }
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • I get error "Process was not started by this object, so requested information cannot be determined" how could you get Process.GetCurrentProcess().StartInfo! – Mohammad Nikravan Aug 16 '21 at 05:07