www.pudn.com > LrcMaker.zip > LrcMakerDlg.cpp


// LrcMakerDlg.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "LrcMaker.h" 
#include "LrcMakerDlg.h" 
 
 
#include "wmpcontrols.h" 
#include "wmpmedia.h" 
 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
 
BYTE *	pAsciiFont; 
BYTE *	pHanziFont; 
 
 
#define	WM_MONITOR		(WM_USER+100) 
 
 
///////////////////////////////////////////////////////////////////////////// 
// 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) 
		// No message handlers 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CLrcMakerDlg dialog 
 
CLrcMakerDlg::CLrcMakerDlg(CWnd* pParent /*=NULL*/) 
	: CDialog(CLrcMakerDlg::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CLrcMakerDlg) 
	m_bSimulation = FALSE; 
	//}}AFX_DATA_INIT 
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32 
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); 
	nIndex = 0; 
	bPlaying = FALSE; 
	tminfo = NULL; 
	strVoiceFile = ""; 
} 
 
void	CLrcMakerDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CLrcMakerDlg) 
	DDX_Control(pDX, IDC_VSCROLLBAR, m_VScrollBarCtrl); 
	DDX_Control(pDX, IDC_VIEW, m_ctrlView); 
	DDX_Control(pDX, IDC_LIST, m_ctrlListBox); 
	DDX_Control(pDX, IDC_PLAYER, m_ctrlPlayer); 
	DDX_Check(pDX, IDC_SIMULATION, m_bSimulation); 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CLrcMakerDlg, CDialog) 
	//{{AFX_MSG_MAP(CLrcMakerDlg) 
	ON_WM_SYSCOMMAND() 
	ON_WM_PAINT() 
	ON_WM_QUERYDRAGICON() 
	ON_BN_CLICKED(IDC_AUDIO, OnAudio) 
	ON_BN_CLICKED(IDC_TEXT, OnText) 
	ON_BN_CLICKED(IDC_PLAY, OnPlay) 
	ON_BN_CLICKED(IDC_LOCATE, OnLocate) 
	ON_BN_CLICKED(IDC_SAVE, OnSave) 
	ON_BN_CLICKED(IDC_SIMULATION, OnSimulation) 
	ON_WM_VSCROLL() 
	ON_LBN_SELCHANGE(IDC_LIST, OnSelchangeList) 
	ON_WM_TIMER() 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CLrcMakerDlg message handlers 
 
BOOL	CLrcMakerDlg::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 
	 
	// TODO: Add extra initialization here 
	pAsciiFont = LoadFont( "ascii12x24.bin" ); 
	pHanziFont = LoadFont( "hanzi24x24.bin" ); 
 
	if( !pAsciiFont || !pHanziFont ) 
	{ 
		MessageBox( "加载字库文件失败!", "错误", MB_ICONERROR | MB_OK ); 
		EndDialog( FALSE ); 
		return	FALSE; 
	} 
 
	 
	return TRUE;  // return TRUE  unless you set the focus to a control 
} 
 
void	CLrcMakerDlg::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	CLrcMakerDlg::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 CLrcMakerDlg::OnQueryDragIcon() 
{ 
	return (HCURSOR) m_hIcon; 
} 
 
void	CLrcMakerDlg::OnAudio()  
{ 
	// TODO: Add your control notification handler code here 
	CFileDialog  openDlg(TRUE, NULL, NULL,  
		OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, 
		TEXT("多媒体音频文件 (*.MP3;*.WMA)|*.MP3;*.WMA|所有文件 (*.*)|*.*||"), 
		NULL ); 
 
	if( openDlg.DoModal()==IDOK ) 
	{ 
		strVoiceFile = openDlg.GetPathName(); 
		m_ctrlPlayer.SetUrl( strVoiceFile ); 
	} 
} 
 
void	CLrcMakerDlg::OnText()  
{ 
	// TODO: Add your control notification handler code here 
	CFileDialog  openDlg(TRUE, NULL, NULL,  
		OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, 
		TEXT("文本文件 (*.TXT)|*.TXT|所有文件 (*.*)|*.*||"), 
		NULL ); 
 
	if( openDlg.DoModal()==IDOK ) 
	{ 
		CString	strFile; 
		FILE *	fp; 
		CHAR	buf[256+1]={0}; 
		int	i, len, line=0; 
 
		//将文本内容加入到列表框中 
		strFile = openDlg.GetPathName(); 
		 
		fp = fopen( (LPCTSTR)strFile, "rt" ); 
		if( !fp ) 
		{ 
			MessageBox( "无法打开指定的文本文件!", "错误", MB_ICONERROR | MB_OK ); 
			return; 
		} 
 
		m_ctrlListBox.ResetContent(); 
 
		while(1) 
		{ 
			fgets( buf, 256, fp ); 
 
			if( feof(fp) ) break; 
 
			if( buf[0] !=0x0d && buf[0]!=0x0a ) 
			{ 
				len = strlen( buf ); 
				 
				for( i=0; iGetTextExtent(str); 
			 
			if (sz.cx > dx) 
				dx = sz.cx; 
		} 
 
		m_ctrlListBox.ReleaseDC(pDC); 
		 
		// Set the horizontal extent so every character of all strings  
		// can be scrolled to. 
		m_ctrlListBox.SetHorizontalExtent(dx); 
 
		nIndex = 0; 
 
		m_ctrlListBox.SetCurSel( nIndex ); 
		m_ctrlListBox.GetText( nIndex, str ); 
		m_ctrlView.SetText( (LPTSTR)(LPCTSTR)str, TRUE ); 
	} 
	 
} 
 
void	CLrcMakerDlg::OnPlay()  
{ 
	// TODO: Add your control notification handler code here 
	CString	str; 
 
	if( strVoiceFile != "" ) 
	{ 
		if( !bPlaying ) 
		{ 
			nIndex = 0; 
 
			if( tminfo ) 
			{ 
				m_ctrlListBox.SetCurSel( nIndex ); 
				m_ctrlListBox.GetText( nIndex, str ); 
				m_ctrlView.SetText( (LPTSTR)(LPCTSTR)str, TRUE ); 
			} 
			 
			bPlaying = TRUE; 
		} 
 
		if( m_bSimulation ) 
		{ 
			SetTimer( WM_MONITOR, 100, NULL ); 
		} 
 
		m_ctrlPlayer.GetControls().play(); 
	}	 
} 
 
void	CLrcMakerDlg::OnLocate()  
{ 
	// TODO: Add your control notification handler code here 
	CString	str; 
	int	sec, ms, cnt; 
	double	pos; 
	 
	cnt = m_ctrlListBox.GetCount(); 
 
	if( strVoiceFile != "" && bPlaying ) 
	{ 
		pos = m_ctrlPlayer.GetControls().GetCurrentPosition(); 
 
		str.Format( "%f", pos ); 
 
		sscanf( str, "%d.%d", &sec, &ms ); 
 
		if( tminfo ) 
		{ 
			tminfo[nIndex].min = sec/60; 
			tminfo[nIndex].sec = sec%60; 
			tminfo[nIndex].ms = ms/1000; 
		} 
 
		if( nIndex < (cnt-1) ) 
		{ 
			nIndex++; 
 
			m_ctrlListBox.GetText( nIndex, str ); 
			m_ctrlListBox.SetCurSel( nIndex ); 
			m_ctrlView.SetText( (LPTSTR)(LPCTSTR)str, TRUE ); 
		} 
	} 
} 
 
void	CLrcMakerDlg::OnSave()  
{ 
	// TODO: Add your control notification handler code here 
	int	cnt = m_ctrlListBox.GetCount(); 
	FILE *	fp; 
	CString	fname, str; 
 
	if( tminfo && strVoiceFile!="" ) 
	{ 
		fname = strVoiceFile; 
 
		if( fname.Find( ".mp3" )!= -1 ) 
		{ 
			fname.Replace( ".mp3", ".LRC" ); 
		} 
		else if( fname.Find( ".MP3" )!= -1 ) 
		{ 
			fname.Replace( ".MP3", ".LRC" ); 
		} 
		else 
		{ 
			MessageBox( "指定的音频文件不是MP3类型的文件!", "错误", MB_ICONINFORMATION | MB_OK );		 
			return; 
		} 
 
		fp = fopen( (LPCTSTR)fname, "wt" ); 
 
		if( !fp ) 
		{ 
			MessageBox( "创建 LRC 文件失败!", "错误", MB_ICONINFORMATION | MB_OK );		 
			return; 
		} 
 
		fprintf( fp, "[ar:unknown]\n" ); 
		fprintf( fp, "[ti:sample]\n" ); 
		fprintf( fp, "[al:none]\n" ); 
		fprintf( fp, "[by:me]\n" ); 
 
		for( int i=0; iSetScrollPos( nPos ); 
		nIndex = nPos; 
		m_ctrlListBox.GetText( nIndex, str ); 
		m_ctrlListBox.SetCurSel( nIndex ); 
		m_ctrlView.SetText( (LPTSTR)(LPCTSTR)str, TRUE ); 
		break; 
	case	SB_LINEUP: 
	case	SB_PAGEUP: 
		nPos = pScrollBar->GetScrollPos()-1; 
		pScrollBar->SetScrollPos( nPos ); 
 
		if( nIndex > 0 ) 
		{ 
			nIndex--; 
			m_ctrlListBox.GetText( nIndex, str ); 
			m_ctrlListBox.SetCurSel( nIndex ); 
			m_ctrlView.SetText( (LPTSTR)(LPCTSTR)str, TRUE ); 
		} 
		break; 
	case	SB_LINEDOWN: 
	case	SB_PAGEDOWN: 
		nPos = pScrollBar->GetScrollPos()+1; 
		pScrollBar->SetScrollPos( nPos ); 
		cnt = m_ctrlListBox.GetCount(); 
 
		if( nIndex < (cnt-1) ) 
		{ 
			nIndex++; 
			m_ctrlListBox.GetText( nIndex, str ); 
			m_ctrlListBox.SetCurSel( nIndex ); 
			m_ctrlView.SetText( (LPTSTR)(LPCTSTR)str, TRUE ); 
		} 
		break; 
	default: 
		break; 
	} 
 
 
	if( nPos ) 
	{ 
		double val; 
 
		if( tminfo[nPos].sec || tminfo[nPos].ms ) 
		{ 
			val = (( tminfo[nPos].min*60+tminfo[nPos].sec)*1000+tminfo[nPos].ms ) /1000.0; 
			m_ctrlPlayer.GetControls().SetCurrentPosition( val );	 
		} 
	} 
	else 
	{ 
		m_ctrlPlayer.GetControls().SetCurrentPosition( (double)0.0 );	 
	} 
} 
 
 
 
 
 
void	CLrcMakerDlg::OnSelchangeList()  
{ 
	// TODO: Add your control notification handler code here 
	CString	str; 
 
	nIndex = m_ctrlListBox.GetCurSel(); 
 
	m_ctrlListBox.GetText( nIndex, str ); 
	 
	m_ctrlView.SetText( (LPTSTR)(LPCTSTR)str, TRUE ); 
} 
 
void	CLrcMakerDlg::OnTimer(UINT nIDEvent)  
{ 
	// TODO: Add your message handler code here and/or call default 
	CString		strCurrent; 
	unsigned int	sec, ms, now, tm; 
	int		cnt; 
	double		pos; 
	 
 
	if( bPlaying && nIDEvent==WM_MONITOR && m_bSimulation ) 
	{ 
		//取播放文件的当前位置 
		pos = m_ctrlPlayer.GetControls().GetCurrentPosition(); 
 
		strCurrent.Format( "%f", pos ); 
 
		sscanf( (LPCTSTR)strCurrent, "%d.%d", &sec, &ms ); 
 
		now = sec*1000+ms/1000; 
 
#if	1 
		//与 CurPage 中的 position 比较 
		if( tminfo ) 
		{ 
			cnt = m_ctrlListBox.GetCount(); 
 
			tm = (tminfo[nIndex].min * 60 + tminfo[nIndex].sec ) * 1000 + tminfo[nIndex].ms; 
 
			if( now >= tm ) 
			{ 
				CString	str; 
 
				//显示下一页 
				if( nIndex < (cnt-1) ) 
				{ 
					nIndex++; 
					m_ctrlListBox.SetCurSel( nIndex ); 
 
					m_ctrlListBox.GetText( nIndex, str ); 
					m_ctrlView.SetText( (LPTSTR)(LPCTSTR)str, TRUE ); 
				} 
			} 
		} 
#endif 
	} 
	 
	CDialog::OnTimer(nIDEvent); 
} 
 
BEGIN_EVENTSINK_MAP(CLrcMakerDlg, CDialog) 
    //{{AFX_EVENTSINK_MAP(CLrcMakerDlg) 
	ON_EVENT(CLrcMakerDlg, IDC_PLAYER, 5101 /* PlayStateChange */, OnPlayStateChangePlayer, VTS_I4) 
	//}}AFX_EVENTSINK_MAP 
END_EVENTSINK_MAP() 
 
void	CLrcMakerDlg::OnPlayStateChangePlayer(long NewState)  
{ 
	// TODO: Add your control notification handler code here 
	if( NewState==8 ) 
	{ 
		CString	str; 
		int	cnt, sec, ms; 
		 
		double  tm; 
		 
		tm = m_ctrlPlayer.GetCurrentMedia().GetDuration(); 
 
		cnt = m_ctrlListBox.GetCount(); 
 
		if( cnt ) 
		{ 
			str.Format( "%f", tm ); 
			sscanf( str, "%d.%d", &sec, &ms ); 
			//MessageBox( str ); 
 
			tminfo[cnt-1].min = sec / 60; 
			tminfo[cnt-1].sec = sec % 60; 
			tminfo[cnt-1].ms = ms / 1000; 
		} 
 
		bPlaying = FALSE; 
		KillTimer( WM_MONITOR ); 
	} 
}