如何使用PictureBox播放视频

对于winform设计者来说,我们如果想实现和网页相同的效果,真心感觉很难。为此,今天我们谈谈winform注册窗体如何使用视频当做背景,我们用到的控件为PictureBox。PictureBox 控件可以显示来自位图、图标或者元文件,以及来自增强的元文件、JPEG 或 GIF 文件的图形。在讲述这个方法之前先看下效果,如图1所示:


图1 效果展示图

我们如果想用PictureBox有几种方法,第一种是使用提前设计好的Gif图片,第二种是使用一组图片然后使用定时器循环播放图片,第三种也就是我们今天要讲的直接播放视频。PictureBox播放视频用法的是mciSendString方法,mciSendString是用于播放多媒体文件的API指令,其可以播放MPEG、AVI、WAV、MP3等等,下面我们来介绍一下它的使用方法:

第一、打开媒体文件

首先我们在winform上拖放一个PictureBox控件,设置其Dock属性为Fill,使其全部铺满。我们定义一个新的PictureBox,然后将刚才拖放的控件赋值为新的PictureBox。由于我们所需要播放的文件放在程序根目录指定目录下,为了防止播放视频找不到,我们需要判断目录下的文件目录是否存在 

path = System.Windows.Forms.Application.StartupPath + @"\Background";

if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

我们定义一个string mciCommand;用于赋值我们需要操作的指令。打开多媒体指令为“open " + path + " alias MyAVI”,接着赋值对应的控件  mciCommand + " parent " + PlayScreen.Handle.ToInt32() + " style child";【注:PlayScreen为新定义的控件名称

第二、设置播放窗体的大小

我们取当前播放控件的宽和高,代码如:Rectangle r = PlayScreen.ClientRectangle;mciCommand = "put MyAVI window at 0 0 " + r.Width + " " + r.Height;这样我们就设置好了视频的大小。

第三、播放视频

在实现播放视频前我们先了解下mciSendString播放视频的几个操作。

a)、全屏播放:
mciSendString("play movie fullscreen",buf,sizeof(buf),NULL);
b)、暂停播放。
mciSendString("pause movie",buf,sizeof(buf),NULL);
c)、停止播放。
mciSendString("close movie",buf,sizeof(buf),NULL);

d)、播放视频

mciSendString("play movie ",buf,sizeof(buf),NULL);

我们既然想要做个背景视频,那么我们需要我们的视频一直循环播放,那只需要在play movie 后面加上repeat即可实现循环播放视频。

为了能我们再次启动窗体的时候,视频可以正常播放,那我们需要在窗体关闭前将我们播放的视频关闭。

这样我们就实现我们想要的效果,接着你就可以往上面添你所需要的控件,设计你理想的注册窗体。完整代码如下:

   	#region 背景视频播放
        private class LibWrap
        {
            [DllImport(("winmm.dll"), EntryPoint = "mciSendString", CharSet = CharSet.Auto)]
            public static extern int mciSendString(string lpszCommand, string lpszReturnString, uint cchReturn, IntPtr hwndCallback);
        }
        private void PlayViedo()
        {
            PictureBox PlayScreen = new PictureBox();
            PlayScreen = this.pictureBox;
            string mciCommand;
            string path = string.Empty;
            path = System.Windows.Forms.Application.StartupPath + @"\Background";
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            path += @"\bg.wmv";
            mciCommand = "open " + path + " alias MyAVI";
            mciCommand = mciCommand + " parent " + PlayScreen.Handle.ToInt32() + " style child";
            LibWrap.mciSendString(mciCommand, null, 0, IntPtr.Zero);
            Rectangle r = PlayScreen.ClientRectangle;
            mciCommand = "put MyAVI window at 0 0 " + r.Width + " " + r.Height;
            LibWrap.mciSendString(mciCommand, null, 0, IntPtr.Zero);
            // LibWrap.mciSendString("play MyAVI", null, 0, IntPtr.Zero);
            LibWrap.mciSendString("play MyAVI repeat", null, 0, IntPtr.Zero);
        }
        #endregion
  	#region  防止再次启动窗体无法播放视频,窗体关闭前关闭播放视频
        private void registers_FormClosing(object sender, FormClosingEventArgs e)
        {
            LibWrap.mciSendString("close MyAVI ", null, 0, IntPtr.Zero);
        }
        #endregion



猜你喜欢

转载自blog.csdn.net/qq_21726707/article/details/70050624