2016-01-22 116 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); 
    }