2016-01-22 118 views
0

Microsoft Paint(mspaint.exe)將啓動最小化,沒有問題。當我在c#(myWinForm.exe)中寫入一個Wi​​ndows窗體時,Process.StartInfo.WindowStyle命令被忽略(總是作爲普通窗口啓動)。以下代碼的評論詳細說明c#獲取WinForm以編程方式啓動時確認Process.StartInfo.WindowStyle

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

如何配置myWinForm以便在從Process.Start()命令調用時確認ProcessWindowStyle?

回答

1

這必須在您啓動的程序中處理,它不是自動的。這些信息可以從Process.GetCurrentProcess()。StartInfo屬性中獲得。它的WindowState屬性包含請求的窗口狀態。修改項目的Program.cs文件與此類似:

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