VC+ActiveX+ini=Multi-language Mp3 player
在VC下用ActiveX+INI文件打造(简体/繁体/英文)多国语言Mp3播放器
blog http://blog.csdn.net/chinayaosir
author:chinanetboy=chinayaosir
Make Tools:vc++6.0 complied oK
目录:
0.language.ini内容
1.生成对话框程序
2.添加ActiveX
3.对话框控件
4.添加控件代码
5.添加定时函数
6.编译和测试
内容:
0.language.ini内容
/*---------------------------------------------------------------------*/
[default]
language=Traditional
[english]
open =&Open
play =&Play
pause =&Pause
stop =&Stop
repeat =&Repeat
exit =&Exit
volume =Volume
voladd =VOL&+
volsub =VOL&-
language=language
[Simplified]
open =&O打开
play =&P播放
pause =&P暂停
stop =&S停止
repeat =&R重复
exit =&E退出
volume =音量
voladd =音量&+
volsub =音量&-
language=语言
[Traditional]
open =&O打開
play =&P播放
pause =&P暫停
stop =&S停止
repeat =&R重復
exit =&E退出
volume =音量
voladd =音量&+
volsub =音量&-
language=语言
1.生成对话框程序
open VC++6.0,projects->MFC AppWizard(exe)->projects name=Mp3Play,
Build application with Dialog Based,finished!
/*---------------------------------------------------------------------*/
2.添加ActiveX
OPen Project ->add to Project->Components and Controls
->Registered ActiveX Control,choose ActiveMovieControl Object,
单击Insert按钮,在对话框设计的控件面板多了一个ActiveMovieControl Object,
把它放到对话框正上方,Ctrl+W打开ClassWizard为它添加public变量CActiveMovie3 m_ActiveMovie。
/*---------------------------------------------------------------------*/
3.对话框控件
2.在Resource View,选择其中的对话框,打开其中的主对话框,
删除对话框上的“确定”按钮,保留“取消”,将Caption改为“&Exit”
添中7个按钮,3个静态文本,1个下拉列表
7个按钮:ID和Caption分别为
IDC_OPEN,&Open;
IDC_PLAY,&Play;
IDC_PAUSE,Pause;
IDC_STOP,&Stop;
IDC_LOWER,V&--;
IDC_UPPER,V&++;
IDC_REPEAT,&Repeat;
3个静态文本控件,ID和Caption分别为
IDC_STATIC,Volume;(音量)
IDC_STATIC2,Status:Normal;(播放状态)
IDC_STATIC_SONG,SongName;(歌名)
IDC_STATIC_l,language;(语言)
1个下拉列表:
IDC_COMBOBOXEX1,data为1,2,3(语言选择)
在Mp3PlayDlg.cpp开始处添加如下4行代码
#define Simplified 1
#define English 2
#define Traditional 3
char filename[80]="E://language.ini";
控件排列方式
第1行到第3行放CActiveMovie3控件
第4行放IDC_STATIC_SONG
第5行放IDC_STATIC,IDC_STATIC2
第6行放7个按钮
/*---------------------------------------------------------------------*/
4.添加控件代码
4.1为class CMediaPlayerDlg添加如下2个方法和12个私有变量
代码如下:
public:
void SetLanguage();
void GetLanguage(int choice);
private:
BOOL isRepeat;
int choice;
char language[30];
char m_open[40],m_play[40],m_pause[40],m_stop[40],m_repeat[40];
char m_exit[40],m_volume[40],m_voladd[40],m_volsub[40],m_language[40];
----------------------------------------------
4.2添加语言操纵函数GetLanguage和SetLanguage
代码如下:
void CMediaPlayerDlg::GetLanguage(int choice)
{
switch(choice)
{
case English:
GetPrivateProfileString("english","open","",m_open,sizeof(m_open),filename);
GetPrivateProfileString("english","play","",m_play,sizeof(m_play),filename);
GetPrivateProfileString("english","pause","",m_pause,sizeof(m_pause),filename);
GetPrivateProfileString("english","stop","",m_stop,sizeof(m_stop),filename);
GetPrivateProfileString("english","repeat","",m_repeat,sizeof(m_repeat),filename);
GetPrivateProfileString("english","exit","",m_exit,sizeof(m_exit),filename);
GetPrivateProfileString("english","volume","",m_volume,sizeof(m_volume),filename);
GetPrivateProfileString("english","voladd","",m_voladd,sizeof(m_voladd),filename);
GetPrivateProfileString("english","volsub","",m_volsub,sizeof(m_volsub),filename);
GetPrivateProfileString("english","language","",m_language,sizeof(m_language),filename);
break;
case Simplified:
GetPrivateProfileString("Simplified","open","",m_open,sizeof(m_open),filename);
GetPrivateProfileString("Simplified","play","",m_play,sizeof(m_play),filename);
GetPrivateProfileString("Simplified","pause","",m_pause,sizeof(m_pause),filename);
GetPrivateProfileString("Simplified","stop","",m_stop,sizeof(m_stop),filename);
GetPrivateProfileString("Simplified","repeat","",m_repeat,sizeof(m_repeat),filename);
GetPrivateProfileString("Simplified","exit","",m_exit,sizeof(m_exit),filename);
GetPrivateProfileString("Simplified","volume","",m_volume,sizeof(m_volume),filename);
GetPrivateProfileString("Simplified","voladd","",m_voladd,sizeof(m_voladd),filename);
GetPrivateProfileString("Simplified","volsub","",m_volsub,sizeof(m_volsub),filename);
GetPrivateProfileString("Simplified","language","",m_language,sizeof(m_language),filename);
break;
case Traditional:
GetPrivateProfileString("Traditional","open","",m_open,sizeof(m_open),filename);
GetPrivateProfileString("Traditional","play","",m_play,sizeof(m_play),filename);
GetPrivateProfileString("Traditional","pause","",m_pause,sizeof(m_pause),filename);
GetPrivateProfileString("Traditional","stop","",m_stop,sizeof(m_stop),filename);
GetPrivateProfileString("Traditional","repeat","",m_repeat,sizeof(m_repeat),filename);
GetPrivateProfileString("Traditional","exit","",m_exit,sizeof(m_exit),filename);
GetPrivateProfileString("Traditional","volume","",m_volume,sizeof(m_volume),filename);
GetPrivateProfileString("Traditional","voladd","",m_voladd,sizeof(m_voladd),filename);
GetPrivateProfileString("Traditional","volsub","",m_volsub,sizeof(m_volsub),filename);
GetPrivateProfileString("Traditional","language","",m_language,sizeof(m_language),filename);
break;
}
}
----------------------------------------------
void CMediaPlayerDlg::SetLanguage()
{
SetDlgItemText(IDC_OPEN,m_open);
SetDlgItemText(IDC_PLAY,m_play);
SetDlgItemText(IDC_PAUSE,m_pause);
SetDlgItemText(IDC_STOP,m_stop);
SetDlgItemText(IDC_REPEAT,m_repeat);
SetDlgItemText(IDCANCEL,m_exit);
SetDlgItemText(IDC_LOWER,m_volsub);
SetDlgItemText(IDC_UPPER,m_voladd);
SetDlgItemText(IDC_STATIC,m_volume);
SetDlgItemText(IDC_STATIC_l,m_language);
}
----------------------------------------------
4.3为其它8个控件添加事件处理函数
Ctrl+w打开ClassWizard,选中messages maps
在Object IDS:清单中找到上面的7个按钮之一IDC_OPEN,
选中Messages:下面的BN_CLIKED,再点再Add Funcation
Merber function name:对话框点击OK,再点击Edit code
void CMediaPlayerDlg::OnOpen()
{
char szFileFilter[]="Mp3 File(*.mp3)|*.mp3|"
"Wma File(*.wma)|*.wma|"
"All File(*.*)|*.*||";
CFileDialog dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY,szFileFilter);
if(dlg.DoModal()==IDOK)
{
CString PathName=dlg.GetPathName();
PathName.MakeUpper();
m_ActiveMovie.SetFileName(PathName);
SetDlgItemText(IDC_STATIC_SONG,PathName);
}
用类向导分别完成其它6个控件的代码
代码参考如下!
void CMediaPlayerDlg::OnPlay()
{
SetDlgItemText(IDC_PLAY,"&Play");
m_ActiveMovie.Run();
SetTimer(0,20,NULL);
}
void CMediaPlayerDlg::OnStop()
{
m_ActiveMovie.Stop();
KillTimer(0);
}
void CMediaPlayerDlg::OnPause()
{
m_ActiveMovie.Pause();
SetDlgItemText(IDC_PLAY,"&Contine");
}
void CMediaPlayerDlg::OnLower()
{
CString v1;
long Volume=m_ActiveMovie.GetVolume();
m_ActiveMovie.Pause();
m_ActiveMovie.SetVolume(Volume - 100);
m_ActiveMovie.Run();
v1.Format(_T("volume %d"),m_ActiveMovie.GetVolume() );
SetDlgItemText(IDC_STATIC,v1);
}
void CMediaPlayerDlg::OnUpper()
{
CString v2;
long Volume=m_ActiveMovie.GetVolume();
m_ActiveMovie.Pause();
m_ActiveMovie.SetVolume(Volume + 100);
m_ActiveMovie.Run();
v2.Format(_T("volume %d"),m_ActiveMovie.GetVolume() );
SetDlgItemText(IDC_STATIC,v2);
}
void CMediaPlayerDlg::OnRepeat()
{
if(!isRepeat)
{
isRepeat=TRUE;
SetDlgItemText(IDC_STATIC2,"Status:Repeat");
}
else
{
isRepeat=FALSE;
SetDlgItemText(IDC_STATIC2,"Status:Normal");
}
}
//下拉列表语言控件事件,不同数字,加载不同语言版本
void CMediaPlayerDlg::OnEditchangeComboboxex1()
{
CString get_language;
GetDlgItemText(IDC_COMBOBOXEX1,get_language);
choice=atoi(get_language.GetBuffer(get_language.GetLength()));
GetLanguage(choice);
SetLanguage();
}
/*---------------------------------------------------------------------*/
5.添加定时函数
Ctrl+w打开ClassWizard,选中messages maps下的WM_TIMER,再选择中Add function
为它添加代码如下:
void CMediaPlayerDlg::OnTimer(UINT nIDEvent)
{
double CurrentPos=m_ActiveMovie.GetCurrentPosition();
if(CurrentPos==0&&isRepeat)
m_ActiveMovie.Run();
CDialog::OnTimer(nIDEvent);
}
/*---------------------------------------------------------------------*/
目的为使播放器具有Repeat功能,添加类的私有变量isRepeat和初始化界面语言
添加方法如下:
在头文件CMediaPlayer.h的OnInitDialog()函数:初始化代码如下:
BOOL CMediaPlayerDlg::OnInitDialog()
{
CDialog::OnInitDialog();
isRepeat=FALSE;
//初始化对话框时,自动加载一种语言版本!
int i_choice=3;
GetLanguage(i_choice);
SetLanguage();
}
/*---------------------------------------------------------------------*/
6.编译和测试
直接按Ctrl+F5,编译通过之后,再打开个mp3文件,试听一下效果!
点击语言下拉列表,全部按钮的界面文字可以自动在简体/繁体/英文3个语言中转换
有兴趣的人就试试吧,chinanetboy用vc++6.0编译通过!
/*---------------------------------------------------------------------*/