⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dialogbarplay.cpp

📁 一个学习ODBC访问Access数据库的好例子
💻 CPP
字号:
// DialogBarPlay.cpp : implementation file
//

#include "stdafx.h"
#include "chapter20.h"
#include "DialogBarPlay.h"
#include "vfw.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CDialogBarPlay dialog

#define TIMER_PLAYING   0

CDialogBarPlay::CDialogBarPlay(CWnd* pParent /*=NULL*/)
{
	//{{AFX_DATA_INIT(CDialogBarPlay)
	//}}AFX_DATA_INIT
}


void CDialogBarPlay::DoDataExchange(CDataExchange* pDX)
{
	CDialogBar::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDialogBarPlay)
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDialogBarPlay, CDialogBar)
	//{{AFX_MSG_MAP(CDialogBarPlay)
	ON_BN_CLICKED(IDC_STOP, OnStop)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_PLAY, OnPlay)
	ON_BN_CLICKED(IDC_PAUSE, OnPause)
	ON_WM_HSCROLL()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDialogBarPlay message handlers

BOOL CDialogBarPlay::InitDialog()
{
	//起始隐藏播放状态图标
	CWnd* pPic=(CWnd*)GetDlgItem(IDC_PLAY_PICTURE);
	pPic->ShowWindow(SW_HIDE);
	//初始化个成员变量
	m_hMp3=NULL;
	m_bPause=FALSE;
	m_strPath="";
	m_Volume=500;
	m_CurPosition=0;
	m_EndPosition=0;
	//设置播放音量
	CSliderCtrl* pSlidCtrl=(CSliderCtrl*)GetDlgItem(IDC_SLIDER_VOLUM);
	//设置滑动条范围
	pSlidCtrl->SetRange(0,m_Volume*2,TRUE);
	//设置滑动条位置
	pSlidCtrl->SetPos(m_Volume);
	//子类化播放控制按钮
	m_btnOpen.SubclassDlgItem(IDC_OPEN_SINGLE, this);
    m_btnOpen.SetIcon(IDI_OPEN); 
	m_btnPlay.SubclassDlgItem(IDC_PLAY, this);
    m_btnPlay.SetIcon(IDI_PLAY); 
	m_btnPause.SubclassDlgItem(IDC_PAUSE, this);
    m_btnPause.SetIcon(IDI_PAUSE); 
	m_btnStop.SubclassDlgItem(IDC_STOP, this);
    m_btnStop.SetIcon(IDI_STOP); 
	return TRUE;
}

void CDialogBarPlay::OnTimer(UINT nIDEvent)
{
	// TODO: Add your message handler code here and/or call default
	switch(nIDEvent)
	{
		case TIMER_PLAYING:
			{
				CWnd* pPic=(CWnd*)GetDlgItem(IDC_PLAY_PICTURE);
				//获取当前位置
				m_CurPosition=MCIWndGetPosition(m_hMp3);
				div_t div_pos;
				//被2整除时显示
				div_pos=div(m_CurPosition,2);
				if(div_pos.rem==0)
				{
					pPic->ShowWindow(SW_SHOW);
				}
				else
				{
					pPic->ShowWindow(SW_HIDE);
				}
				//判断播放结束否
				if(m_CurPosition==m_EndPosition)
				{
					KillTimer(TIMER_PLAYING);
					CWnd* pPic=(CWnd*)GetDlgItem(IDC_PLAY_PICTURE);
					pPic->ShowWindow(SW_HIDE);
					MCIWndDestroy(m_hMp3);
					m_hMp3=NULL;
				}
				break;
			}
		default:
			break;
	}
	CDialogBar::OnTimer(nIDEvent);
}

void CDialogBarPlay::Play(CString &strMp3Name)
{
	//如果还没有播放窗口则创建
	if(m_hMp3==NULL)
	{
		m_hMp3= MCIWndCreate(this->GetSafeHwnd(),
			AfxGetInstanceHandle(),
			WS_CHILD | WS_VISIBLE|MCIWNDF_NOPLAYBAR,strMp3Name);
	}
	else
	{
		MCIWndHome(m_hMp3);
	}
	//设置定时器用于控制播放状态图标的显隐
	SetTimer(TIMER_PLAYING,100,NULL);
	MCIWndSetVolume(m_hMp3,m_Volume);
	//开始播放
	if(!MCIWndPlay(m_hMp3))
		return;
	CWnd* pPic=(CWnd*)GetDlgItem(IDC_PLAY_PICTURE);
	pPic->ShowWindow(SW_SHOW);
}

void CDialogBarPlay::OnPlay() 
{
	// TODO: Add your control notification handler code here
	Play(m_strPath);
}

void CDialogBarPlay::OnPause() 
{
	// TODO: Add your control notification handler code here
	//先前是否处于暂停状态
	if(m_bPause)
	{
		//若先前处于暂停则继续播放
		MCIWndResume(m_hMp3);
		m_bPause= FALSE;
	}
	else
	{
		//否则,暂停
		MCIWndPause(m_hMp3);
		m_bPause= TRUE;
	}
}

void CDialogBarPlay::OnStop() 
{
	// TODO: Add your control notification handler code here
	MCIWndStop(m_hMp3);
	if(m_hMp3!=NULL)
	{
		MCIWndDestroy(m_hMp3);
	}
	CWnd* pPic=(CWnd*)GetDlgItem(IDC_PLAY_PICTURE);
	pPic->ShowWindow(SW_HIDE);
}

void CDialogBarPlay::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
	// TODO: Add your message handler code here and/or call default
	//强制转换,关于这个请参看MSDN说明
	CSliderCtrl* pSlidCtrl=(CSliderCtrl*)pScrollBar;
	//设置滑动条位置
	m_Volume=pSlidCtrl->GetPos();
	if(m_hMp3==NULL)
	{
		return;
	}
	//设置音量
	MCIWndSetVolume(m_hMp3,m_Volume);
	CDialogBar::OnHScroll(nSBCode, nPos, pScrollBar);
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -