---
title: "在VC++2005中使用Windows Media Play ActiveX控件"
author: "utensil"
date: "2007-12-26T20:29:00Z"
tags: [".net", "F#", "MFC", "VC++", "Windows"]
slug: "vc-2005-windows-media-play-activex"
draft: false
lastmod: "2009-04-15T12:06:01Z"
id: 367434
url: "https://utensil.iteye.com/blog/367434"
---

VC++2005有许多东西变了，比如ActiveX控件的添加和控制——仅仅为了添加一个Windows Media Player控件，我就弄得头晕转向，按老办法做行不通不说，自己摸索了半天+上网搜索+去书店翻书都没有彻底。幸亏找到了这篇文章，才得以解决。感谢touzani大侠（[http://blog.csdn.net/touzani/](http://blog.csdn.net/touzani/) ）！

皿

原文如下：

 

以ActiveX 控件——媒体播放器为例，说明组件应用之功能强大和使用的简单方便。

 

媒体播放器 (Media Player) 是 DirectX Media 中的一个 ActiveX 控件，支持大多数标准流媒体格式的播放，如 ASF 、 AVI 、 MPEG 、 WAV 、 MIDI 、 MP3 、 WMA 等，但对 RM 和 MOV 支持得不够。

 

创建一个基于对话框的 MFC 应用程序 MPlay

为了能够在项目中使用媒体播放器控件，必须先将媒体播放器控件添加到对话框编辑器工具箱中。（这需要在你的系统中，已经正确安装了微软公司的媒体播放器）

 

1\. 添加 ActiveX 控件

(VC++ 6.0)

工程－添加到工程－ &gt; components and controls 选择 Windows Media Player

![http://p.blog.csdn.net/images/p_blog_csdn_net/touzani/303255/o_mplay.jpg](http://p.blog.csdn.net/images/p_blog_csdn_net/touzani/303255/o_mplay.jpg)

 

插入到工具箱 , 再拖到对话框中

( VC++2005方法)

1 打开工具箱窗口，在其空白区单击鼠标右键，在弹出菜单中选中“选择项”菜单项；也可以选 VS 的“工具 \\ 选择工具箱项”菜单项；在弹出的“选择工具箱项”对话框中，选择“ COM 组件”页；滚动到列表框的底部，选中“ Windows Media Player ”项，按“确定”钮，关闭对话框。系统会将一个“ Windows Media Player ”控件，加入到工具箱的对话框编辑器栏的底部。这样你就可以将其加入到对话框资源中了。

 

2 你也可以简单地在对话框资源中单击鼠标右键，在弹出菜单中选中“插入 ActiveX 控件”菜单项；在打开的“插入 ActiveX 控件”对话框中，滚动列表框，选择底部的“ Windows Media Player ”项，按“确定”钮，关闭对话框。则系统会将其自动加入到对话框资源中（但这样是不会在工具箱中添加对应控件的）。

)

#### 2 ）设计对话框

 

在对话框中调整好媒体播放器控件的位置和大小，打开属性窗口，设置其 ID 为 IDC\_MP 。去掉对话框中 “取消”钮，将“确定”钮中的文字改为“打开 (&O) ”，并修改其 ID 为 IDC\_LOAD （用来打开文件对话框，选择播放的流媒体文件，并将该文件的路径名传递给媒体播放器），而播放、暂停、停止等图形按钮控件中已经有了。  
在你为对话框加入媒体播放器控件后， MFC 会自动在资源文件 MPlay.rc 中添加对它的复杂初始化代码.

还可以按属性窗口工具条上的属性页按钮，在弹出的“ Windows Media Player 属性”对话框中，进行各种属性设置，例如选中“按比例伸展”和“全屏播放”复选框。

 

#### 3 ）添加控件变量

为了能使用媒体播放器控件，我们必须为该控件添加一个控件型类变量 m\_ctrlMPlayer;

同时给打开按钮添加了变量 m\_ctrlLoad.

 

#### （ 4 ）使用控件

这里所要做的工作，只是在 CMPlayDlg 类中，为“装入”按钮添加单击事件 BN\_CLICKED 的处理程序 OnBnClickedLoad ，并在该函数中打开公用文件对话框，获取用户选择的媒体文件名，并将该文件的路径传递给媒体播放器类对象：

 

VC++ 6.0 代码:

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)void CMPlayDlg::OnBnClickedLoad() ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) CFileDialogdlg(TRUE,NULL,L " \*.* " ,OFN\_FILEMUSTEXIST,  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) " ActiveStreamingFormat(\*.asf)|\*.asf| "  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) " AudioVideoInterleaveFormat(\*.avi)|\*.avi| "  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) " RealAudio/RealVideo(\*.rm)|\*.rm| "  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) " WaveAudio(\*.wav)|\*.wav| "  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) " MIDIFile(\*.mid)|\*.mid| "  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) " 所有文件(\*.\*)|\*.\*|| " );  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif) if (dlg.DoModal() == IDOK) ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) // m\_ctrlMPlayer.put\_stretchToFit(TRUE); // 伸缩画面，使其适合播放窗口  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) // ……可以在此进行各种其他设置  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) m\_ctrlMPlayer.SetUrl(dlg.GetPathName()); // 传递媒体文件到播放器  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif) }

VC++ 2005 代码:

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)void CMPlayDlg::OnBnClickedLoad() ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) CFileDialogdlg(TRUE,NULL,L"\*.\*",OFN\_FILEMUSTEXIST,  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) L"ActiveStreamingFormat(\*.asf)|\*.asf|"  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) L"AudioVideoInterleaveFormat(\*.avi)|\*.avi|"  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) L"RealAudio/RealVideo(\*.rm)|\*.rm|"  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) L"WaveAudio(\*.wav)|\*.wav|"  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) L"MIDIFile(\*.mid)|\*.mid|"  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) L"所有文件(\*.\*)|\*.\*||" );  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif) if (dlg.DoModal()==IDOK) ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) // m\_ctrlMPlayer.put\_stretchToFit(TRUE); // 伸缩画面，使其适合播放窗口  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) // ……可以在此进行各种其他设置  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)m\_ctrlMPlayer.put\_URL(dlg.GetPathName()); // 传递媒体文件到播放器  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

 

在该函数中设置了若干流媒体文件过虑器，创建打开文件公用对话框对象，让用户选择要播放的流媒体文件，并将该文件的路径通过调用 CMp 类的成员函数 Set\_URL 传递给媒体播放器。

 

注意 ，媒体播放器控件有各种不同的版本，对应的函数名也可能有所不同。如果找不到类似功能的函数，说明你系统中安装的媒体播放器控件有问题，可以[点击这里](http://download1.csdn.net/down3/20070526/26003723951.h) 下栽 mp.h 头文件，用它替代你的同名文件。

现在程序可以编译运行了...

 

#### （ 5 ）改变播放器窗口大小

要使程序可以让用户任意改变播放器窗口的大小，则需要设置对话框（位于属性窗口顶部的外观栏中）的边框属性 border 为 Resizing （可调整大小），该属性的缺省值为 Dialog Frame （对话框边框），是不能调整大小的。还可以同时设置 Maximize Box 和 Minimize Box 属性为 True 。

为了使媒体播放器的窗口随对话框而改变大小，需要为项目的对话框类添加 WM\_SIZE 的消息响应函数 OnSize 。并在该函数中，调用 CWnd 的成员函数

void MoveWindow(int x, int y, int nWidth, int nHeight, BOOL bRepaint = TRUE);

来改变播放器窗口的大小（注意：媒体播放器类是 CWnd 的派生类）。注意也要把打开按钮一起移动.

代码如下:

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)void CMplayDlg::OnSize(UINTnType, int cx, int cy)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif) ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) CDialog::OnSize(nType,cx,cy);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) m\_ctrlMPlayer.MoveWindow( 0 , 0 ,cx - 40 ,cy);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) m\_ctrlLoad.MoveWindow(cx - 40 , 0 ,cx,cy); // 改变打开按钮位置  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

还可以为对话框类再添加 WM\_KEYUP 消息响应函数。在该函数中，响应用户按键 Ctrl+L|O|F ，打开公用文件对话框，让用户选择媒体文件，并将其传送给播放器对象（后面这部分代码与前面的一样）。

代码如下：(VC++ 2005)

 

 

[源文件下载: MPlay.rar(VC6.0)46KB](http://download1.csdn.net/down3/20070526/26013651628.rar) [MPlay.rar (for VC2005) 45KB](http://download1.csdn.net/down3/20070526/26013934676.rar)

 

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)void CMPlayDlg::OnKeyUp(UINTnChar,UINTnRepCnt,UINTnFlags)... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)if ((GetKeyState(VK\_CONTROL)&1&lt;&lt;15)&&  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif) (nChar=='L'||nChar=='O'||nChar=='F'))... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) CFileDialogdlg(TRUE,NULL,L"\*.\*",OFN\_FILEMUSTEXIST,  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) L"ActiveStreamingFormat(\*.asf)|\*.asf|"  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) L"AudioVideoInterleaveFormat(\*.avi)|\*.avi|"  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) L"RealAudio/RealVideo(\*.rm)|\*.rm|"  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) L"WaveAudio(\*.wav)|\*.wav|"  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) L"MIDIFile(\*.mid)|\*.mid|"  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) L"所有文件(\*.\*)|\*.\*||");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)if (dlg.DoModal()==IDOK)... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) m\_ctrlMPlayer.put\_stretchToFit(TRUE);//伸缩画面，使其适合播放窗口  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) CStringfn=L"MPlayer:"+dlg.GetFileName();//构造程序标题  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) SetWindowText(fn);//设置程序窗口的标题  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) m\_ctrlMPlayer.put\_URL(dlg.GetPathName());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) CDialog::OnKeyUp(nChar,nRepCnt,nFlags);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif) }