Search code examples
c#processwindow-style

Start VLC maximized


I have this:

                    Process process = new Process();
                    string VLCPath = ConfigurationManager.AppSettings["VLCPath"];
                    process.StartInfo.FileName = VLCPath;
                    process.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
                    process.Start();

But it wont start vlc maximized, what am I doing wrong? It keeps starting vlc in the state that I closed it the last time..


Solution

  • You can set the Window state to maximize with Microsofts ShowWindow function.

    using System;
    using System.Diagnostics;
    using System.Runtime.InteropServices;
    using System.Threading.Tasks;
    
    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
    
    const int SW_MAXIMIZE = 3;
    
    var process = new Process();
    process.StartInfo.FileName = ConfigurationManager.AppSettings["VLCPath"];
    process.Start();
    process.WaitForInputIdle();
    
    int count = 0;
    while (process.MainWindowHandle == IntPtr.Zero && count < 1000)
    {
        count++;
        Task.Delay(10);
    }
    
    if  (process.MainWindowHandle != IntPtr.Zero)
    { 
        ShowWindow(process.MainWindowHandle, SW_MAXIMIZE);
    }
    

    You will need the while loop because WaitForInputIdle() only waits until the process has started up. So there is a high chance that the MainWindowHandle is not set yet.