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

📄 mp3listbox.cpp

📁 一些关于C++开发的多媒体制作书籍的源代码
💻 CPP
📖 第 1 页 / 共 3 页
字号:
* 函数名称:GetPath
* 函数介绍:查询Path字符串
* 输入参数:UINT uItemID, 要查询的条目
* 输出参数:无
* 返回值  :CString, Path字符串
*/
CString CMP3ListBox::GetPath(UINT uItemID) const
{
	return ((CMP3ListBoxItem *)GetItemData(uItemID))->m_Path;
}

//////////////////////////////////////////////////////////////////////////
/*
* 函数名称:OpenM3U
* 函数介绍:读取strPath所指示的M3U文件:
            if 文件格式正确
            		清空列表
            		将文件加入列表
            endif
* 输入参数:const CString &strPath, 要打开的M3U的路径
* 输出参数:无
* 返回值  :int, 加入的MP3文件的数目, -1表示读文件错误
*/
int CMP3ListBox::OpenM3U(const CString &strPath)
{
	UINT uReturn=0;
	CString sLine=_T("");
	CString sHead=_T("");
	CString sInfo=_T("");
	CString sPath=_T("");
	
	CString sLength=_T("");
	CString sTitle=_T("");
	int iMaoHao, iDouHao;
	int iLength=0;
	int iCount=0;

	TRY
	{	CStdioFile f( strPath,
			CFile::modeRead | CFile::typeText | CFile::shareExclusive);

		//读文件头"#EXTM3U"
		if (!f.ReadString(sLine))
			return 0;
		if (!(sLine=="#EXTM3U"))
		{	TRACE("文件格式错误!\n"+strPath+"\n");
			return -1;
		}
		
		// 清空列表
		DeleteAllFiles();

		while (f.ReadString(sLine))
		{	sHead=sLine.Left(7);
			sHead.MakeUpper();
			if (sHead=="#EXTINF")
			{	sInfo=sLine;
				if ((iMaoHao=sInfo.Find(':'))!=-1 &&
					(iDouHao=sInfo.Find(','))!=-1)
				{	sLength=sInfo.Mid(iMaoHao+1, iDouHao-iMaoHao);
					sTitle = sInfo.Mid(iDouHao+1);
					istrstream buffer(sLength.GetBuffer(1));
					buffer>>iLength;
					sLength.Format("%d:%02d", iLength/60, iLength%60);
					if (iLength <= 0)
						UpdateSongInfo(iCount);
				}
				continue;
			}else
			{	sPath=sLine;
				if (sTitle.IsEmpty())
				{
					GetFileTitle(sPath.GetBuffer(255), sTitle.GetBuffer(255), 255);
					sTitle.ReleaseBuffer();
					sTitle = (char)(iCount+1+48) + sTitle;
				}
				if (sLength.IsEmpty())
					sLength="00:00";
				// 往列表中加入新项...
				m_pItem = new CMP3ListBoxItem;
				m_pItem->m_Path = sPath;
				m_pItem->m_Title = sTitle;
				m_pItem->m_Length = sLength;
				CListBox::AddString((LPCTSTR)m_pItem);
//				this->InsertItem(iCount, sTitle, sLength, sPath);
//				UpdateSongInfo(iCount);
				iCount++;
				sInfo=_T("");
				sPath=_T("");
				sLength=_T("");
				sTitle=_T("");
			}
			// handle all the message in the message loop
			MSG msg;
			while( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE) )
				DispatchMessage(&msg);
		}
		f.Close();
	}CATCH( CFileException, e )
	{	TRACE("不能打开文件!\n"+strPath+"\n");
		return -1;
	}
	END_CATCH
	
	return iCount;
}

//////////////////////////////////////////////////////////////////////////
/*
* 函数名称:SaveM3U
* 函数介绍:保存M3U文件:
            将列表中的MP3文件信息、路径保存到strPath指示的M3U文件, 列表为空照样保存
* 输入参数:const CString &strPath, M3U的保存路径
* 输出参数:无
* 返回值  :int, 保存的条目, -1表示保存出错
*/
int CMP3ListBox::SaveM3U(const CString &strPath)
{
	CString sPath=_T("");	
	CString sLength=_T("");
	CString sTitle=_T("");
	int i;
	int iCount=this->GetCount();

	TRY
	{	CStdioFile f( strPath,
			CFile::modeCreate |
			CFile::typeText |
			CFile::shareExclusive |
			CFile::modeWrite);

		//写文件头"#EXTM3U"
		f.WriteString("#EXTM3U\n");

		for (i=0; i<iCount;i++)
		{	sTitle=this->GetRawTitle(i);
			sLength=this->GetRawLength(i);
			sPath=this->GetPath(i);

			f.WriteString("#EXTINF:" + sLength + "," + sTitle + "\n");
			f.WriteString(sPath+"\n");
		}
		f.Close();
	}CATCH( CFileException, e )
	{	TRACE("不能保存文件!"+sPath+"\n");
		return -1;
	}
	END_CATCH

	return iCount;
}

/////////////////////////////////////////////////////////////////////////////
/*
* 函数名称:UpdateSongInfo
* 函数介绍:用MP3文件信息更新指定项的m_Title和m_Length
            读取uItemID项的SongArtist和SongName,更新该项的m_Title和m_Length
            若不能读取或SongArtist和SongName都为空,则以文件名(Title)更新
            调用前要求对应项的m_Path不为空
* 输入参数:UINT uItemID, 要更新的项
* 输出参数:无
* 返回值  :无
*/
void CMP3ListBox::UpdateSongInfo(UINT uItemID)
{
//	ASSERT(uItemID < (UINT)(this->GetItemCount()));

	//更新Title, 格式为:
	//  作者 - 歌名
	CString strSongArtist=_T(""),
			strSongTitle=_T(""), 
			strNewTitle,
			strPath;
	strPath= this->GetPath(uItemID);;
//	ASSERT(!strPath.IsEmpty());
	if (strPath.IsEmpty())
		return;
	GetFileTitle(strPath, strSongTitle.GetBuffer(255), 255);
	strSongTitle.ReleaseBuffer();

	m_pItem = (CMP3ListBoxItem *)GetItemData(uItemID);

	strNewTitle =  strSongTitle;
	m_pItem->m_Title = strNewTitle;
	if (m_id3tag.Open(strPath.GetBuffer(255)) && m_id3tag.GetData())
	{
		strSongArtist = m_id3tag.GetSongArtist();
		strSongTitle = m_id3tag.GetSongTitle();
		if (!strSongTitle.IsEmpty())
		{
			if (!strSongArtist.IsEmpty())
			{
				strNewTitle.Format("%s - %s", strSongArtist, strSongTitle);
				m_pItem->m_Title = strNewTitle;
			}
			else
			{
				strNewTitle.Format("%s", strSongTitle);
				m_pItem->m_Title = strNewTitle;
			}
		}
	}

	//更新Length格式为:
	//      00:00
	CFileStatus rStatus;
	DWORD dwLength=0;
	if(CFile::GetStatus(strPath, rStatus))
		dwLength=(DWORD)(rStatus.m_size/16000);
	CString strLength;
	strLength.Format("%d:%02d", dwLength/60, dwLength%60);
	m_pItem->m_Length = strLength;

	// 重画该项
	CRect rect;
	this->GetItemRect(uItemID, &rect);
	this->InvalidateRect(&rect);
}

////////////////////////////////////////////////////////////////////////
/*
* 函数名称:SetHotItem
* 函数介绍:将第uHotItemID项设置为唯一的选定项(设置有别于其他项的标志)
* 输入参数:UINT uHotItemID, 选定项
* 输出参数:无
* 返回值  :无
*/
void CMP3ListBox::SetHotItem(UINT uHotItemID)
{
	// AVOID TO REDRAW THE ENTIRE CONTROL
	int iOldHot = this->m_iHotItemID;
	this->m_iHotItemID=uHotItemID;
	CRect itemrect;
	this->GetItemRect(iOldHot, &itemrect);
	this->InvalidateRect(&itemrect);
	this->GetItemRect(m_iHotItemID, &itemrect);
	this->InvalidateRect(&itemrect);

	int iFirstVisibleItem = this->GetTopIndex();	// 第一可见项
	int iVisibleItems;		// 当前可见项数目
	CRect rect;
	this->GetClientRect(&rect);
	iVisibleItems = rect.Height() / itemrect.Height();
	// 选定项不可见...
	if ((int)uHotItemID<iFirstVisibleItem || (int)uHotItemID>iFirstVisibleItem+iVisibleItems)
	{
		this->SetTopIndex(uHotItemID);
	}
}

////////////////////////////////////////////////////////////////////////
/*
* 函数名称:GetHotItem
* 函数介绍:查询Hot列表项的ID
* 输入参数:无
* 输出参数:无
* 返回值  :Hot列表项的ID
*/
UINT CMP3ListBox::GetHotItem() const
{
	return this->m_iHotItemID;
}

////////////////////////////////////////////////////////////////////////
/*
* 函数名称:SetBkColor
* 函数介绍:设定列表项的底色
* 输入参数:COLORREF, 列表项的底色
* 输出参数:无
* 返回值  :无
*/
void CMP3ListBox::SetBkColor(COLORREF crColor)
{
	this->m_crNormalBk=crColor;
	::DeleteObject(this->m_hBrush);
	this->m_hBrush = (HBRUSH)::CreateSolidBrush(this->m_crNormalBk);
//	CListBox::SetBkColor(crColor);
}

////////////////////////////////////////////////////////////////////////
/*
* 函数名称:GetBkColor
* 函数介绍:查询列表项的底色
* 输入参数:无
* 输出参数:无
* 返回值  :COLORREF, 列表项的底色
*/
COLORREF CMP3ListBox::GetBkColor() const
{
	return this->m_crNormalBk;
}

////////////////////////////////////////////////////////////////////////
/*
* 函数名称:SetSelectedBkColor
* 函数介绍:设定选定列表项的底色
* 输入参数:COLORREF, 选定列表项的底色
* 输出参数:无
* 返回值  :无
*/
void CMP3ListBox::SetSelectedBkColor(COLORREF crColor)
{
	this->m_crSelectedBk=crColor;
}

////////////////////////////////////////////////////////////////////////
/*
* 函数名称:GetSelectedBkColor
* 函数介绍:查询选定列表项的底色
* 输入参数:无
* 输出参数:无
* 返回值  :COLORREF, 选定列表项的底色
*/
COLORREF CMP3ListBox::GetSelectedBkColor() const
{
	return this->m_crSelectedBk;
}

////////////////////////////////////////////////////////////////////////
/*
* 函数名称:SetHotBkColor
* 函数介绍:设定Hot列表项的底色
* 输入参数:COLORREF, Hot列表项的底色
* 输出参数:无
* 返回值  :无
*/
void CMP3ListBox::SetHotBkColor(COLORREF crColor)
{
	this->m_crHotBk=crColor;
}

////////////////////////////////////////////////////////////////////////
/*
* 函数名称:GetHotBkColor
* 函数介绍:查询Hot列表项的底色
* 输入参数:无
* 输出参数:无
* 返回值  :COLORREF, Hot列表项的底色
*/
COLORREF CMP3ListBox::GetHotBkColor() const
{
	return this->m_crHotBk;
}

////////////////////////////////////////////////////////////////////////
/*
* 函数名称:SetTextColor
* 函数介绍:设定列表项的文字颜色
* 输入参数:COLORREF, 列表项的文字颜色
* 输出参数:无
* 返回值  :无
*/
void CMP3ListBox::SetTextColor(COLORREF crColor)
{
	this->m_crNormalText=crColor;
//	CListBox::SetTextColor(crColor);
}

////////////////////////////////////////////////////////////////////////
/*
* 函数名称:GetTextColor
* 函数介绍:查询列表项的文字颜色
* 输入参数:无
* 输出参数:无
* 返回值  :COLORREF, 列表项的文字颜色
*/
COLORREF CMP3ListBox::GetTextColor() const
{
	return this->m_crNormalText;
}

////////////////////////////////////////////////////////////////////////
/*
* 函数名称:SetSelectedTextColor
* 函数介绍:设定选定列表项的文字颜色
* 输入参数:COLORREF, 选定列表项的文字颜色
* 输出参数:无
* 返回值  :无
*/
void CMP3ListBox::SetSelectedTextColor(COLORREF crColor)
{
	this->m_crSelectedText=crColor;
}

////////////////////////////////////////////////////////////////////////
/*
* 函数名称:GetSelectedTextColor
* 函数介绍:查询选定列表项的文字颜色
* 输入参数:无

⌨️ 快捷键说明

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