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|OFN_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();
}