vc制作视频播放器模板

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

VC:制作视频播放器(ActiveX控件、工具栏、菜单栏、滑块、打开对话框))
1、先把文件AMOVIE.OCX复制到某一目录下,例如D:\下,然后选择“开始”|“运行”命令,输入“regsvr32D:\AMOVIE.OCX”,单击“确定”按钮后,弹出对话框,显示注册成功。

2、利用类向导,创建一个基于对话框的应用程序,删除向导自动生成的确定、取消按钮。

选择Project?ADD。

添加ActiveMovie Control Object选项。

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("Failed to create toolbar\n");
return -1; // fail to create
}
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”的消息映射:
void CVideoDlg::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);
// TODO: Add your message handler code here
//工具栏处在对话框的下端
RepositionBars(AFX_IDW_CONTROLBAR_FIRST,AFX_IDW_CONTROLBAR _LAST,0);
//为了使滑块控件也可以一直显示在对话框的最下端位置
CRect rect;
GetClientRect(rect);//得到客户端
int top=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()消息。

void CVideoDlg::OnInitMenuPopup(CMenu* pPopupMenu, UINT nIndex, BOOL bSysMenu)
{
CDialog::OnInitMenuPopup(pPopupMenu, nIndex, bSysMenu);
// TODO: Add your message handler code here
ASSERT(pPopupMenu != NULL);
// Check the enabled state of various menu items.
CCmdUI state;
state.m_pMenu = pPopupMenu;
ASSERT(state.m_pOther == NULL);
ASSERT(state.m_pParentMenu == NULL);
// Determine if menu is popup in top-level menu and set m_pOther to
// it if so (m_pParentMenu == NULL indicates that it is secondary popup).
HMENU hParentMenu;
if (AfxGetThreadState()->m_hTrackingMenu ==
pPopupMenu->m_hMenu)
state.m_pParentMenu = pPopupMenu; // Parent == child for tracking popup.
else if ((hParentMenu = ::GetMenu(m_hWnd)) != NULL)
{
CWnd* pParent = this;
// Child windows don't have menus--need to go to the top!
if (pParent != NULL &&(hParentMenu
= ::GetMenu(pParent->m_hWnd)) != NULL)
{
int nIndexMax = ::GetMenuItemCount(hParentMenu);
for (int nIndex = 0; nIndex < nIndexMax; nIndex++)
{
if (::GetSubMenu(hParentMenu, nIndex) == pPopupMenu->m_hMenu)
{
// When popup is found, m_pParentMenu is containing menu.
state.m_pParentMenu =
CMenu::FromHandle(hParentMenu);
break;
}
}
}
}
state.m_nIndexMax = pPopupMenu->GetMenuItemCount();
for (state.m_nIndex = 0; state.m_nIndex <
state.m_nIndexMax;state.m_nIndex++)
{
state.m_nID = pPopupMenu->GetMenuItemID(state.m_nIndex);
if (state.m_nID == 0)
continue; // Menu separator or invalid cmd - ignore it.
ASSERT(state.m_pOther == NULL);
ASSERT(state.m_pMenu != NULL);
if (state.m_nID == (UINT)-1)
{
// Possibly a popup menu, route to first item of that popup.
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; // First item of popup can't be routed to.
}
state.DoUpdate(this, TRUE); // Popups are never auto disabled.
}
else
{
// Normal menu item.
// Auto enable/disable if frame window has m_bAutoMenuEnable
// set and command is _not_ a system command.
state.m_pSubMenu = NULL;
state.DoUpdate(this, FALSE);
}
// Adjust for menu deletions and additions.
UINT nCount = pPopupMenu->GetMenuItemCount();
if (nCount < state.m_nIndexMax)
{
state.m_nIndex -= (state.m_nIndexMax - nCount);
while (state.m_nIndex < nCount &&
pPopupMenu->GetMenuItemID(state.m_nIndex) == state.m_nID)
{
state.m_nIndex++;
}
}
state.m_nIndexMax = nCount;
}
}
void CVideoDlg::OnUpdateStop(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->Enable(flag);
}
6、为了使ActiveMovie控件进行播放,对ActiveMovie控件进行属性设置。

SelectionStart 0
ShowDisplay 真
ShowPositionContrls 真
ShowSelectionControls 假
ShowTracker 真
Volume真
等等。

7、利用类向导为ActiveMovie控件添加变量CActiveMovie m_play;添加文件打开消息映射:
void CVideoDlg::OnOpenFile()
{
// TODO: Add your command handler code here
CFileDialog
FileDlg(TRUE,NULL,NULL,OFN_HIDEREADONLY|OFN_ALLOWMULTISELECT|OF N_NOCHANGEDIR,
"Video
File(*.avi;*.asf;*.wmv;*.rm;*.rmvb)|*.avi;*.asf;*.wmv;*.rm;*.rmvb|Music Files(*.mp3;*.wav;*.cda)|*.mp3;*.wav;*.cda|Mpegvideo
File(*.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();
int iInstallresult;
iInstallresult=SetTimer(1,1000,NULL);
if(iInstallresult==0)
{
MessageBox("fail to install the timer!");
}
}
}
8、改变对话框的大小以适应ActiveMovie控件的大小:
void CVideoDlg::MoveMovieWindow()//改变窗口大小以适应控件大小
{
CRect rc1,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、暂停|播放:
void CVideoDlg::OnPlaypause()
{
// TODO: Add your command handler code here
if(playpause==false)
{
m_player.Pause();
playpause=true;
}
else
{
m_player.Run();
playpause=false;
}
}
10、停止播放:
void CVideoDlg::OnStop()
{
// TODO: Add your command handler code here
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、音量大小调整:
void CVideoDlg::OnAddVolume()
{
// TODO: Add your command handler code here
long m_Reduce = m_player.GetVolume();
if(m_Reduce<0)
{
m_player.Pause();
m_player.SetVolume(m_Reduce+100);
m_player.Run();
}
}
void CVideoDlg::OnSubVolume()
{
// TODO: Add your command handler code here
long m_Reduce = m_player.GetVolume();
if(m_Reduce>=-1500)
{
m_player.Pause();
m_player.SetVolume(m_Reduce-100);
m_player.Run();
}
}
12、选择视频大小:
void CVideoDlg::OnSizeFour()
{
// TODO: Add your command handler code here
m_player.Pause();
m_player.SetFullScreenMode(false);
m_player.SetMovieWindowSize(3);
m_player.Run();
num=3;
}
void CVideoDlg::OnUpdateSizeFour(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here pCmdUI->Enable(flag);
if(num==3)
pCmdUI->SetCheck(true);
else
pCmdUI->SetCheck(false);
}
void CVideoDlg::OnSizeSixten()
{
// TODO: Add your command handler code here
m_player.Pause();
m_player.SetFullScreenMode(false);
m_player.SetMovieWindowSize(2);
m_player.Run();
num=2;
}
void CVideoDlg::OnUpdateSizeSixten(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here pCmdUI->Enable(flag);
if(num==2)
pCmdUI->SetCheck(true);
else
pCmdUI->SetCheck(false);
}
void CVideoDlg::OnSizeTwo()
{
// TODO: Add your command handler code here
m_player.Pause();
m_player.SetFullScreenMode(false);
m_player.SetMovieWindowSize(4);
m_player.Run();
num=4;
}
void CVideoDlg::OnUpdateSizeTwo(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here pCmdUI->Enable(flag);
if(num==4)
pCmdUI->SetCheck(true);
else
pCmdUI->SetCheck(false);
}
void CVideoDlg::OnFull()
{
// TODO: Add your command handler code here
m_player.Pause();
m_player.SetFullScreenMode(true);
m_player.SetMovieWindowSize(SW_SHOWMAXIMIZED);
m_player.Run();
num=5;
}
13、添加单击工具栏从头播放的消息映射:
void CVideoDlg::OnPlay()
{
// TODO: Add your command handler code here
m_player.Stop();
m_player.Run();
flag=true;
toolbarctrl->EnableButton(IDM_PLAYPAUSE,true);
toolbarctrl->EnableButton(IDM_STOP,true);
toolbarctrl->EnableButton(IDM_ADD_VOLUME,true);
toolbarctrl->EnableButton(IDM_SUB_VOLUME,true);
14、动态的改变对话框的大小,需要利用类向导添加:ActiveMovie的StateChange的消息映射:
void CVideoDlg::OnStateChangeActivemovie1(long oldState, long newState) {
// TODO: Add your control notification handler code here
MoveMovieWindow();
}
15、捕获播放器的错误:
void CVideoDlg::OnErrorActivemovie1(short SCode, LPCTSTR Description, LPCTSTR Source, BOOL FAR* CancelDisplay)
{
// TODO: Add your control notification handler code here
CString str;
str.Format("出现错误[%d]:\n\n%s",SCode,Description);
AfxMessageBox(str);
*CancelDisplay=TRUE;
}
16、添加响应滑块控件移动的消息映射:ON_WM_HSCROLL的消息映射:
void CVideoDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
// TODO: Add your message handler code here and/or call default
if(flag)
{
if(pScrollBar->GetDlgCtrlID()==IDC_SLIDER);
{
int nRunTime = m_sliderctrl.GetPos();
m_player.SetCurrentPosition(nRunTime);
int MaxRange = m_player.GetCurrentPosition();
double sum=m_player.GetDuration();
m_sliderctrl.SetRange(0,int(sum));//需要得到文件长度
m_sliderctrl.SetTicFreq(10);
m_sliderctrl.SetPageSize(10);
}
CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
}
else
{
return;
}
}
17、播放时显示滑块的相应位置:
void CVideoDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default int pos = m_player.GetCurrentPosition();
double sum=m_player.GetDuration();
m_sliderctrl.SetRange(0,int(sum));
m_sliderctrl.SetPos(pos);
CDialog::OnTimer(nIDEvent);
CDialog::OnTimer(nIDEvent);
}
并在OnOpenFile()函数中添加代码:
int iInstallresult;
iInstallresult=SetTimer(1,1000,NULL);
if(iInstallresult==0)
{
MessageBox("fail to install the timer!");
}
}
18、直接打开帮助文件:
void CVideoDlg::OnHelp()
{
// TODO: Add your command handler code here
ShellExecute(NULL,"open","help.txt",NULL,NULL,SW_SHOWNORMAL); }
19、关于菜单下的消息映射:
void CVideoDlg::OnAbout()
{
// TODO: Add your command handler code here
CAboutDlg dlg;
dlg.DoModal();
}。

相关文档
最新文档