2011-07-26 134 views
3

我有我的應用程序,我正在開始一個新進程。但我需要調整過程中的窗口大小以適應我的要求。但首先這個過程會以正常大小打開窗口,然後調整它的大小以適合它。這使它看起來很奇怪。那麼我可以在不可見模式下使用winodw啓動進程,然後調整大小並使其可見?使進程窗口可見/隱藏在.NET中

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

你在談論Windows窗體或Windows服務?你說服務,但是服務通常不包含UI。 – BentOnCoding

+0

也許它會更好,使myApp.exe採取一些命令行參數和調整自己 - 沒有等待,沒有奇怪的看 – Yahia

+1

在旁邊注意'Thread.Sleep(2000)'讓我畏縮。有沒有一種方法可以測試窗口是否加載並循環,直到發生這種情況? –

回答

3

試過startInfo.WindowStyle = ProcessWindowStyle.Hidden;之前.Start()調用來隱藏它嗎?然後用你的代碼來顯示它?

像這樣:

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); 
+0

這聽起來是合乎邏輯的解決方案。但系統在ShowWindow中拋出異常。無法弄清楚爲什麼。 – Vishal

+0

什麼是例外?試試這個'ShowWindow(MyApp.MainWindowHandle,1);'這應該適用於第一次顯示的窗口...下面是nCmdShow值的其餘部分,請嘗試一下:http://msdn.microsoft.com/ en-us/library/ms633548(v = vs.85).aspx – Cipi

+0

崩潰是因爲錯誤的DLL。我已經修復了這個問題,但仍然無法正常工作。即使在ShowWindow之後窗口也不顯示。 – Vishal

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