Embedding Windows Media Player in Windows Form

1. Need to add reference to WMPLib and AxWMPLib

On the Toolbox right click on Components and select Choose Items….
Select COM Components tab.
Find Windows Media Player, enable and click on OK.
In the Toolbox the Windows Media Player should appear. Drag that item to your form. This should create the required references.

2. Code example

using WMPLib;
using AxWMPLib;

namespace test
{
   public partial class Test : Form
   {
     AxWindowsMediaPlayer movies;
    
    private void Test_Load(object sender, EventArgs e)
    {
           movies = new AxWindowsMediaPlayer();
           movies.PlayStateChange +=
              new AxWMPLib._WMPOCXEvents_PlayStateChangeEventHandler(movies_PlayStateChange);
           movies.Enabled = true;
           movies.Dock = DockStyle.Fill;
           this.Controls.Add(movies);
           movies.uiMode = "none";
           movies.settings.mute = true;

           movies.Enabled = true;
           movies.URL = @"c:\movie.avi";
           movies.Ctlcontrols.play();
    }

    private void movies_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
    {
        // 1 = Stopped
        // 2 = Paused
        // 3 = Playing
        // 8 = Media ended
        switch (e.newState)
        {
            case (int)WMPLib.WMPPlayState.wmppsStopped: // Console.WriteLine("Stopped");
                break;
            case (int)WMPLib.WMPPlayState.wmppsPaused: // Console.WriteLine("Paused");
                break;
            case (int)WMPLib.WMPPlayState.wmppsPlaying: // Console.WriteLine("Playing");
                break;
            case (int)WMPLib.WMPPlayState.wmppsMediaEnded: // Console.WriteLine("Media ended");
                break;
        }
    }

   }
}

Mapping DOS Device

When trying to work out how to map an LPT1 port to file so I could capture the output print jobs I had a heck of a time of it. Then I found this code on Stack Overflow which showed how to map a drive to another path. I only had to tweak it to remove the “:” at the end of the device as the LPT1 is just “LPT1”, not “LPT1:”. Also changed the input from a character, which is drive letters, to a string so it would accept “LPT1”.

    public static class Subst {

        // Redirect a device to a path
        // MapDrive("z:","c:\temp\zdrive\")
        // MapDrive("LPT1","c:\temp\printer\file.txt")
        // Console.WriteLine("LPT1 mapped to:"+Subst.GetDriveMapping("lpt1"));

        public static void MapDrive(string device, string path) 
        {
            if (!DefineDosDevice(0, devName(device), path))
                throw new Win32Exception();
        }

        public static void UnmapDrive(string device) 
        {
            if (!DefineDosDevice(2, devName(device), null))
                throw new Win32Exception();
        }

        public static string GetDriveMapping(string device) 
        {
            var sb = new StringBuilder(259);
            if (QueryDosDevice(devName(device), sb, sb.Capacity) == 0) 
            {
                // Return empty string if the drive is not mapped
                int err = Marshal.GetLastWin32Error();
                if (err == 2) return "";
                throw new Win32Exception();
            }
            return sb.ToString().Substring(4);
        }

        private static string devName(string device) 
        {
            return device.ToUpper();
        }

        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern bool DefineDosDevice(int flags, string devname, string path);
        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern int QueryDosDevice(string devname, StringBuilder buffer, int bufSize);
    }

TeamWalrus Software

Welcome to the TeamWalrus software website (www.teamwalrus.info). On this site I will make available utilities that I create that I think may be useful to others.

If you do find any of the packages useful I’d love to hear back from you on how you are using them. I’d also love to hear if you have feedback on how the program can be improved, or if you have an idea for something else which may be useful.