vc制作视频播放器.docx
《vc制作视频播放器.docx》由会员分享,可在线阅读,更多相关《vc制作视频播放器.docx(14页珍藏版)》请在冰豆网上搜索。
vc制作视频播放器
VC:
制作视频播放器(ActiveX控件、工具栏、菜单栏、滑块、打开对话框))
1、先把文件AMOVIE.OCX复制到某一目录下,例如D:
\下,然后选择“开始”|“运行”命令,输入“regsvr32D:
\AMOVIE.OCX”,单击“确定”按钮后,弹出对话框,显示注册成功。
2、利用类向导,创建一个基于对话框的应用程序,删除向导自动生成的确定、取消按钮。
选择Project?
ADD。
。
。
添加ActiveMovieControlObject选项。
3、单击Insert按钮,关闭该对话框,ActiveMovie控件便出现在控件面板中,调整好控件在对话框中的位置。
4、添加菜单资源。
。
5、添加工具栏。
。
。
6、添加滑块控件,及变量。
。
。
系统实现:
1、设置对话框的最小化,对话框属性对话框中选择Style标签。
2、添加工具栏。
在OnInitDialog()函数中:
if(!
m_toolbar.CreateEx(this,TBSTYLE_FLAT,WS_CHILD|WS_VISIBLE|CBRS_ALIGN_BOTTOM
|CBRS_TOOLTIPS)||
!
m_toolbar.LoadToolBar(IDR_TOOLBAR1))
{
TRACE0("Failedtocreatetoolbar\n");
return-1; //failtocreate
}
RepositionBars(AFX_IDW_CONTROLBAR_FIRST,AFX_IDW_CONTROLBAR_LAST,0);
m_toolbar.SetBarStyle(m_toolbar.GetBarStyle()|CBRS_BOTTOM|CBRS_SIZE_DYNAMIC|CBRS_SIZE_DYNAMIC);
3、利用类向导,添加“WM_SIZE”的消息映射:
voidCVideoDlg:
:
OnSize(UINTnType,intcx,intcy)
{
CDialog:
:
OnSize(nType,cx,cy);
//TODO:
Addyourmessagehandlercodehere
//工具栏处在对话框的下端
RepositionBars(AFX_IDW_CONTROLBAR_FIRST,AFX_IDW_CONTROLBAR_LAST,0);
//为了使滑块控件也可以一直显示在对话框的最下端位置
CRectrect;
GetClientRect(rect);//得到客户端
inttop=rect.top;
rect.top=rect.bottom-60;
rect.bottom=rect.top+30;
if(IsWindow(m_sliderctrl.GetSafeHwnd()))
{
m_sliderctrl.MoveWindow(rect);//设置窗口大小
}
}
4、下面对菜单进行控制,在对话框中的菜单资源是利用对话框属性在对话框中进行绑定的,这时的菜单资源仅仅是与对话框进行绑定。
它不能响应ON_UPDATE_COMMAND_UI消息。
为了响应这一事件,
需要首先重载ON_WM_INITMENUPOPUP()消息。
voidCVideoDlg:
:
OnInitMenuPopup(CMenu*pPopupMenu,UINTnIndex,BOOLbSysMenu)
{
CDialog:
:
OnInitMenuPopup(pPopupMenu,nIndex,bSysMenu);
//TODO:
Addyourmessagehandlercodehere
ASSERT(pPopupMenu!
=NULL);
//Checktheenabledstateofvariousmenuitems.
CCmdUIstate;
state.m_pMenu=pPopupMenu;
ASSERT(state.m_pOther==NULL);
ASSERT(state.m_pParentMenu==NULL);
//Determineifmenuispopupintop-levelmenuandsetm_pOtherto
//itifso(m_pParentMenu==NULLindicatesthatitissecondarypopup).
HMENUhParentMenu;
if(AfxGetThreadState()->m_hTrackingMenu==pPopupMenu->m_hMenu)
state.m_pParentMenu=pPopupMenu;//Parent==childfortrackingpopup.
elseif((hParentMenu=:
:
GetMenu(m_hWnd))!
=NULL)
{
CWnd*pParent=this;
//Childwindowsdon'thavemenus--needtogotothetop!
if(pParent!
=NULL&&(hParentMenu=:
:
GetMenu(pParent->m_hWnd))!
=NULL)
{
intnIndexMax=:
:
GetMenuItemCount(hParentMenu);
for(intnIndex=0;nIndex {
if(:
:
GetSubMenu(hParentMenu,nIndex)==pPopupMenu->m_hMenu)
{
//Whenpopupisfound,m_pParentMenuiscontainingmenu.
state.m_pParentMenu=CMenu:
:
FromHandle(hParentMenu);
break;
}
}
}
}
state.m_nIndexMax=pPopupMenu->GetMenuItemCount();
for(state.m_nIndex=0;state.m_nIndex {
state.m_nID=pPopupMenu->GetMenuItemID(state.m_nIndex);
if(state.m_nID==0)
continue;//Menuseparatororinvalidcmd-ignoreit.
ASSERT(state.m_pOther==NULL);
ASSERT(state.m_pMenu!
=NULL);
if(state.m_nID==(UINT)-1)
{
//Possiblyapopupmenu,routetofirstitemofthatpopup.
state.m_pSubMenu=pPopupMenu->GetSubMenu(state.m_nIndex);
if(state.m_pSubMenu==NULL||
(state.m_nID=state.m_pSubMenu->GetMenuItemID(0))==0||
state.m_nID==(UINT)-1)
{
continue;//Firstitemofpopupcan'tberoutedto.
}
state.DoUpdate(this,TRUE);//Popupsareneverautodisabled.
}
else
{
//Normalmenuitem.
//Autoenable/disableifframewindowhasm_bAutoMenuEnable
//setandcommandis_not_asystemcommand.
state.m_pSubMenu=NULL;
state.DoUpdate(this,FALSE);
}
//Adjustformenudeletionsandadditions.
UINTnCount=pPopupMenu->GetMenuItemCount();
if(nCount {
state.m_nIndex-=(state.m_nIndexMax-nCount);
while(state.m_nIndex pPopupMenu->GetMenuItemID(state.m_nIndex)==state.m_nID)
{
state.m_nIndex++;
}
}
state.m_nIndexMax=nCount;
}
}
voidCVideoDlg:
:
OnUpdateStop(CCmdUI*pCmdUI)
{
//TODO:
AddyourcommandupdateUIhandlercodehere
pCmdUI->Enable(flag);
}
6、为了使ActiveMovie控件进行播放,对ActiveMovie控件进行属性设置。
SelectionStart0
ShowDisplay真
ShowPositionContrls真
ShowSelectionControls假
ShowTracker真
Volume真
等等。
。
。
7、利用类向导为ActiveMovie控件添加变量CActiveMoviem_play;添加文件打开消息映射:
voidCVideoDlg:
:
OnOpenFile()
{
//TODO:
Addyourcommandhandlercodehere
CFileDialogFileDlg(TRUE,NULL,NULL,OFN_HIDEREADONLY|OFN_ALLOWMULTISELECT|OFN_NOCHANGEDIR,
"VideoFile(*.avi;*.asf;*.wmv;*.rm;*.rmvb)|*.avi;*.asf;*.wmv;*.rm;*.rmvb|MusicFiles(*.mp3;*.wav;*.cda)|*.mp3;*.wav;*.cda|MpegvideoFile(*.dat;*.mpg;*.mpeg)|*.dat;*.mpg;*.mpeg;*.mpe|");
if(FileDlg.DoModal()==IDOK)
{
filename=FileDlg.GetPathName();
m_player.SetFileName(filename);
flag=true;
toolbarctrl->EnableButton(IDM_PLAY,true);
toolbarctrl->EnableButton(IDM_PLAYPAUSE,true);
toolbarctrl->EnableButton(IDM_STOP,true);
toolbarctrl->EnableButton(IDM_ADD_VOLUME,true);
toolbarctrl->EnableButton(IDM_SUB_VOLUME,true);
MoveMovieWindow();
intiInstallresult;
iInstallresult=SetTimer(1,1000,NULL);
if(iInstallresult==0)
{
MessageBox("failtoinstallthetimer!
");
}
}
}
8、改变对话框的大小以适应ActiveMovie控件的大小:
voidCVideoDlg:
:
MoveMovieWindow()//改变窗口大小以适应控件大小
{
CRectrc1,rc2,rc3;
//得到ActiveMovie控件大小
m_player.GetWindowRect(rc1);//保证对话框客户区的宽不小于300象素,高不小于225象素
if(rc1.Width()<300||rc1.Height()<255)
{
rc1.right=rc1.left+300;
rc1.bottom=rc1.top+225;
}
//获得对话框的大小
GetWindowRect(rc2);
//获得对话框客户区大小
GetClientRect(rc3);
//改变对话框大小以适应ActiveMovie控件大小
MoveWindow(rc2.left,rc2.top,rc1.Width()+25,rc1.Height()+130);
Invalidate(true);
}
9、暂停|播放:
voidCVideoDlg:
:
OnPlaypause()
{
//TODO:
Addyourcommandhandlercodehere
if(playpause==false)
{
m_player.Pause();
playpause=true;
}
else
{
m_player.Run();
playpause=false;
}
}
10、停止播放:
voidCVideoDlg:
:
OnStop()
{
//TODO:
Addyourcommandhandlercodehere
m_player.Stop();
flag=false;
toolbarctrl->EnableButton(IDM_PLAYPAUSE,false);
toolbarctrl->EnableButton(IDM_STOP,false);
toolbarctrl->EnableButton(IDM_ADD_VOLUME,false);
toolbarctrl->EnableButton(IDM_SUB_VOLUME,false);
}
11、音量大小调整:
voidCVideoDlg:
:
OnAddVolume()
{
//TODO:
Addyourcommandhandlercodehere
longm_Reduce=m_player.GetVolume();
if(m_Reduce<0)
{
m_player.Pause();
m_player.SetVolume(m_Reduce+100);
m_player.Run();
}
}
voidCVideoDlg:
:
OnSubVolume()
{
//TODO:
Addyourcommandhandlercodehere
longm_Reduce=m_player.GetVolume();
if(m_Reduce>=-1500)
{
m_player.Pause();
m_player.SetVolume(m_Reduce-100);
m_player.Run();
}
}
12、选择视频大小:
voidCVideoDlg:
:
OnSizeFour()
{
//TODO:
Addyourcommandhandlercodehere
m_player.Pause();
m_player.SetFullScreenMode(false);
m_player.SetMovieWindowSize(3);
m_player.Run();
num=3;
}
voidCVideoDlg:
:
OnUpdateSizeFour(CCmdUI*pCmdUI)
{
//TODO:
AddyourcommandupdateUIhandlercodehere
pCmdUI->Enable(flag);
if(num==3)
pCmdUI->SetCheck(true);
else
pCmdUI->SetCheck(false);
}
voidCVideoDlg:
:
OnSizeSixten()
{
//TODO:
Addyourcommandhandlercodehere
m_player.Pause();
m_player.SetFullScreenMode(false);
m_player.SetMovieWindowSize
(2);
m_player.Run();
num=2;
}
voidCVideoDlg:
:
OnUpdateSizeSixten(CCmdUI*pCmdUI)
{
//TODO:
AddyourcommandupdateUIhandlercodehere
pCmdUI->Enable(flag);
if(num==2)
pCmdUI->SetCheck(true);
else
pCmdUI->SetCheck(false);
}
voidCVideoDlg:
:
OnSizeTwo()
{
//TODO:
Addyourcommandhandlercodehere
m_player.Pause();
m_player.SetFullScreenMode(false);
m_player.SetMovieWindowSize(4);
m_player.Run();
num=4;
}
voidCVideoDlg:
:
OnUpdateSizeTwo(CCmdUI*pCmdUI)
{
//TODO:
AddyourcommandupdateUIhandlercodehere
pCmdUI->Enable(flag);
if(num==4)