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

📄 mediaplayervcdlg.cpp

📁 Repeat Easily 1.0 版 程序说明 这个小软件是用来练习英语听力用的
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// MediaPlayerVCDlg.cpp : 实现文件
//

#include "stdafx.h"
#include "MediaPlayerVC.h"
#include "MediaPlayerVCDlg.h"
//#include "Windows.h"//add by yjk
#include <io.h>//调用_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<WPARAM>(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<HCURSOR>(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<maxIndex+1;i++)
		{
			sprintf(buff,"%f\n",positions[i]);
			file1.WriteString((LPCTSTR)buff);

		}
		file1.Close();
		MessageBox("声音的暂停点文件保存完毕.");
		alreadyModified=false;
}
//添加暂停点
void CMediaPlayerVCDlg::OnBnClickedAdd()
{
	// TODO: 在此添加控件通知处理程序代码
	UpdateData(true);
	double v1=axMediaPlayer1.get_CurrentPosition()-shiftValue;
	maxIndex++;
	positions[maxIndex]=v1;
	char buff[20];
	sprintf(buff,"%f",v1);
	listBox1.AddString(buff);
	alreadyModified=true;
	listBox1.SetCurSel(maxIndex);
}

void CMediaPlayerVCDlg::savePausePoints(void)
{
	//在退出系统之前,需要先检测一下,看看声音文件的暂停点系列是否被改编过了,
	//如果被改编过了,就需要弹出一个对话框,让用户选择是否将修改保存到文件中.
	if(alreadyModified)
	{
		if(MessageBox("暂停点已经被修改过了,并且尚未保存(如果选择保存,将可能会覆盖原来的暂停点文件),是否要保存?","杨竣凯",MB_YESNO)==IDYES)
		{
			//下面开始写文件
			CStdioFile file1;
			//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(textFileName1,CStdioFile::modeCreate|CStdioFile::modeWrite);
			//将数组positions中的所有有效值都保存到文本文件中
			char buff[80];
			for(int i=0;i<maxIndex+1;i++)
			{
				sprintf(buff,"%f\n",positions[i]);
				file1.WriteString((LPCTSTR)buff);
			}
			file1.Close();
			alreadyModified=false;
		}
	}
}

void CMediaPlayerVCDlg::OnClose()
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	axMediaPlayer1.Stop();
	SaveStateToIniFile();
	savePausePoints();
	CDialog::OnClose();
}

void CMediaPlayerVCDlg::enableAllButtonsExceptOpenFileButton(bool enable)
{
	//CButton *pButton;
	if(enable)
	{
		((CButton *)GetDlgItem(IDC_SAVEFILE))->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);   

⌨️ 快捷键说明

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