本文介紹如何插入RealPlay控件實現媒體文件的播放,代碼運行效果圖如下:
下面簡要介紹一下具體實現步驟:
一、建立基於對話框的程序
二、在對話框內添加RealPlayer G2 control的ActiveX控件
(工程->添加工程->compontent and controls->registed ActiveX controls )。
三、在對話框內添加源程序內所示的按鈕和靜態文本 分別用於控制打開播放等控制及顯示歌曲信息 其ID號如源程序
四、用MFC映射各按鈕消息
void CSunapplerealplayerDlg::OnOpen()
{
char szFileFilter[]=
"RM File(*.rm)|*.rm|"
"Mp3 File(*.mp3)|*.mp3|"
"MPEG File(*.mpeg)|*.mpeg|"
"Media File(*.asf)|*.asf|"
"Video File(*.dat)|*.dat|"
"MPGA File(*.mpga)|*.mpga|"
"Wave File(*.wav)|*.wav|"
"AVI File(*.avi)|*.avi|"
"Movie File(*.mov)|*.mov|"
"Mid File(*.mid;*,rmi)|*.mid;*.rmi|"
"Wma File(*.wma)|*.wma|"
"All File(*.*)|*.*||";
CFileDialog dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY,szFileFilter);
if(dlg.DoModal()==IDOK){
CString PathName=dlg.GetPathName();
PathName.MakeUpper();
m_player->SetSource(PathName);
m_player->DoPlay();
SetDlgItemText(IDC_STATIC1,m_player->GetAuthor());
SetDlgItemText(IDC_STATIC2,m_player->GetTitle());
SetDlgItemText(IDC_COPYRIGHT,m_player->GetCopyright());
SetDlgItemText(IDC_SOURCE,m_player->GetSource());
}
}
void CSunapplerealplayerDlg::OnPlay()
{
SetDlgItemText(IDC_STATIC1,m_player->GetAuthor());
SetDlgItemText(IDC_STATIC2,m_player->GetTitle());
SetDlgItemText(IDC_COPYRIGHT,m_player->GetCopyright());
SetDlgItemText(IDC_SOURCE,m_player->GetSource());
m_player->DoPlay();
UpdateData(false);
SetTimer(1,20,NULL);
}
void CSunapplerealplayerDlg::OnTimer(UINT nIDEvent)
{
if(0&&isRepeat)
m_player->DoPlay();
CDialog::OnTimer(nIDEvent);
}
void CSunapplerealplayerDlg::OnClose()
{
///////添加此代碼時不要忘了在stdafx.h開頭處添加前兩行
AnimateWindow(GetSafeHwnd(),1000,AW_HIDE|AW_BLEND);
KillTimer(0);
////////////////////////////////////
//此處采用DestroyWindow關閉窗口
//多謝杜修杏 老師指點
///////////////////////////////////
this->DestroyWindow();
}
void CSunapplerealplayerDlg::OnFullscreen()
{
m_player->DoPause();
m_player->SetFullScreen();
m_player->DoPlay();
}
void CSunapplerealplayerDlg::OnMp3down()
{
ShellExecute(NULL,_T("open"),"http://sunapple.51.net",NULL,NULL,TRUE);
}
void CSunapplerealplayerDlg::OnPause()
{
m_player->DoPause();
}
void CSunapplerealplayerDlg::OnStop()
{
m_player->DoStop();
KillTimer(0);
}
void CSunapplerealplayerDlg::OnRepeat()
{
m_player->SetLoop(true);
if(isRepeat){
isRepeat=FALSE;
SetDlgItemText(IDC_REPEAT,"循環");
}
else
{
isRepeat=TRUE;
SetDlgItemText(IDC_REPEAT,"正常");
}
}
void CSunapplerealplayerDlg::OnLower()
{
// TODO: Add your control notification handler code here
short volume=m_player->GetVolume();
m_player->DoPause();
m_player->SetVolume(volume-100);
m_player->DoPlay();
}
void CSunapplerealplayerDlg::OnUpper()
{
// TODO: Add your control notification handler code here
short volume=m_player->GetVolume();
m_player->DoPause();
m_player->SetVolume(volume+100);
m_player->DoPlay();
}
void CSunapplerealplayerDlg::OnFloat()
{
// TODO: Add your command handler code here
ShellExecute(NULL,_T("open"),"http://sunapple.51.net",NULL,NULL,TRUE);
}
void CSunapplerealplayerDlg::OnPetroleum()
{
// TODO: Add your command handler code here
ShellExecute(NULL,_T("open"),"http://www.hdpu.edu.cn",NULL,NULL,TRUE);
}
五、映射WM_CTLCOLOR消息,用於控制文本顯示的顏色
HBRUSH CSunapplerealplayerDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
if(nCtlColor==CTLCOLOR_STATIC)
if(pWnd.GetDlgCtrlID()==IDC_VOLUME)
{
pDC.SetTextColor(RGB(165,182,222));
pDC.SetBkMode(TRANSPARENT);
return (HBRUSH) m_brush.GetSafeHandle();
}
if(pWnd.GetDlgCtrlID()==IDC_STATIC1||pWnd.GetDlgCtrlID()==IDC_STATIC2
||pWnd.GetDlgCtrlID()==IDC_SOURCE||pWnd.GetDlgCtrlID()==IDC_COPYRIGHT)
{
pDC.SetTextColor(RGB(0,0,255));
pDC.SetBkMode(TRANSPARENT);
return (HBRUSH) m_brush.GetSafeHandle();
}
if(pWnd.GetDlgCtrlID()==IDC_STATIC||pWnd.GetDlgCtrlID()==IDC_INFO)
{
pDC.SetTextColor(RGB(255,0,0));
pDC.SetBkMode(TRANSPARENT);
return (HBRUSH) m_brush.GetSafeHandle();
}
// TODO: Return a different brush if the default is not desired
return hbr;
}
六、在APP類裡的initInstance()裡添加下面函數改變對話框背景。
SetDialogBkColor(RGB(206,227,99));
七、為了美觀我們的外形可引入CButtonXP類,將個按鈕類型設置為CButtonXP
好了,應該大功告成了!調試一下吧!
本程序在調試過程中要多謝杜修杏老師的指點。