ID #324

用RealPlayer控件制作的播放器

本文介绍如何插入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

好了,应该大功告成了!调试一下吧!

本程序在调试过程中要多谢杜修杏老师的指点。


2011-07-01 16:01
阅读:
I'm VC , Just U know Y
本站部分文章来源于互联网,版权归原作者所有。

延伸阅读:

从32位应用程序到64位应用程序

使用VC++开发考场随机排座系统

VC6下用控件进行串口通信

通用的应用程序高级日志系统

用VC6实现IE工具栏