Make process window visible/Invisble in .NET
13,608
Solution 1
Tried startInfo.WindowStyle = ProcessWindowStyle.Hidden;
before .Start() call to hide it? And then use your code to show it?
Like this:
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);
To show the window import this method:
[DllImport("user32.dll")]
private static extern bool ShowWindow(IntPtr hwnd, int nCmdShow);
Then call it after MoveWindow function:
ShowWindow(MyApp.MainWindowHandle, 5);
Solution 2
According to http://msdn.microsoft.com/en-us/library/ms633548%28v=vs.85%29.aspx you should use ShowWindowAsync for Windows you don't own to avoid erratic results.
Author by
Vishal
Updated on June 04, 2022Comments
-
Vishal almost 2 years
I have my application, in which I am starting a new process. But I need to resize the window in the process to fit into my requirement. But first the process opens the window in normal size and then I resize it to fit. This make it look odd. So can I start the process with the winodw in invisible mode and then resize and then make it visible?
ProcessStartInfo startInfo = new ProcessStartInfo("myApp.exe"); MyApp = Process.Start(startInfo); Thread.Sleep(2000); MoveWindow(MyApp.MainWindowHandle, 0, 380, 2040, 1150, true);
-
Vishal almost 13 yearsThis sounds a logical solution. But the system is throwing an exception in ShowWindow. Not able to figure out why.
-
Cipi almost 13 yearsWhat is the exception? Try this
ShowWindow(MyApp.MainWindowHandle, 1);
This should work for the windows that are shown for the first time... Heres the rest of the nCmdShow values, try some: msdn.microsoft.com/en-us/library/ms633548(v=vs.85).aspx -
Vishal almost 13 yearsThe crash was because of the wrong DLL. I have fixed that but still it is not working. The window doesn't show even after the ShowWindow.
-
Cipi almost 13 yearsAnd you did put 1 instead of 5?
-
Vishal almost 13 yearsYes I tried that. I don't know if it has anything to do with ProcessStartInfo, the msn site says somethings about startupinfo, but I couldn't understand this.
-
Yahia almost 13 yearsAccording to msdn.microsoft.com/en-us/library/ms633548%28v=vs.85%29.aspx you should use
ShowWindowAsync
for Windows you don't own to avoid erratic results -
Cipi almost 13 yearsHey yeah, try ShowWindowAsync: msdn.microsoft.com/en-us/library/ms633549(v=vs.85).aspx
-
Leandro Bardelli almost 12 yearsThanks a lot @Yahia, you must write the answer below. It was luck I look here.