Baran Topal

Baran Topal


May 2024
M T W T F S S
« Feb    
 12345
6789101112
13141516171819
20212223242526
2728293031  

Categories


Hiding an executable’s window when that executable starts to process in C#

baranbaran

I found this old C# code in one of my old backups. It was the case I wanted to hide an executable’s window and that executable has no relation with C# btw. It has a window that i want to hide with my C# code. Here you go.


        [DllImport("User32")] 
        private static extern int ShowWindow(IntPtr handle, int nCmdShow);
        private const int SW_HIDE = 0;

        private void button1_Click(object sender, EventArgs e)
        {

            ProcessStartInfo psi = new ProcessStartInfo();
            psi.FileName = "notepad.exe";

            Process p = Process.Start(psi);
            p.WaitForInputIdle() ;  //Wait until app is running - if one doesn't wait here then there may be no main window to hide
            ShowWindow(p.MainWindowHandle, SW_HIDE);
            
        }