📄 simplemp3dlg.cpp
字号:
Play();
}
else
{
AddFile();
if (!Open(0))
return;
Play();
}
if (m_mcimp3.MCIIsPlaying())
m_Play.SetCheck(true);
else
m_Play.SetCheck(false);
}
void CSimpleMP3Dlg::OnPrev()
{
// TODO: Add your control notification handler code here
if (m_mcimp3.MCIIsOpened())
{ m_mcimp3.MCISeekToStart();
Play();
}
}
void CSimpleMP3Dlg::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);
// TODO: Add your message handler code here
CRect WinRect, ListRect;
if (m_List)
{ this->GetWindowRect(&WinRect);
m_List.SetWindowPos(
NULL,
0,
0,
WinRect.Width()-this->m_iWidth,
WinRect.Height()-this->m_iHeight,
SWP_NOMOVE | SWP_NOZORDER);
m_List.Invalidate();
}
}
void CSimpleMP3Dlg::OnStep()
{
// TODO: Add your control notification handler code here
DWORD dwPos;
BOOL bPlaying=m_mcimp3.MCIIsPlaying();
m_mcimp3.MCIGetPos(&dwPos);
dwPos+=5;
if (dwPos<0 || dwPos>m_mcimp3.m_dwLength)
dwPos=m_mcimp3.m_dwLength;
m_mcimp3.MCISeekTo(dwPos);
SetTime(m_bElapse ? dwPos : m_mcimp3.m_dwLength-dwPos);
m_Slider.SetPos(dwPos);
if (bPlaying)
Play();
}
void CSimpleMP3Dlg::OnStop()
{
// TODO: Add your control notification handler code here
if (!m_mcimp3.MCIIsOpened())
{ m_Stop.SetCheck(false);
return;
}
// if (!m_mcimp3.MCIIsPlaying())
// { m_Stop.SetCheck(true);
// return;
// }
m_Stop.SetCheck(true);
m_Play.SetCheck(false);
m_Pause.SetCheck(false);
Stop();
m_mcimp3.MCISeekToStart();
SetTime(0);
m_Slider.SetPos(0);
}
void CSimpleMP3Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == SC_MINIMIZE)
{
this->ShowWindow(SW_HIDE);
this->m_bIconized=true;
}
else if ((nID & 0xFFF0) == SC_RESTORE)
{
this->ShowWindow(SW_SHOW);
this->SetActiveWindow();
::SetForegroundWindow(this->GetSafeHwnd());
this->m_bIconized=false;
}
else if ((nID & 0xFFF0) == SC_CLOSE)
{
this->Exit();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
void CSimpleMP3Dlg::OnTime()
{
// TODO: Add your control notification handler code here
DWORD dwPos;
m_bElapse = !m_bElapse;
if (m_mcimp3.MCIIsOpened())
{
if (m_mcimp3.MCIGetPos(&dwPos)!=0)
{
return;
}
SetTime(m_bElapse ? dwPos : m_mcimp3.m_dwLength-dwPos);
}
}
void CSimpleMP3Dlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
DWORD dwPos;
int iNextID=0;
if (m_mcimp3.MCIIsPlaying())
{ if (m_mcimp3.MCIGetPos(&dwPos)!=0)
{ KillTimer(nIDEvent);
return;
}
SetTime(m_bElapse ? dwPos : m_mcimp3.m_dwLength-dwPos);
m_Slider.SetPos(dwPos);
}else
{ SetTime(0);
m_Slider.SetPos(0);
if ((iNextID=GetNext())>=0)
{ if (iNextID!=(int)m_iOpenedID)
{ m_iOpenedID=iNextID;
if (!Open(m_iOpenedID))
return;
}
else
m_mcimp3.MCISeekToStart();
Play();
}
}
CDialog::OnTimer(nIDEvent);
}
BOOL CSimpleMP3Dlg::PreTranslateMessage(MSG* pMsg)
{
// CG: The following block was added by the ToolTips component.
{
// Let the ToolTip process this message.
m_tooltip.RelayEvent(pMsg);
}
// 处理快捷键
if (::TranslateAccelerator(this->GetSafeHwnd(), m_hAccTable, pMsg))
{
return(TRUE);
}
return CDialog::PreTranslateMessage(pMsg); // CG: This was added by the ToolTips component.
}
// end of CSimpleMP3Dlg message handlers
/////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
/*
* 函数名称:AddDir
* 函数介绍:完成加入目录功能
* 输入参数:无
* 输出参数:无
* 返回值 :无
*/
void CSimpleMP3Dlg::AddDir()
{
CString strDirPath=_T("");
int i;
int iItemCount=m_List.GetCount();
BROWSEINFO bi = {
this->GetSafeHwnd(),
NULL,
strDirPath.GetBuffer(255),
NULL,//_T("加入目录中的所有MP3文件"),
0,
NULL,
NULL,
0};
ITEMIDLIST* piid = NULL;
// call the shell function
piid = :: SHBrowseForFolder(&bi);
strDirPath.ReleaseBuffer();
// process the result
if (piid && ::SHGetPathFromIDList(piid, strDirPath.GetBuffer(255)))
{
strDirPath.ReleaseBuffer();
i=iItemCount;
m_List.AddDir(strDirPath.GetBuffer(255));
}
else
{
return;
}
// Release the ITEMIDLIST if we got one
if (piid)
{
LPMALLOC lpMalloc;
VERIFY(::SHGetMalloc(&lpMalloc) == NOERROR);
lpMalloc->Free(piid);
lpMalloc->Release();
}
}
////////////////////////////////////////////////////////////////////////
/*
* 函数名称:CloseM3U
* 函数介绍:完成关闭M3U文件功能
* 输入参数:无
* 输出参数:无
* 返回值 :无
*/
void CSimpleMP3Dlg::CloseM3U()
{
m_mcimp3.MCIClose();
m_Play.SetCheck(FALSE);
m_Pause.SetCheck(FALSE);
m_Stop.SetCheck(FALSE);
m_iOpenedID=-1;
m_Slider.SetPos(0);
this->SetTime(0);
m_Title.SetWindowText("无");
CString strWinTitle;
strWinTitle.LoadString(IDS_TITLE);
m_pTrayIcon->ModifyTip(strWinTitle);
m_strM3UPath=m_strDefaultDir+_T("\\Default");
CString strTitle;
strTitle.LoadString(IDS_TITLE);
this->SetWindowText(strTitle);
m_List.DeleteAllFiles();
}
////////////////////////////////////////////////////////////////////////
/*
* 函数名称:Exit
* 函数介绍:退出程序
* 输入参数:无
* 输出参数:无
* 返回值 :无
*/
void CSimpleMP3Dlg::Exit()
{
CDialog::OnCancel();
DestroyWindow();
}
////////////////////////////////////////////////////////////////////////
/*
* 函数名称:GetNext
* 函数介绍:根据LOOP、REPEAT、RANDOM取得将要播放的下一首乐曲的ID(从0开始计算)
* 输入参数:无
* 输出参数:无
* 返回值 :int, 返回ID值,若列表为空或出错,则返回-1
*/
int CSimpleMP3Dlg::GetNext()
{
int iItemCount=m_List.GetCount();
int iReturn=-1;
if (iItemCount<=0) //列表为空
iReturn=-1;
else
{ if (m_Repeat.GetCheck()) //Repeat=true
{ if (m_iOpenedID>=0 && m_iOpenedID<iItemCount)
iReturn=m_iOpenedID;
else
iReturn=0;
}else if (m_Random.GetCheck()) //Random=true
iReturn=GetRandID(iItemCount);
else if (m_iOpenedID!=iItemCount-1)//不是末一首
iReturn=m_iOpenedID+1;
else if (m_Loop.GetCheck()) //Loop=true
iReturn=0;
}
return iReturn;
}
////////////////////////////////////////////////////////////////////////
/*
* 函数名称:GetPrev
* 函数介绍:根据LOOP、REPEAT、RANDOM取得将要播放的上一首乐曲的ID(从0开始计算)
* 输入参数:无
* 输出参数:无
* 返回值 :int, 返回ID值,若列表为空或出错,则返回-1
*/
int CSimpleMP3Dlg::GetPrev()
{
int iItemCount=m_List.GetCount();
int iReturn=-1;
if (iItemCount==0) //列表为空
iReturn=-1;
else
{ if (m_Repeat.GetCheck()) //Repeat=true
{ if (m_iOpenedID>=0 && m_iOpenedID<iItemCount)
iReturn=m_iOpenedID;
else
iReturn=0;
}else if (m_Random.GetCheck()) //Random=true
iReturn=GetRandID(iItemCount);
else if (m_iOpenedID!=0) //不是第一首
iReturn=m_iOpenedID-1;
else if (m_Loop.GetCheck()) //Loop=true
iReturn=0;
}
return iReturn;
}
////////////////////////////////////////////////////////////////////////
/*
* 函数名称:GetRandID
* 函数介绍:产生随机数
* 输入参数:UINT uItemCount
* 输出参数:无
* 返回值 :0 ~ uItemCount-1之间的一个随机数
*/
int CSimpleMP3Dlg::GetRandID(UINT uItemCount)
{
return rand()%uItemCount;
}
////////////////////////////////////////////////////////////////////////
/*
* 函数名称:InitInterface
* 函数介绍:初始化界面
* 输入参数:无
* 输出参数:无
* 返回值 :无
*/
void CSimpleMP3Dlg::InitInterface()
{
//窗口的宽、高
CRect WinRect, ListRect;
this->GetWindowRect(&WinRect);
m_List.GetWindowRect(&ListRect);
m_iWidth = WinRect.Width() - ListRect.Width();
m_iHeight = WinRect.Height() - ListRect.Height();
m_iMinWidth = WinRect.Width(); //窗口的最小宽度为设计宽度
m_iMinHeight = WinRect.Height(); //窗口的最小高度为设计高度
//设置按钮图案
m_Play.SetPic(IDB_BACKGROUND, IDB_PLAY);
m_Pause.SetPic(IDB_BACKGROUND, IDB_PAUSE);
m_Stop.SetPic(IDB_BACKGROUND, IDB_STOP);
m_Prev.SetPic(IDB_BACKGROUND, IDB_PREV);
m_Back.SetPic(IDB_BACKGROUND, IDB_BACK);
m_Step.SetPic(IDB_BACKGROUND, IDB_STEP);
m_Next.SetPic(IDB_BACKGROUND, IDB_NEXT);
m_Mute.SetPic(IDB_BACKGROUND, IDB_VOLUME);
m_Repeat.SetPic(IDB_BACKGROUND, IDB_REPEAT);
m_Loop.SetPic(IDB_BACKGROUND, IDB_LOOP);
m_Random.SetPic(IDB_BACKGROUND, IDB_RANDOM);
m_M3u.SetPic(IDB_BACKGROUND, IDB_M3U);
//设置Time
m_Time.Initialize();
CFont f;
f.CreateFont(24,0,0,0,FW_BOLD,0,0,0,0,0,0,0,0,"宋体");
m_Time.SetFont(&f);
m_Time.SetWindowText("00:00");
m_Time.SetTextColor(CONTROL_TEXTCOLOR);
m_Time.SetBkColor(CONTROL_BKCOLOR);
m_Time.SetTextHAlign(CScrollLabel::SL_TA_HCENTER);
m_Time.SetTextVAlign(CScrollLabel::SL_TA_VCENTER);
m_Time.SetScrollStyle(CScrollLabel::SL_NONE);
//设置Title
m_Title.Initialize();
m_Title.SetWindowText("无");
m_Title.SetTextColor(CONTROL_TEXTCOLOR);
m_Title.SetBkColor(CONTROL_BKCOLOR);
m_Title.SetTextVAlign(CScrollLabel::SL_TA_VCENTER);
m_Title.SetScrollStyle(CScrollLabel::SL_HSCROLLLEFT);
f.DeleteObject();
//设置List
m_List.SetTextColor(CONTROL_TEXTCOLOR);
m_List.SetBkColor(CONTROL_BKCOLOR);
m_List.SetSelectedBkColor(RGB(0, 0, 255));
m_List.SetSelectedTextColor(CONTROL_TEXTCOLOR);
m_List.SetHotBkColor(CONTROL_BKCOLOR);
m_List.SetHotTextColor(RGB(255, 255, 255));
//设置Slider
m_Slider.SetBkColor(DIALOG_BKCOLOR);
// 建立任务栏图标
m_pTrayIcon = new CAnimateTrayIcon(
this, // Window asossiated with the tray icon
MYWM_TRAYICON, // Callback msg ID used by the window
IDB_TRAYICON,
16,
RGB(255, 255, 255)
);
m_pTrayIcon->SetUpdateInterval(500);
CString strWinTitle;
strWinTitle.LoadString(IDS_TITLE);
m_pTrayIcon->ModifyTip(strWinTitle);
//加入Tooltip
// CG: The following block was added by the ToolTips component.
{
// Create the ToolTip control.
m_tooltip.Create(this);
m_tooltip.Activate(TRUE);
// TODO: Use one of the following forms to add controls:
// m_tooltip.AddTool(GetDlgItem(IDC_<name>), <string-table-id>);
m_tooltip.AddTool(&m_Play, _T("播放"));
m_tooltip.AddTool(&m_Pause, _T("暂停/恢复"));
m_tooltip.AddTool(&m_Stop, _T("停止"));
m_tooltip.AddTool(&m_Prev, _T("上一首"));
m_tooltip.AddTool(&m_Back, _T("向后5秒"));
m_tooltip.AddTool(&m_Step, _T("向前5秒"));
m_tooltip.AddTool(&m_Next, _T("下一首"));
m_tooltip.AddTool(&m_Mute, _T("静音"));
m_tooltip.AddTool(&m_Repeat, _T("重复"));
m_tooltip.AddTool(&m_Loop, _T("循环"));
m_tooltip.AddTool(&m_Random, _T("随机"));
//CStatic控件要设定为Notify才可显示Tooltip
m_tooltip.AddTool(&m_Time, _T("播放时间/剩余时间"));
m_tooltip.AddTool(&m_Title, _T("标题"));
m_tooltip.AddTool(&m_M3u, _T("列表菜单"));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -