www.pudn.com > dllDSPlayer1.rar > T_dllDSPlayerDlg.cpp


// T_dllDSPlayerDlg.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "T_dllDSPlayer.h" 
#include "T_dllDSPlayerDlg.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CAboutDlg dialog used for App About 
 
class CAboutDlg : public CDialog 
{ 
public: 
	CAboutDlg(); 
 
// Dialog Data 
	//{{AFX_DATA(CAboutDlg) 
	enum { IDD = IDD_ABOUTBOX }; 
	//}}AFX_DATA 
 
	// ClassWizard generated virtual function overrides 
	//{{AFX_VIRTUAL(CAboutDlg) 
	protected: 
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support 
	//}}AFX_VIRTUAL 
 
// Implementation 
protected: 
	//{{AFX_MSG(CAboutDlg) 
	//}}AFX_MSG 
	DECLARE_MESSAGE_MAP() 
}; 
 
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) 
{ 
	//{{AFX_DATA_INIT(CAboutDlg) 
	//}}AFX_DATA_INIT 
} 
 
void CAboutDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CAboutDlg) 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) 
	//{{AFX_MSG_MAP(CAboutDlg) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
// 添加动态连接库信息 
#ifdef _DEBUG 
#pragma comment(lib,"..\\Debug\\dllDSPlayer.lib" ) 
#else 
#pragma comment(lib,"..\\release\\dllDSPlayer.lib" ) 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CT_dllDSPlayerDlg dialog 
 
CT_dllDSPlayerDlg::CT_dllDSPlayerDlg(CWnd* pParent /*=NULL*/) 
	: CDialog(CT_dllDSPlayerDlg::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CT_dllDSPlayerDlg) 
		// NOTE: the ClassWizard will add member initialization here 
	//}}AFX_DATA_INIT 
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32 
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); 
} 
 
void CT_dllDSPlayerDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CT_dllDSPlayerDlg) 
	DDX_Control(pDX, IDC_SLIDER1, m_SliderGraph); 
	DDX_Control(pDX, IDC_VIDEO_WINDOW, m_VideoWindow); 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CT_dllDSPlayerDlg, CDialog) 
	//{{AFX_MSG_MAP(CT_dllDSPlayerDlg) 
	ON_WM_SYSCOMMAND() 
	ON_WM_PAINT() 
	ON_WM_QUERYDRAGICON() 
	ON_BN_CLICKED(IDC_BUTTON_Play, OnBUTTONPlay) 
	ON_WM_ERASEBKGND() 
	ON_BN_CLICKED(IDC_BUTTON_Pause, OnBUTTONPause) 
	ON_BN_CLICKED(IDC_BUTTON_Stop, OnBUTTONStop) 
	ON_BN_CLICKED(IDC_BUTTON_GrabImage, OnBUTTONGrabImage) 
	ON_BN_CLICKED(IDC_BUTTON_FullScreen, OnBUTTONFullScreen) 
	ON_WM_TIMER() 
	ON_WM_DESTROY() 
	ON_WM_HSCROLL() 
	ON_CBN_SELCHANGE(IDC_COMBO_AudioDevice, OnSelchangeCOMBOAudioDevice) 
	ON_CBN_SELCHANGE(IDC_COMBO_CDROM, OnSelchangeComboCdrom) 
	ON_MESSAGE(WM_GRAPHNOTIFY,OnGraphNotify) 
	ON_CBN_SELCHANGE(IDC_COMBO_Track, OnSelchangeCOMBOTrack) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CT_dllDSPlayerDlg message handlers 
 
BOOL CT_dllDSPlayerDlg::OnInitDialog() 
{ 
	CDialog::OnInitDialog(); 
 
	// Add "About..." menu item to system menu. 
 
	// IDM_ABOUTBOX must be in the system command range. 
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); 
	ASSERT(IDM_ABOUTBOX < 0xF000); 
 
	CMenu* pSysMenu = GetSystemMenu(FALSE); 
	if (pSysMenu != NULL) 
	{ 
		CString strAboutMenu; 
		strAboutMenu.LoadString(IDS_ABOUTBOX); 
		if (!strAboutMenu.IsEmpty()) 
		{ 
			pSysMenu->AppendMenu(MF_SEPARATOR); 
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); 
		} 
	} 
 
	// Set the icon for this dialog.  The framework does this automatically 
	//  when the application's main window is not a dialog 
	SetIcon(m_hIcon, TRUE);			// Set big icon 
	SetIcon(m_hIcon, FALSE);		// Set small icon 
	 
	SetDlgItemText ( IDC_EDIT_Note, NOTE_CDSPlayer ); 
 
	m_VideoWindow.ModifyStyle(0, WS_CLIPCHILDREN); 
	m_SliderGraph.SetRange(0,1000); 
	m_SliderGraph.SetPos(0); 
 
	if ( !m_DSPlayer.Create () ) 
	{ 
		AfxMessageBox ( "Create DSPlayer module failed" ); 
		PostQuitMessage (0); 
		return FALSE; 
	} 
 
	CStringArray StrAry_AudioDevice; 
	m_DSPlayer.GetAudioDeviceList ( StrAry_AudioDevice ); 
	CComboBox *pCombo = (CComboBox*)GetDlgItem ( IDC_COMBO_AudioDevice ); 
	if ( pCombo ) 
	{ 
		for ( int i=0; iAddString ( StrAry_AudioDevice.GetAt(i) ); 
		} 
	} 
 
	int nDefaultAudioDeviceID = 4; 
	if ( StrAry_AudioDevice.GetSize() > nDefaultAudioDeviceID ) 
	{ 
		pCombo->SetCurSel ( nDefaultAudioDeviceID ); 
		m_DSPlayer.SetAudioDevice ( StrAry_AudioDevice.GetAt(nDefaultAudioDeviceID) ); 
	} 
 
	m_DSPlayer.m_hWndNotify = GetSafeHwnd(); 
	m_DSPlayer.m_hWndDisplay = m_VideoWindow.GetSafeHwnd(); 
	 
	ShowCDROMDrivers (); 
	return TRUE;  // return TRUE  unless you set the focus to a control 
} 
 
void CT_dllDSPlayerDlg::OnSysCommand(UINT nID, LPARAM lParam) 
{ 
	if ((nID & 0xFFF0) == IDM_ABOUTBOX) 
	{ 
		CAboutDlg dlgAbout; 
		dlgAbout.DoModal(); 
	} 
	else 
	{ 
		CDialog::OnSysCommand(nID, lParam); 
	} 
} 
 
// If you add a minimize button to your dialog, you will need the code below 
//  to draw the icon.  For MFC applications using the document/view model, 
//  this is automatically done for you by the framework. 
 
void CT_dllDSPlayerDlg::OnPaint()  
{ 
	if (IsIconic()) 
	{ 
		CPaintDC dc(this); // device context for painting 
 
		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); 
 
		// Center icon in client rectangle 
		int cxIcon = GetSystemMetrics(SM_CXICON); 
		int cyIcon = GetSystemMetrics(SM_CYICON); 
		CRect rect; 
		GetClientRect(&rect); 
		int x = (rect.Width() - cxIcon + 1) / 2; 
		int y = (rect.Height() - cyIcon + 1) / 2; 
 
		// Draw the icon 
		dc.DrawIcon(x, y, m_hIcon); 
	} 
	else 
	{ 
		CDialog::OnPaint(); 
	} 
} 
 
// The system calls this to obtain the cursor to display while the user drags 
//  the minimized window. 
HCURSOR CT_dllDSPlayerDlg::OnQueryDragIcon() 
{ 
	return (HCURSOR) m_hIcon; 
} 
 
void CT_dllDSPlayerDlg::OnBUTTONPlay()  
{ 
	CFileDialog filedlg ( TRUE, "mp3", NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, 
		_OPENTYPE, this ); 
	if ( filedlg.DoModal() != IDOK ) 
		return; 
	 
	Play ( filedlg.GetPathName() ); 
} 
 
BOOL CT_dllDSPlayerDlg::OnEraseBkgnd(CDC* pDC)  
{ 
	// TODO: Add your message handler code here and/or call default 
	// Intercept background erasing for the movie window, since the 
    // video renderer will keep the screen painted.  Without this code, 
    // your video window might get painted over with gray (the default 
    // background brush) when it is obscured by another window and redrawn. 
    CRect rc; 
    // Get the bounding rectangle for the movie screen 
    m_VideoWindow.GetWindowRect(&rc); 
    ScreenToClient(&rc); 
    // Exclude the clipping region occupied by our movie screen 
    pDC->ExcludeClipRect(&rc); 
 
	// Erase the remainder of the dialog as usual 
	return CDialog::OnEraseBkgnd(pDC); 
} 
 
void CT_dllDSPlayerDlg::OnBUTTONPause()  
{ 
	if( m_DSPlayer.IsInitedOK () ) 
	{ 
		m_DSPlayer.Pause(); 
		SetTimer ( SLIDER_TIMER, 100, NULL ); 
	} 
} 
 
void CT_dllDSPlayerDlg::OnBUTTONStop()  
{ 
	if( m_DSPlayer.IsInitedOK () ) 
	{ 
		m_SliderGraph.SetPos(0); 
		m_DSPlayer.SetCurrentPosition ( 0 ); 
		m_DSPlayer.Stop (); 
		KillTimer ( SLIDER_TIMER ); 
	} 
} 
 
void CT_dllDSPlayerDlg::OnBUTTONGrabImage()  
{ 
	if( m_DSPlayer.IsInitedOK () ) 
	{ 
		CString csBmpFile ( ' ', MAX_PATH+256 ); 
		::GetTempPath ( MAX_PATH-1, csBmpFile.GetBuffer(MAX_PATH) ); 
		csBmpFile.ReleaseBuffer (); 
		if ( csBmpFile.GetLength() > 0 && csBmpFile.GetAt(csBmpFile.GetLength()-1) != '\\' ) 
		{ 
			csBmpFile += "\\"; 
		} 
		csBmpFile += "tempsnapshot.bmp"; 
		if ( m_DSPlayer.SnapshotBitmap(csBmpFile) ) 
		{ 
			CString strFilter = "BMP File(*.bmp)|*.bmp|"; 
			CFileDialog dlgOpen(FALSE,".bmp",NULL,OFN_HIDEREADONLY,strFilter,NULL); 
			if(IDOK==dlgOpen.DoModal()) 
			{ 
				::CopyFile(csBmpFile,dlgOpen.GetPathName(),FALSE); 
			} 
			::DeleteFile(csBmpFile); 
		} 
	} 
} 
 
void CT_dllDSPlayerDlg::OnBUTTONFullScreen()  
{ 
	if( m_DSPlayer.IsInitedOK () ) 
	{ 
		m_DSPlayer.SetFullScreen ( TRUE ); 
	} 
} 
 
BOOL CT_dllDSPlayerDlg::PreTranslateMessage(MSG* pMsg)  
{ 
	if(pMsg->message==WM_KEYDOWN) 
	{ 
		if ( pMsg->wParam==VK_RETURN || pMsg->wParam==VK_ESCAPE ) 
		{ 
			RestoreFromFullScreen(); 
			return 1; 
		} 
	} 
 
	return CDialog::PreTranslateMessage(pMsg); 
} 
 
void CT_dllDSPlayerDlg::RestoreFromFullScreen() 
{ 
	if( m_DSPlayer.IsInitedOK () ) 
	{ 
		if ( m_DSPlayer.GetFullScreen () ) 
		{ 
			m_DSPlayer.SetFullScreen ( FALSE ); 
		} 
	} 
} 
 
void CT_dllDSPlayerDlg::OnTimer(UINT nIDEvent)  
{ 
	if ( nIDEvent == SLIDER_TIMER && m_DSPlayer.IsInitedOK () ) 
	{ 
		double pos=0, duration=1.; 
		m_DSPlayer.GetCurrentPosition(&pos); 
		m_DSPlayer.GetDuration(&duration); 
		int newpos=int(pos * 1000 / duration); 
		if(m_SliderGraph.GetPos()!=newpos) 
		{ 
			m_SliderGraph.SetPos(newpos); 
		} 
	} 
	 
	CDialog::OnTimer(nIDEvent); 
} 
 
void CT_dllDSPlayerDlg::OnDestroy()  
{ 
	CDialog::OnDestroy(); 
	KillTimer ( SLIDER_TIMER ); 
} 
 
void CT_dllDSPlayerDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)  
{ 
	if ( pScrollBar->GetSafeHwnd() == m_SliderGraph.GetSafeHwnd() ) 
	{ 
		if ( m_DSPlayer.IsInitedOK () ) 
		{ 
			double duration=1.; 
			m_DSPlayer.GetDuration(&duration); 
			double pos=duration *m_SliderGraph.GetPos()/1000.; 
			m_DSPlayer.SetCurrentPosition(pos); 
		} 
	} 
	else 
	{ 
		CDialog::OnHScroll(nSBCode, nPos, pScrollBar); 
	} 
} 
 
LRESULT CT_dllDSPlayerDlg::OnGraphNotify(WPARAM wParam, LPARAM lParam) 
{ 
	IMediaEventEx *pEvent=NULL; 
	if ( m_DSPlayer.IsInitedOK () && (pEvent=m_DSPlayer.GetEventHandle()) ) 
	{ 
		LONG eventCode=0,eventParam1=0,eventParam2=0; 
		while(SUCCEEDED(pEvent->GetEvent(&eventCode,&eventParam1,&eventParam2,0))) 
		{ 
			pEvent->FreeEventParams(eventCode,eventParam1,eventParam2); 
			switch(eventCode) 
			{ 
			case EC_COMPLETE: 
				OnBUTTONPause(); 
				m_DSPlayer.SetCurrentPosition(0); 
				break; 
			case EC_USERABORT: 
			case EC_ERRORABORT: 
				OnBUTTONStop(); 
				break; 
 
			default: 
				break; 
			} 
		} 
	} 
	return 0; 
} 
 
void CT_dllDSPlayerDlg::OnSelchangeCOMBOAudioDevice()  
{ 
	CComboBox *pCombo = (CComboBox*)GetDlgItem ( IDC_COMBO_AudioDevice ); 
	ASSERT ( pCombo ); 
	int nCurSel = pCombo->GetCurSel (); 
	if ( nCurSel >= 0 ) 
	{ 
		CString csAudioDeviceName; 
		pCombo->GetLBText ( nCurSel, csAudioDeviceName ); 
		m_DSPlayer.SetAudioDevice ( csAudioDeviceName ); 
	} 
} 
 
void CT_dllDSPlayerDlg::ShowCDROMDrivers() 
{ 
	CString csCDROMDriver; 
	int nCount = hwprGetCDromDriver ( csCDROMDriver ); 
	if ( nCount < 1 ) return; 
	CComboBox *pCombo = (CComboBox*)GetDlgItem(IDC_COMBO_CDROM); 
	if ( !pCombo ) return; 
 
	char seps[]   = " ,\t\n"; 
	char *token = strtok ( csCDROMDriver.GetBuffer(0), seps ); 
	while ( token != NULL ) 
	{ 
		pCombo->AddString ( token ); 
		/* Get next token: */ 
		token = strtok( NULL, seps ); 
	} 
	pCombo->SetCurSel ( 0 ); 
	csCDROMDriver.ReleaseBuffer (); 
} 
 
void CT_dllDSPlayerDlg::OnSelchangeComboCdrom()  
{ 
	CComboBox *pCombo = (CComboBox*)GetDlgItem(IDC_COMBO_CDROM); 
	if ( !pCombo ) return; 
	int nCurSel = pCombo->GetCurSel (); 
	if ( nCurSel < 0 ) 
		return; 
	CString csCDROM; 
	pCombo->GetLBText ( nCurSel, csCDROM ); 
 
	int nTrackNum = GetCDTrackNum ( csCDROM ); 
	if ( nTrackNum <= 0 ) 
		return; 
 
	AddCDAllTracksToCombo ( nTrackNum ); 
	OnSelchangeCOMBOTrack (); 
} 
 
void CT_dllDSPlayerDlg::AddCDAllTracksToCombo ( int nTrackNum ) 
{ 
	CComboBox *pCombo = (CComboBox*)GetDlgItem(IDC_COMBO_Track); 
	if ( !pCombo ) return; 
	pCombo->ResetContent (); 
	for ( int i=0; iAddString ( csText ); 
	} 
	if ( pCombo->GetCount() > 0 ) pCombo->SetCurSel ( 0 ); 
} 
 
void CT_dllDSPlayerDlg::OnSelchangeCOMBOTrack()  
{ 
	CComboBox *pCombo_Cdrom = (CComboBox*)GetDlgItem(IDC_COMBO_CDROM); 
	CComboBox *pCombo_Track = (CComboBox*)GetDlgItem(IDC_COMBO_Track); 
	if ( !pCombo_Cdrom || !pCombo_Track ) return; 
	int nCurSel_Cdrom = pCombo_Cdrom->GetCurSel (); 
	int nCurSel_Track = pCombo_Track->GetCurSel (); 
	if ( nCurSel_Cdrom < 0 || nCurSel_Track < 0 ) 
		return; 
 
	CString csText_Cdrom, csText_Track; 
	pCombo_Cdrom->GetLBText ( nCurSel_Cdrom, csText_Cdrom ); 
	pCombo_Track->GetLBText ( nCurSel_Track, csText_Track ); 
 
	CString csTrackFileName; 
	csTrackFileName.Format ( "%s\\%s", csText_Cdrom, csText_Track ); 
	Play ( csTrackFileName ); 
} 
 
BOOL CT_dllDSPlayerDlg::Play(LPCTSTR lpszMediaFileName) 
{ 
	if ( !lpszMediaFileName ) return FALSE; 
	if ( m_DSPlayer.GetMediaFileName () == lpszMediaFileName ) 
		return TRUE; 
	OnBUTTONStop (); 
	if ( !m_DSPlayer.RenderFile ( lpszMediaFileName ) ) 
		return FALSE; 
	m_DSPlayer.Delete_DefaultDirectSoundDevice ();	//d 
	if ( !m_DSPlayer.Run() ) 
		return FALSE; 
	SetTimer(SLIDER_TIMER,100,NULL); 
 
	return TRUE; 
}