Baran Topal

Baran Topal


September 2022
M T W T F S S
« Feb    
 1234
567891011
12131415161718
19202122232425
2627282930  

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