www.pudn.com > sndrpr10.zip > MediaPlayerVCDlg.cpp
// MediaPlayerVCDlg.cpp : 实现文件 // #include "stdafx.h" #include "MediaPlayerVC.h" #include "MediaPlayerVCDlg.h" //#include "Windows.h"//add by yjk #include//调用_access()需要 #ifdef _DEBUG #define new DEBUG_NEW #endif // 用于应用程序“关于”菜单项的 CAboutDlg 对话框 class CAboutDlg : public CDialog { public: CAboutDlg(); // 对话框数据 enum { IDD = IDD_ABOUTBOX }; protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持 // 实现 protected: DECLARE_MESSAGE_MAP() }; CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) { } void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); } BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) END_MESSAGE_MAP() // CMediaPlayerVCDlg 对话框 CMediaPlayerVCDlg::CMediaPlayerVCDlg(CWnd* pParent /*=NULL*/) : CDialog(CMediaPlayerVCDlg::IDD, pParent) , m_textBox2(0) , m_textBox3(0) , m_nTimer(0) , shiftValue(0) , incrementValue2(0) , incrementValue1(0) { m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CMediaPlayerVCDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); DDX_Control(pDX, IDC_MEDIAPLAYER1, axMediaPlayer1); DDX_Control(pDX, IDC_EDIT1, textBox1); DDX_Control(pDX, IDC_LIST1, listBox1); DDX_Text(pDX, IDC_EDIT2, m_textBox2); DDX_Text(pDX, IDC_EDIT3, m_textBox3); DDX_Control(pDX, IDC_MEDIAPLAYER1, axMediaPlayer1); DDX_Text(pDX, IDC_SHIFT, shiftValue); DDX_Text(pDX, IDC_INCREMENT2, incrementValue2); DDX_Text(pDX, IDC_INCREMENT1, incrementValue1); } BEGIN_MESSAGE_MAP(CMediaPlayerVCDlg, CDialog) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() //}}AFX_MSG_MAP ON_BN_CLICKED(IDC_OPENFILE, OnBnClickedOpenfile) ON_BN_CLICKED(IDC_PLAY, OnBnClickedPlay) ON_BN_CLICKED(IDC_SAVEFILE, OnBnClickedSavefile) ON_BN_CLICKED(IDC_ADD, OnBnClickedAdd) ON_WM_CLOSE() ON_BN_CLICKED(IDC_PAUSE, OnBnClickedPause) ON_BN_CLICKED(IDC_REPEAT1, OnBnClickedRepeat1) ON_WM_TIMER() ON_BN_CLICKED(IDC_REPEAT2, OnBnClickedRepeat2) ON_BN_CLICKED(IDC_DELETE, OnBnClickedDelete) ON_BN_CLICKED(IDC_BUTTON8, OnBnClickedButton8) ON_BN_CLICKED(IDC_BUTTON9, OnBnClickedButton9) ON_BN_CLICKED(IDC_NEXT, OnBnClickedNext) ON_LBN_DBLCLK(IDC_LIST1, OnLbnDblclkList1) ON_BN_CLICKED(IDC_SUBTRACT1, OnBnClickedSubtract1) ON_BN_CLICKED(IDC_PLUS1, OnBnClickedPlus1) ON_BN_CLICKED(IDC_SUBTRACT2, OnBnClickedSubtract2) ON_BN_CLICKED(IDC_PLUS2, OnBnClickedPlus2) ON_EN_KILLFOCUS(IDC_EDIT2, OnEnKillfocusEdit2) ON_EN_KILLFOCUS(IDC_EDIT3, OnEnKillfocusEdit3) ON_BN_CLICKED(IDC_SETSTART, OnBnClickedSetstart) ON_BN_CLICKED(IDC_SETEND, OnBnClickedSetend) ON_BN_CLICKED(IDC_ABOUT, OnBnClickedAbout) END_MESSAGE_MAP() // CMediaPlayerVCDlg 消息处理程序 BOOL CMediaPlayerVCDlg::OnInitDialog() { CDialog::OnInitDialog(); // 将\“关于...\”菜单项添加到系统菜单中。 // IDM_ABOUTBOX 必须在系统命令范围内。 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); } } // 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动 // 执行此操作 SetIcon(m_hIcon, TRUE); // 设置大图标 SetIcon(m_hIcon, FALSE); // 设置小图标 // TODO:在此添加额外的初始化代码 //yjk add begin //如果从ini文件加载上次播放的文件的信息失败,则 if(!LoadStateFromIniFile()) { position1=0; position2=0; incrementValue1=0.50; incrementValue2=0.50; shiftValue=0.33; textBox1Text="D:\\English1.mp3"; enableAllButtonsExceptOpenFileButton(false); sprintf(textFileName1,"D:\\English1.txt"); maxIndex=-1; alreadyModified=false; } textBox1.SetWindowText(textBox1Text); UpdateData(FALSE); //yjk add end return TRUE; // 除非设置了控件的焦点,否则返回 TRUE } void CMediaPlayerVCDlg::OnSysCommand(UINT nID, LPARAM lParam) { if ((nID & 0xFFF0) == IDM_ABOUTBOX) { CAboutDlg dlgAbout; dlgAbout.DoModal(); } else { CDialog::OnSysCommand(nID, lParam); } } // 如果向对话框添加最小化按钮,则需要下面的代码 // 来绘制该图标。对于使用文档/视图模型的 MFC 应用程序, // 这将由框架自动完成。 void CMediaPlayerVCDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // 用于绘制的设备上下文 SendMessage(WM_ICONERASEBKGND, reinterpret_cast (dc.GetSafeHdc()), 0); // 使图标在工作矩形中居中 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; // 绘制图标 dc.DrawIcon(x, y, m_hIcon); } else { CDialog::OnPaint(); } } //当用户拖动最小化窗口时系统调用此函数取得光标显示。 HCURSOR CMediaPlayerVCDlg::OnQueryDragIcon() { return static_cast (m_hIcon); } //“Open File”按钮的消息处理 void CMediaPlayerVCDlg::OnBnClickedOpenfile() { // TODO: 在此添加控件通知处理程序代码 axMediaPlayer1.Stop(); savePausePoints(); char currentDir[250]; GetCurrentDirectory(250,currentDir); if(currentDir[lstrlen(currentDir)-1]=='\\') lstrcat(currentDir,"MP3文件"); else lstrcat(currentDir,"\\MP3文件"); CFileDialog dlg(TRUE,"",currentDir,NULL,"*.mp3|*.mp3|*.*|*.*||"); if(dlg.DoModal()==IDOK) { wsprintf(currentDir,"%s",dlg.GetPathName()); textBox1.SetWindowText(currentDir); textBox1Text=currentDir;//保存声音文件名 axMediaPlayer1.put_FileName(currentDir); axMediaPlayer1.Stop(); position1=0; position2=0; m_textBox2=position1; m_textBox3=position2; UpdateData(false); //下面处理暂停点 listBox1.ResetContent();//清空暂停点列表框 maxIndex=-1; lstrcpyn(textFileName1,currentDir,lstrlen(currentDir)-2); lstrcat(textFileName1,"txt"); CStdioFile file1; //如果 打开 跟要打开的mp3文件对应的 暂停点文件 成功,则 if(file1.Open(textFileName1,CStdioFile::modeRead)) { while(file1.ReadString(currentDir,80)) { listBox1.AddString(currentDir); maxIndex++; positions[maxIndex]=atof(currentDir);//把字符串转化成double类型的数据 } if(maxIndex>-1) listBox1.SetCurSel(0); } enableAllButtonsExceptOpenFileButton(true); file1.Close(); alreadyModified=false; } } void CMediaPlayerVCDlg::OnBnClickedPlay() { // TODO: 在此添加控件通知处理程序代码 // Declare variables that we can use to save the exception // record and the context if an exception should occur. EXCEPTION_RECORD SavedExceptRec; CONTEXT SavedContext; __try { KillTimer(1); KillTimer(2); //UpdateData(true); axMediaPlayer1.put_FileName(textBox1Text); position1=position2; axMediaPlayer1.put_CurrentPosition(position1); axMediaPlayer1.Play();//为什么执行了这一句后,会抛出一个异常,而且还能正常播放?答:正常了,原因是我原来使用的是Open方法,现在改用put_FileName方法就好了。 m_textBox2=position1; UpdateData(FALSE); } __except(ExpFltr((GetExceptionInformation())->ExceptionRecord)) { MessageBox("可能是选择了无效的声音文件","杨竣凯",MB_OK|MB_ICONSTOP); enableAllButtonsExceptOpenFileButton(false); } } void CMediaPlayerVCDlg::OnBnClickedSavefile() { // TODO: 在此添加控件通知处理程序代码 CStdioFile file1; //if(file1.Open(textFileName1,CStdioFile::modeWrite)) if(_access(textFileName1,0)==0)//CSDN网友提供的简单方法 { if(MessageBox("文件已经存在,是否要覆盖它?","杨竣凯",MB_YESNO)==IDNO) return; } file1.Close(); file1.Open(textFileName1,CStdioFile::modeCreate|CStdioFile::modeWrite); //将数组positions中的所有有效值都保存到文本文件中 char buff[80]; for(int i=0;i EnableWindow(true); ((CButton *)GetDlgItem(IDC_PLAY))->EnableWindow(true); ((CButton *)GetDlgItem(IDC_PAUSE))->EnableWindow(true); ((CButton *)GetDlgItem(IDC_REPEAT1))->EnableWindow(true); ((CButton *)GetDlgItem(IDC_ADD))->EnableWindow(true); ((CButton *)GetDlgItem(IDC_DELETE))->EnableWindow(true); ((CButton *)GetDlgItem(IDC_BUTTON8))->EnableWindow(true); ((CButton *)GetDlgItem(IDC_BUTTON9))->EnableWindow(true); ((CButton *)GetDlgItem(IDC_REPEAT2))->EnableWindow(true); ((CButton *)GetDlgItem(IDC_NEXT))->EnableWindow(true); } else { ((CButton *)GetDlgItem(IDC_SAVEFILE))->EnableWindow(false); ((CButton *)GetDlgItem(IDC_PLAY))->EnableWindow(false); ((CButton *)GetDlgItem(IDC_PAUSE))->EnableWindow(false); ((CButton *)GetDlgItem(IDC_REPEAT1))->EnableWindow(false); ((CButton *)GetDlgItem(IDC_ADD))->EnableWindow(false); ((CButton *)GetDlgItem(IDC_DELETE))->EnableWindow(false); ((CButton *)GetDlgItem(IDC_BUTTON8))->EnableWindow(false); ((CButton *)GetDlgItem(IDC_BUTTON9))->EnableWindow(false); ((CButton *)GetDlgItem(IDC_REPEAT2))->EnableWindow(false); ((CButton *)GetDlgItem(IDC_NEXT))->EnableWindow(false); } } void CMediaPlayerVCDlg::OnBnClickedPause() { // TODO: 在此添加控件通知处理程序代码 __try { axMediaPlayer1.Pause(); position2=axMediaPlayer1.get_CurrentPosition(); m_textBox2=position1; m_textBox3=position2; UpdateData(false); } __except(ExpFltr((GetExceptionInformation())->ExceptionRecord)) { MessageBox("可能是选择了无效的声音文件","杨竣凯",MB_OK|MB_ICONINFORMATION); enableAllButtonsExceptOpenFileButton(false); } } void CMediaPlayerVCDlg::OnBnClickedRepeat1() { // TODO: 在此添加控件通知处理程序代码 __try { //timer1.Enabled=true;很遗憾,MFC工程中没有Timer控件,只能用SetTimer() KillTimer(2); m_nTimer = SetTimer(1, 100, 0); axMediaPlayer1.put_CurrentPosition(position1); axMediaPlayer1.Play(); } __except(EXCEPTION_EXECUTE_HANDLER) { KillTimer(m_nTimer); MessageBox("可能是选择了无效的声音文件","杨竣凯",MB_OK|MB_ICONQUESTION); enableAllButtonsExceptOpenFileButton(false); } } void CMediaPlayerVCDlg::OnTimer(UINT nIDEvent) { // TODO: 在此添加消息处理程序代码和/或调用默认值 if(nIDEvent==1) { if(axMediaPlayer1.get_CurrentPosition()>=position2) { if(((CButton*)GetDlgItem(IDC_CHECK1))->GetCheck()==1) { axMediaPlayer1.put_CurrentPosition(position1); return; } KillTimer(m_nTimer); axMediaPlayer1.Stop(); } } else { if(listBox1.GetCurSel()==maxIndex) return; if(axMediaPlayer1.get_CurrentPosition()>=positions[listBox1.GetCurSel()+1]) { if(((CButton*)GetDlgItem(IDC_CHECK1))->GetCheck()==1) { axMediaPlayer1.put_CurrentPosition(positions[listBox1.GetCurSel()]); return; } KillTimer(2); axMediaPlayer1.Stop(); } } CDialog::OnTimer(nIDEvent); } //单击“重放”按钮,引发重放动作 void CMediaPlayerVCDlg::OnBnClickedRepeat2() { // TODO: 在此添加控件通知处理程序代码 if(listBox1.GetCurSel()==-1) return; axMediaPlayer1.put_CurrentPosition(positions[listBox1.GetCurSel()]); KillTimer(1); SetTimer(2, 100, 0); axMediaPlayer1.Play(); } //删除被选中的暂停点 void CMediaPlayerVCDlg::OnBnClickedDelete() { // TODO: 在此添加控件通知处理程序代码 int i=listBox1.GetCurSel(); if(i==-1) return; while(i maxIndex) { if(i==0) return; listBox1.SetCurSel(maxIndex); } else listBox1.SetCurSel(i); alreadyModified=true; } //减小被选中的暂停点的值 void CMediaPlayerVCDlg::OnBnClickedButton8() { // TODO: 在此添加控件通知处理程序代码 UpdateData(true); int i2=listBox1.GetCurSel(); if(i2<0) return; positions[i2]=positions[i2]-incrementValue2; char str[80]; sprintf(str,"%f",positions[i2]); listBox1.DeleteString(i2); listBox1.InsertString(i2,str); listBox1.SetCurSel(i2); alreadyModified=true; } //增加被选中的暂停点的值 void CMediaPlayerVCDlg::OnBnClickedButton9() { // TODO: 在此添加控件通知处理程序代码 UpdateData(true); int i2=listBox1.GetCurSel(); if(i2<0) return; positions[i2]=positions[i2]+incrementValue2; char str[80]; sprintf(str,"%f",positions[i2]); listBox1.DeleteString(i2); listBox1.InsertString(i2,str); listBox1.SetCurSel(i2); alreadyModified=true; } //播放下一句的按钮 void CMediaPlayerVCDlg::OnBnClickedNext() { // TODO: 在此添加控件通知处理程序代码 int i3=listBox1.GetCurSel(); if(i3==maxIndex) return; listBox1.SetCurSel(i3+1); axMediaPlayer1.put_CurrentPosition(positions[i3+1]); SetTimer(2, 100, 0); axMediaPlayer1.Play(); } void CMediaPlayerVCDlg::OnLbnDblclkList1() { // TODO: 在此添加控件通知处理程序代码 OnBnClickedRepeat2(); } //position1减0.5 void CMediaPlayerVCDlg::OnBnClickedSubtract1() { // TODO: 在此添加控件通知处理程序代码 UpdateData(true); position1=position1-incrementValue1; m_textBox2=position1; UpdateData(false); } //position1加0.5 void CMediaPlayerVCDlg::OnBnClickedPlus1() { // TODO: 在此添加控件通知处理程序代码 UpdateData(true); if((position1+incrementValue1)>position2) { position2=position2+incrementValue1; m_textBox3=position2; } position1=position1+incrementValue1; m_textBox2=position1; UpdateData(false); } //position2减0.5 void CMediaPlayerVCDlg::OnBnClickedSubtract2() { // TODO: 在此添加控件通知处理程序代码 UpdateData(true); if((position2-incrementValue1) ExceptionCode; sprintf(szBuf, "Code = %x, Address = %p", dwExceptionCode, pER->ExceptionAddress); // Find the end of the string. p = strchr(szBuf, 0); // I used a switch statement in case Microsoft adds // information for other exception codes in the future. switch (dwExceptionCode) { case EXCEPTION_ACCESS_VIOLATION: sprintf(p, "Attempt to %s data at address %p", pER->ExceptionInformation[0] ? "write" : "read", pER->ExceptionInformation[1]); break; default: break; } MessageBox(szBuf, "Exception", MB_OK | MB_ICONEXCLAMATION); //return(EXCEPTION_CONTINUE_SEARCH); return(EXCEPTION_EXECUTE_HANDLER); } bool CMediaPlayerVCDlg::LoadStateFromIniFile(void) { CStdioFile fileIni; CStdioFile file1; char currentDir[250]; GetModuleFileName(theApp.m_hInstance,currentDir,250); currentDir[lstrlen(currentDir)-3]=0; lstrcat(currentDir,"ini"); //下面的这一段代码,是从MediaPlayerVC.ini文件中读取上次播放的信息,使 //播放器恢复上次的状态。 bool bReaddingFileOK=true; if(!fileIni.Open(currentDir,CStdioFile::modeRead)) { //打开ini文件失败,说明,ini文件并不存在,所以,就不用从ini文件中读取信息了,返回false return false; } if(fileIni.ReadString(currentDir,80)) position1=atof(currentDir);//把字符串转化成double类型的数据 else bReaddingFileOK=false; if(bReaddingFileOK && fileIni.ReadString(currentDir,80)) position2=atof(currentDir);//把字符串转化成double类型的数据 else bReaddingFileOK=false; if(bReaddingFileOK && fileIni.ReadString(currentDir,80)) incrementValue1=atof(currentDir);//把字符串转化成double类型的数据 else bReaddingFileOK=false; if(bReaddingFileOK && fileIni.ReadString(currentDir,80)) incrementValue2=atof(currentDir);//把字符串转化成double类型的数据 else bReaddingFileOK=false; if(bReaddingFileOK && fileIni.ReadString(currentDir,80)) shiftValue=atof(currentDir);//把字符串转化成double类型的数据 else bReaddingFileOK=false; if(bReaddingFileOK && fileIni.ReadString(currentDir,250)) //读取上一次播放的文件完整路径,到currentDir目录中 textBox1Text=currentDir; else bReaddingFileOK=false; fileIni.Close(); //从MediaPlayerVC.ini文件中读取上次播放的信息 结束 //如果从ini文件中读取信息成功,则 if(bReaddingFileOK) { //这一段代码是将从ini文件中读取的信息,在对话框中的各个控件中,显示出来 textBox1.SetWindowText(currentDir); textBox1Text=currentDir;//保存声音文件的完整路径 axMediaPlayer1.put_FileName(currentDir); axMediaPlayer1.Stop(); m_textBox2=position1; m_textBox3=position2; enableAllButtonsExceptOpenFileButton(true); UpdateData(false); //下面的代码是加载暂停点文件 enableAllButtonsExceptOpenFileButton(true); listBox1.ResetContent();//清空列表框 maxIndex=-1; lstrcpyn(textFileName1,currentDir,lstrlen(currentDir)-2); lstrcat(textFileName1,"txt"); //MessageBox(textFileName1,"",1);//调试用的 //如果打开暂停点文件成功,则 if(file1.Open(textFileName1,CStdioFile::modeRead)) { //读取暂停点文件中的内容,将内容添加到数组和ListBox控件中 while(file1.ReadString(currentDir,80)) { listBox1.AddString(currentDir); maxIndex++; positions[maxIndex]=atof(currentDir);//把字符串转化成double类型的数据 } if(maxIndex>-1) listBox1.SetCurSel(0); } file1.Close(); alreadyModified=false; return true; } return false; } bool CMediaPlayerVCDlg::SaveStateToIniFile(void) { CStdioFile file1; char currentDir[250]; GetModuleFileName(theApp.m_hInstance,currentDir,250); //lstrcpyn(textFileName1,currentDir,lstrlen(currentDir)-2); currentDir[lstrlen(currentDir)-3]=0; lstrcat(currentDir,"ini"); //CFile::modeCreate Directs the constructor to create a new file. If the file exists already, it is truncated to 0 length. //必须要使用CFile::modeCreate这种OpenFlag来打开文件,因为需要去掉旧的文件内容 file1.Open(currentDir,CStdioFile::modeCreate|CStdioFile::modeWrite); //将播放器的当前状态信息保存到ini文件中 char buff[80]; UpdateData(true); sprintf(buff,"%f\n",position1); file1.WriteString((LPCTSTR)buff); sprintf(buff,"%f\n",position2); file1.WriteString((LPCTSTR)buff); sprintf(buff,"%f\n",incrementValue1); file1.WriteString((LPCTSTR)buff); sprintf(buff,"%f\n",incrementValue2); file1.WriteString((LPCTSTR)buff); sprintf(buff,"%f\n",shiftValue); file1.WriteString((LPCTSTR)buff); file1.WriteString((LPCTSTR)textBox1Text); file1.Close(); return true; } void CMediaPlayerVCDlg::OnBnClickedAbout() { // TODO: 在此添加控件通知处理程序代码 CAboutDlg dlgAbout; dlgAbout.DoModal(); }