Search code examples
c#google-chromeminimized

Run chrome in minimized mode


I am trying to open google chrome with C# but I don't want to see it when it opens. I tried to add

process.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;

but it does nothing. I found out that i can use parameters to lunch chrome like --new-window but beside new window, no other parameter is working. I tried to lunch it in other locations on the screen but it has no effect either. I tried to change the window size but it does nothing too.

When I lunch my chrome it always open in maximized window (although I don't pass him this argument), I couldn't figure out why it always run in maximized mode but I think that this is the reason why I cant move it or re-size the window.

How can I run chrome without seeing the window? minimized or even lunch it out of the screen will be great. Thank you for your help


Solution

  • At first be sure that chrome is loaded completely(its window is loaded, I don't have any idea about it in this time) then use this code to minimize it.

    private const int SW_SHOWMINIMIZED = 2;
    
    [DllImport("user32.dll")]
    private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
    
    private void hideChrome()
    {
        Process proc;
        foreach (Process process in Process.GetProcesses())
        {
            if (process.ProcessName.Equals("chrome"))
                proc = process;
        }
    
        IntPtr hWnd = proc.MainWindowHandle;
        if (!hWnd.Equals(IntPtr.Zero))
        {
            ShowWindowAsync(hWnd, SW_SHOWMINIMIZED);
        }
    }