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.

Share:
13,608
Vishal
Author by

Vishal

Updated on June 04, 2022

Comments

  • Vishal
    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
    Vishal almost 13 years
    This sounds a logical solution. But the system is throwing an exception in ShowWindow. Not able to figure out why.
  • Cipi
    Cipi almost 13 years
    What 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
    Vishal almost 13 years
    The 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
    Cipi almost 13 years
    And you did put 1 instead of 5?
  • Vishal
    Vishal almost 13 years
    Yes 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
    Yahia almost 13 years
    According 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
    Cipi almost 13 years
  • Leandro Bardelli
    Leandro Bardelli almost 12 years
    Thanks a lot @Yahia, you must write the answer below. It was luck I look here.