在.NET中使进程窗口可见/不可见

时间:2011-07-26 22:23:00

标签: c# .net resize visible

我有我的申请,我正在开始一个新的流程。但我需要在整个过程中调整窗口大小以符合我的要求。但首先,该过程以正常大小打开窗口,然后调整大小以适应。这让它看起来很奇怪。那么我可以在不可见模式下使用winodw启动该过程然后调整大小然后使其可见吗?

ProcessStartInfo startInfo = new ProcessStartInfo("myApp.exe");
MyApp = Process.Start(startInfo);
Thread.Sleep(2000);
MoveWindow(MyApp.MainWindowHandle, 0, 380, 2040, 1150, true);

3 个答案:

答案 0 :(得分:3)

在.Start()调用隐藏它之前尝试startInfo.WindowStyle = ProcessWindowStyle.Hidden;?然后用你的代码来展示它?

像这样:

ProcessStartInfo startInfo = new ProcessStartInfo("myApp.exe");

startInfo.WindowStyle = ProcessWindowStyle.Hidden;

MyApp = Process.Start(startInfo);
Thread.Sleep(2000);
MoveWindow(MyApp.MainWindowHandle, 0, 380, 2040, 1150, true);

要显示窗口导入此方法:

[DllImport("user32.dll")]
private static extern bool ShowWindow(IntPtr hwnd, int nCmdShow);

然后在MoveWindow函数后调用它:

ShowWindow(MyApp.MainWindowHandle, 5);

答案 1 :(得分:1)

根据http://msdn.microsoft.com/en-us/library/ms633548%28v=vs.85%29.aspx,您应该使用不属于您的Windows的ShowWindowAsync来避免不稳定的结果。

答案 2 :(得分:0)

考虑到您正在使用正在加载进程的面板。 你可以使用这行代码

 ProcessStartInfo info = new ProcessStartInfo();
 Process p = new process(); // you can also use System.Diagnostics.Process
 ProcessStartInfo info = new ProcessStartInfo();
 info.FileName = // your Process
 info.Arguments = "Your Argument";
 info.UseShellExecute = true;
 info.CreateNoWindow = true;
 info.WindowStyle = ProcessWindowStyle.Maximized; //this will make no effect, so optional
 info.RedirectStandardInput = false;
 info.RedirectStandardOutput = false;
 info.RedirectStandardError = false;
 p = System.Diagnostics.Process.Start(info);
 p.WaitForInputIdle();
 Thread.Sleep(10000);
 SetParent(p.MainWindowHandle, this.pnlAlpha.Handle);
 // You also need to use this line so that your window should be re-sized 
 MoveWindow(p.MainWindowHandle, 0, 0, yourPanel.Width, yourPanel.Height, true);


//Dont forget to add this globally 
[DllImport("user32.dll")]
static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

[DllImport("USER32.dll")]
private static extern bool MoveWindow(IntPtr hwnd, int x, int y, int cx, int cy, bool repaint);