0

**EDIT: When I put a breakpoint in my MinimizePlayer() method, and step through it, it minimizes the wmplayer process. But it does not without stepping through. I don't know why. **

I had a similar script on my Linux machine where I automated some tasks that I use every time I boot up. I was able to start Rhythmbox, begin playing my playlist, and minimize the process.

Well, I'm trying the same thing on my new Windows 10 machine, and I don't know how to minimize the wmplayer.exe process from my script.

This is what I have so far. It works fine, I just want wmplayer to be minimized:

using System;
using System.Diagnostics;
using System.Threading;
using System.Runtime.InteropServices;


namespace Playlist
{
    class Program
    {

        [DllImport("user32.dll")]
        private static extern bool ShowWindow(IntPtr hWnd, WindowShowStyle nCmdShow);

        private enum WindowShowStyle:uint
        {
            Hide = 0,
            ShowMinimized = 2, 
            Minimize = 6
        }

        static void Main(string[] args)
        {
            Run();
        }


        public static void Run()
        {

            String username = Environment.UserName;
            username = char.ToUpper(username[0]) + username.Substring(1);
            Console.WriteLine("Hello " + username);
            Thread.Sleep(2000);
            Console.WriteLine("Opening Playlist...");
            Thread.Sleep(2000);

            Process.Start("wmplayer.exe", "C:\\Users\\" + username + "\\Music\\A_ChillstepMix.mp3");
            //Thread.Sleep(2000);
            //Console.WriteLine("Opening your IDE...");
            //Thread.Sleep(2000);
            //Process.Start("C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Community\\Common7\\IDE\\devenv.exe");
            //Thread.Sleep(2000);
            MinimizePlayer();
            Thread.Sleep(2000);
            Console.WriteLine("Goodbye...");
            Thread.Sleep(2000);                
            System.Environment.Exit(0);   

        }       
        public static void MinimizePlayer()
        {
            Process[] ps = Process.GetProcesses();
            foreach(Process p in ps)
            {
                if(p.ProcessName.Contains("wmplayer"))
                {
                    IntPtr h = p.MainWindowHandle;

                    ShowWindow(h, WindowShowStyle.Minimize);
                }
            }
        }
    }
}
DevOpsSauce
  • 1,319
  • 1
  • 20
  • 52
  • 1
    Do you mean how to minimize windows size for process? If so you can try to check [this](https://stackoverflow.com/questions/30486724/setting-process-window-size-and-location-before-process-start) and [this](https://stackoverflow.com/questions/3321535/how-can-i-control-the-size-and-position-of-a-new-process-window-from-a-winforms) – George Alexandria Oct 07 '17 at 18:25
  • Sorry, but no, I don't think? I just want to minimize Windows Media Player when it starts. And the second example is using WinForms. I'm executing this in a Console app. – DevOpsSauce Oct 07 '17 at 18:29
  • Just look at the answer on the second link where you can use **Win32API** functions, `SetWindowPos` and `ShowWindow`. It doesn't matter console app or winforms – George Alexandria Oct 07 '17 at 18:35

2 Answers2

1

You can specify the WindowStyle of the process you are starting if you use the ProcessStartInfo object:

var psi = new System.Diagnostics.ProcessStartInfo();
psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Minimized;
psi.FileName = "wmplayer.exe";
System.Diagnostics.Process.Start(psi);
John Koerner
  • 37,428
  • 8
  • 84
  • 134
  • I have edited my question and code to show changes. It only works when I step through with the debugger. I don't know why. – DevOpsSauce Oct 07 '17 at 19:08
0

Ok, I got it. I needed to add Thread.Sleep() before the MinimizePlayer() method. Working code is as follows (I commented out the Visual Studio stuff while debugging to speed things up):

using System;
using System.Diagnostics;
using System.Threading;
using System.Runtime.InteropServices;


namespace Playlist
{
    class Program
    {

        [DllImport("user32.dll")]
        private static extern bool ShowWindow(IntPtr hWnd, WindowShowStyle nCmdShow);

        private enum WindowShowStyle:uint
        {
            Hide = 0,
            ShowMinimized = 2, 
            Minimize = 6
        }

        static void Main(string[] args)
        {
            Run();
        }


        public static void Run()
        {

            String username = Environment.UserName;
            username = char.ToUpper(username[0]) + username.Substring(1);
            Console.WriteLine("Hello " + username);
            Thread.Sleep(2000);
            Console.WriteLine("Opening Playlist...");
            Thread.Sleep(2000);

            Process.Start("wmplayer.exe", "C:\\Users\\" + username + "\\Music\\A_ChillstepMix.mp3");
            //Thread.Sleep(2000);
            //Console.WriteLine("Opening your IDE...");
            //Thread.Sleep(2000);
            //Process.Start("C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Community\\Common7\\IDE\\devenv.exe");
            Thread.Sleep(2000);
            Console.WriteLine("Minimizing Player...");
            Thread.Sleep(2000);
            MinimizePlayer();
            Thread.Sleep(2000);
            Console.WriteLine("Goodbye...");
            Thread.Sleep(5000);            
            System.Environment.Exit(0);   

        }       
        public static void MinimizePlayer()
        {
            Process[] ps = Process.GetProcesses();
            foreach(Process p in ps)
            {
                if(p.ProcessName.Contains("wmplayer"))
                {
                    IntPtr h = p.MainWindowHandle;

                    ShowWindow(h, WindowShowStyle.Minimize);
                }
            }
        }
    }
}
DevOpsSauce
  • 1,319
  • 1
  • 20
  • 52