www.pudn.com > VC写的MP3播放器源代码.zip > FileListDlg.cpp


// FileListDlg.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "RichiMP3.h" 
#include "FileListDlg.h" 
#include "SBDestination.h" 
#include "ITFindFiles.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CFileListDlg dialog 
 
 
CFileListDlg::CFileListDlg(CWnd* pParent /*=NULL*/) 
: CBitmapDialog(CFileListDlg::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CFileListDlg) 
	// NOTE: the ClassWizard will add member initialization here 
	//}}AFX_DATA_INIT 
	pFileObject = NULL; 
	m_bArchived = 2; 
	m_bCompressed = 2; 
	m_bDirectory = 2; 
	m_bHidden = 2; 
	m_bNormal = 2; 
	m_bOffline = 2; 
	m_bReadOnly = 2; 
	m_bIncludeSubfolders = TRUE; 
	m_bSystem = 2; 
	m_bTemporary = 2; 
	nCurSel = -1; 
} 
 
 
void CFileListDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CBitmapDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CFileListDlg) 
	DDX_Control(pDX, IDC_LIST1, m_List); 
	//}}AFX_DATA_MAP 
} 
 
 
BEGIN_MESSAGE_MAP(CFileListDlg, CBitmapDialog) 
//{{AFX_MSG_MAP(CFileListDlg) 
ON_WM_CTLCOLOR() 
ON_WM_LBUTTONDOWN() 
ON_WM_LBUTTONUP() 
ON_BN_CLICKED(IDC_BUTTON_ABOUT, OnButtonAbout) 
ON_BN_CLICKED(IDC_BUTTON_CLOSE, OnButtonClose) 
ON_BN_CLICKED(IDC_BUTTON_HELP, OnButtonHelp) 
ON_BN_CLICKED(IDC_BUTTON_MIN, OnButtonMin) 
ON_LBN_DBLCLK(IDC_LIST1, OnSelchangeList1) 
ON_BN_CLICKED(IDC_BUTTON_SAVE, OnButtonSave) 
ON_BN_CLICKED(IDC_BUTTON1, OnButton1) 
ON_BN_CLICKED(IDC_BUTTON2, OnButton2) 
ON_BN_CLICKED(IDC_BUTTON3, OnButton3) 
ON_BN_CLICKED(IDC_BUTTON4, OnButton4) 
//}}AFX_MSG_MAP 
ON_MESSAGE(WM_PLAYCHANGED,OnPlayChange) 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CFileListDlg message handlers 
 
BOOL CFileListDlg::OnInitDialog()  
{ 
	CBitmapDialog::OnInitDialog(); 
	SetBitmap(IDB_BITMAP_BACK2); 
	 
	VERIFY(Button1.SubclassDlgItem(IDC_BUTTON1, this)); 
	VERIFY(Button1.LoadBitmaps(_T("addu"), _T("addd"), _T("addf"),_T("addx"))); 
	 
	VERIFY(Button2.SubclassDlgItem(IDC_BUTTON2, this)); 
	VERIFY(Button2.LoadBitmaps(_T("shaofileu"), _T("shaofiled"),  _T("shaofilef"), _T("shaofilex"))); 
	 
	VERIFY(Button3.SubclassDlgItem(IDC_BUTTON3, this)); 
	VERIFY(Button3.LoadBitmaps(_T("deletefileu"), _T("deletefiled"), _T("deletefilef"),_T("deletefilex"))); 
	 
	VERIFY(Button4.SubclassDlgItem(IDC_BUTTON4, this)); 
	VERIFY(Button4.LoadBitmaps(_T("pathu"), _T("pathd"),  _T("pathf"), _T("pathx"))); 
	 
	VERIFY(ButtonSave.SubclassDlgItem(IDC_BUTTON_SAVE, this)); 
	VERIFY(ButtonSave.LoadBitmaps(_T("savefileu"), _T("savefiled"), _T("savefilef"),_T("savefilex"))); 
	 
	// TODO: Add extra initialization here 
	VERIFY(btnHelp.SubclassDlgItem(IDC_BUTTON_HELP, this)); 
	VERIFY(btnHelp.LoadBitmaps(_T("helpu"), _T("helpd"), _T("helpf"))); 
	 
	 
	VERIFY(btnAbout.SubclassDlgItem(IDC_BUTTON_ABOUT, this)); 
	VERIFY(btnAbout.LoadBitmaps(_T("aboutu"), _T("aboutd"), _T("aboutf"))); 
	 
	 
	VERIFY(btnMin.SubclassDlgItem(IDC_BUTTON_MIN, this)); 
	VERIFY(btnMin.LoadBitmaps(_T("minu"), _T("mind"), _T("minf"))); 
	 
	 
	VERIFY(btnClose.SubclassDlgItem(IDC_BUTTON_CLOSE, this)); 
	VERIFY(btnClose.LoadBitmaps(_T("closeu"), _T("closed"), _T("closef"))); 
	m_hbrDlg = ::CreateSolidBrush(RGB(255,213,3)); 
	 
	 
	CListBox *pListBox = (CListBox *)GetDlgItem(IDC_LIST1); 
	pListBox->ResetContent(); 
	CPictureList *pList = (CPictureList *)GetApp()->GetPictureList(); 
	POSITION pos = pList->GetHeadPosition(); 
	while(pos != NULL) 
	{ 
		CPictureObject *pObject = (CPictureObject *)pList->GetNext(pos); 
		CString strFileName; 
		CString strName; 
		strFileName = pObject->GetFileName(); 
		int n = strFileName.ReverseFind('\\'); 
		int n1 = strFileName.GetLength(); 
		if(n>0) 
		{ 
			strName = strFileName.Right(n1-n-1); 
		} 
		 
		int nCurSel = pListBox->AddString(strName); 
		pListBox->SetItemData(nCurSel,(DWORD)pObject); 
		 
	} 
	if(pListBox->GetCount() >=0) 
	{ 
		pListBox->SetCurSel(0); 
	} 
	return TRUE;  // return TRUE unless you set the focus to a control 
	// EXCEPTION: OCX Property Pages should return FALSE 
} 
 
HBRUSH CFileListDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)  
{ 
	// TODO: Change any attributes of the DC here 
	if(nCtlColor == CTLCOLOR_STATIC) 
	{ 
		pDC->SetBkColor(RGB(255,213,3)); 
		pDC->SetTextColor(RGB(0,192,192)); 
	} 
	 
	if(nCtlColor == CTLCOLOR_LISTBOX) 
	{ 
		pDC->SetBkColor(RGB(27,161,216)); 
		pDC->SetTextColor(RGB(0,255,0)); 
		 
	} 
     
	return m_hbrDlg; 
	 
} 
 
void CFileListDlg::OnLButtonDown(UINT nFlags, CPoint point)  
{ 
	// TODO: Add your message handler code here and/or call default 
	CRect rcCaption; 
	GetClientRect(&rcCaption); 
	rcCaption = CRect(rcCaption.left+14,rcCaption.top,rcCaption.right-25,rcCaption.bottom); 
	//ScreenToClient(&rcCaption); 
	/*	if(rc3.PtInRect(point)) 
	{ 
	 
	  return; 
	  } 
	  if(rc4.PtInRect(point)) 
	  { 
	   
		return; 
		} 
		if(rc2.PtInRect(point)) 
		{ 
		return; 
		} 
		if(rc1.PtInRect(point)) 
		{ 
		 
		  return; 
		  //PostMessage(WM_SYSCOMMAND,SC_MINIMIZE); 
}*/ 
	if(rcCaption.PtInRect(point)) 
	{ 
		 
		SendMessage(WM_NCLBUTTONDOWN,HTCAPTION,MAKELPARAM(point.x,point.y)); 
	} 
	CBitmapDialog::OnLButtonDown(nFlags, point); 
} 
 
void CFileListDlg::OnLButtonUp(UINT nFlags, CPoint point)  
{ 
	// TODO: Add your message handler code here and/or call default 
	 
	CBitmapDialog::OnLButtonUp(nFlags, point); 
} 
 
void CFileListDlg::OnButtonAbout()  
{ 
	// TODO: Add your control notification handler code here 
	 
} 
 
void CFileListDlg::OnButtonClose()  
{ 
	// TODO: Add your control notification handler code here 
	PostMessage(WM_SYSCOMMAND,SC_CLOSE); 
} 
 
void CFileListDlg::OnButtonHelp()  
{ 
	// TODO: Add your control notification handler code here 
	theApp.WinHelp(0, HELP_FINDER); 
} 
 
void CFileListDlg::OnButtonMin()  
{ 
	// TODO: Add your control notification handler code here 
	PostMessage(WM_SYSCOMMAND,SC_MINIMIZE); 
} 
 
void CFileListDlg::OnSelchangeList1()  
{ 
	// TODO: Add your control notification handler code here 
	CListBox *pListBox = (CListBox *)GetDlgItem(IDC_LIST1); 
	int nSel = pListBox->GetCurSel(); 
	nCurSel = nSel; 
	pFileObject = (CPictureObject *)pListBox->GetItemData(nSel); 
	GetParent()->SendMessage(WM_SETCHANGED); 
} 
 
void CFileListDlg::OnButtonSave()  
{ 
	// TODO: Add your control notification handler code here 
	static char szFilter[]="m3u文件(*.m3u)|*.m3u||"; 
	CFileDialog dlg(FALSE,"相马MP3播放器",NULL,OFN_HIDEREADONLY| OFN_OVERWRITEPROMPT,szFilter,this); 
	if(dlg.DoModal()) 
	{ 
		CString string; 
		string = dlg.GetPathName(); 
		CFile fi; 
		if(fi.Open(string,CFile::modeCreate|CFile::modeWrite)) 
		{ 
			fi.Close(); 
		} 
		GetApp()->Write(string); 
	} 
} 
 
void CFileListDlg::OnButton1()  
{ 
	// TODO: Add your control notification handler code here 
	static char szFilter[]="mp3文件(*.mp3)|*.mp3|m3u文件(*.m3u)|*.m3u||"; 
	CFileDialog dlg(TRUE,"相马MP3播放器",NULL,OFN_HIDEREADONLY| OFN_OVERWRITEPROMPT,szFilter,this); 
	dlg.m_ofn.Flags |= OFN_ALLOWMULTISELECT; 
    CString		csfncbl; 
	int res = dlg.DoModal(); 
   	if(res==IDOK) 
	{ 
		 
		CString cs = (LPCSTR)dlg.GetPathName(); 
		POSITION pos = dlg.GetStartPosition(); 
		if(pos != NULL) 
		{ 
			do{ 
				CString strExt; 
				strExt = dlg.GetFileExt(); 
				strExt.MakeLower(); 
				if(strExt == _T("m3u")) 
				{ 
					csfncbl =  dlg.GetNextPathName( pos); 
					ReadFromM3U(csfncbl); 
				} 
				else 
				{ 
					csfncbl =  dlg.GetNextPathName( pos); 
					CString strName; 
					int n = csfncbl.ReverseFind('\\'); 
					int n1 = csfncbl.GetLength(); 
					if(n>0) 
					{ 
						strName = csfncbl.Right(n1-n-1); 
					} 
					//if(m_list.FindString(1,strName)<0) 
					//	{ 
					CPictureObject *pObj = new CPictureObject; 
					pObj->SetFileName(csfncbl); 
					GetApp()->AddPictureObject(pObj); 
					//	int nCur = m_List.AddString(strName); 
					//	m_List.SetItemData(nCur,(DWORD)pObj); 
					//	} 
				} 
				 
			}while(pos!=NULL); 
		} 
		UpdateListBox(); 
        m_List.SetCurSel(nCurSel); 
	} 
} 
 
void CFileListDlg::OnButton2()  
{ 
	// TODO: Add your control notification handler code here 
	int nCount = m_List.GetCount(); 
	int nTemp = m_List.GetCurSel(); 
	if(nTemp == -1) 
		return; 
	CPictureObject *pObj = (CPictureObject *)m_List.GetItemData(nTemp); 
	GetApp()->RemovePictureObject(pObj); 
	//	m_List.DeleteString(nTemp); 
	UpdateListBox(); 
	if(nTemp == nCount) 
	{ 
		m_List.SetCurSel(0); 
		nCurSel = 0; 
	} 
	else if(nTemp <= 0) 
	{ 
		if(nCount>1) 
		{ 
			m_List.SetCurSel(nCount-1); 
			nCurSel = nCount -1; 
		} 
	} 
	else 
	{ 
		m_List.SetCurSel(nTemp -1); 
		nCurSel = nTemp -1; 
	} 
} 
 
void CFileListDlg::OnButton3()  
{ 
	// TODO: Add your control notification handler code here 
	int nCur = m_List.GetCurSel(); 
	int nCount = m_List.GetCount(); 
	if(nCount <0) 
		return; 
	for(int n = 0;nRemovePictureObject(pObj); 
		} 
	} 
	UpdateListBox(); 
	nCurSel = 0; 
} 
 
void CFileListDlg::OnButton4()  
{ 
	// TODO: Add your control notification handler code here 
	UpdateData(TRUE); 
	CSBDestination sb(m_hWnd, IDS_STRING2); 
	sb.SetFlags(BIF_RETURNONLYFSDIRS | BIF_STATUSTEXT); 
	CString SelPath; 
	SelPath = "c:\\"; 
	//	CEdit *pEdit = (CEdit *)GetDlgItem(IDC_PATH); 
	sb.SetInitialSelection(SelPath); 
	if (sb.SelectFolder()) 
	{ 
		SelPath = sb.GetSelectedFolder(); 
		//		::SetCurrentDirectory(SelPath); 
		//pEdit->SetWindowText(SelPath); 
		FindProjectFiles(SelPath,"*.mp3"); 
		UpdateData(FALSE); 
	} 
} 
#include  
#include  
void CFileListDlg::ReadFromM3U(const char * cFileName) 
{ 
	char buf[1024]; 
	ifstream ifs(cFileName); 
	while (ifs.good()) 
	{ 
		ifs.getline(buf, 1024); 
		CString cs(buf); 
		if(!cs.IsEmpty()) 
		{ 
			 
			CString strName; 
			int n = cs.ReverseFind('\\'); 
			int n1 = cs.GetLength(); 
			if(n>0) 
			{ 
				strName = cs.Right(n1-n-1); 
			} 
			//if(m_List.FindString(1,strName)<0) 
			//	{ 
			CPictureObject *pObj = new CPictureObject; 
			pObj->SetFileName(cs); 
			GetApp()->AddPictureObject(pObj); 
			int nCur = m_List.AddString(strName); 
			m_List.SetItemData(nCur,(DWORD)pObj); 
			 
		} 
	} 
	 
} 
static BOOL CALLBACK CountFilesFound(ITCFindFiles* pFind, LONG lParam) 
{ 
	(*((LONG*)lParam))++; 
	return TRUE; // Continue search 
} 
void CFileListDlg::FindProjectFiles(CString strDirectory,CString strFileName)  
{ 
	 
	CWaitCursor wait; // Changes cursor 
	 
	 
	 
	if (!UpdateData(TRUE)) 
		return; 
	 
	DWORD dwAttribSet = 0; 
	if (m_bArchived == 1) 
		dwAttribSet |= FILE_ATTRIBUTE_ARCHIVE; 
	if (m_bCompressed == 1) 
		dwAttribSet |= FILE_ATTRIBUTE_COMPRESSED; 
	if (m_bDirectory == 1) 
		dwAttribSet |= FILE_ATTRIBUTE_DIRECTORY; 
	if (m_bHidden == 1) 
		dwAttribSet |= FILE_ATTRIBUTE_HIDDEN; 
	if (m_bNormal == 1) 
		dwAttribSet |= FILE_ATTRIBUTE_NORMAL; 
	if (m_bOffline == 1) 
		dwAttribSet |= FILE_ATTRIBUTE_OFFLINE; 
	if (m_bReadOnly == 1) 
		dwAttribSet |= FILE_ATTRIBUTE_READONLY; 
	if (m_bSystem == 1) 
		dwAttribSet |= FILE_ATTRIBUTE_SYSTEM; 
	if (m_bTemporary == 1) 
		dwAttribSet |= FILE_ATTRIBUTE_TEMPORARY; 
	 
	DWORD dwAttribClear = 0; 
	if (m_bArchived == 0) 
		dwAttribClear |= FILE_ATTRIBUTE_ARCHIVE; 
	if (m_bCompressed == 0) 
		dwAttribClear |= FILE_ATTRIBUTE_COMPRESSED; 
	if (m_bDirectory == 0) 
		dwAttribClear |= FILE_ATTRIBUTE_DIRECTORY; 
	if (m_bHidden == 0) 
		dwAttribClear |= FILE_ATTRIBUTE_HIDDEN; 
	if (m_bNormal == 0) 
		dwAttribClear |= FILE_ATTRIBUTE_NORMAL; 
	if (m_bOffline == 0) 
		dwAttribClear |= FILE_ATTRIBUTE_OFFLINE; 
	if (m_bReadOnly == 0) 
		dwAttribClear |= FILE_ATTRIBUTE_READONLY; 
	if (m_bSystem == 0) 
		dwAttribClear |= FILE_ATTRIBUTE_SYSTEM; 
	if (m_bTemporary == 0) 
		dwAttribClear |= FILE_ATTRIBUTE_TEMPORARY; 
	 
	ITCFindFiles find; 
	DWORD dwFindFlags = ITCFindFiles::None; 
	 
	if (m_bIncludeSubfolders) 
		dwFindFlags |= ITCFindFiles::IncludeSubfolders; 
	 
	find.NewSearch(strDirectory, strFileName, 
		0, dwFindFlags); 
	find.SetFindAttributes(dwAttribSet, dwAttribClear); 
	 
	LONG lCount = 0; 
	find.SetCallback(CountFilesFound, (LONG)&lCount); 
	 
	while (find.NextFile()) 
	{ 
		CString strPath; 
		strPath = find.GetFilePath(); 
		CString strName; 
		strName = find.GetFileName(); 
		//			if(m_List.FindString(1,strName)<0) 
		//	{ 
		CPictureObject *pObj = new CPictureObject; 
		pObj->SetFileName(strPath); 
		GetApp()->AddPictureObject(pObj); 
		//	int nCur = m_List.AddString(find.GetFileName()); 
		//	m_List.SetItemData(nCur,(DWORD)pObj); 
		//	} 
	} 
	UpdateListBox(); 
	m_List.SetCurSel(nCurSel); 
	 
	 
	UpdateData(FALSE); 
	 
} 
LRESULT CFileListDlg::OnPlayChange(WPARAM wp, LPARAM lp) 
{ 
	m_List.SetCurSel(nCurSel); 
	return 0L; 
} 
void CFileListDlg::UpdateListBox() 
{ 
	CListBox *pListBox = (CListBox *)GetDlgItem(IDC_LIST1); 
	pListBox->ResetContent(); 
	CPictureList *pList = (CPictureList *)GetApp()->GetPictureList(); 
	POSITION pos = pList->GetHeadPosition(); 
	while(pos != NULL) 
	{ 
		CPictureObject *pObject = (CPictureObject *)pList->GetNext(pos); 
		CString strFileName; 
		CString strName; 
		strFileName = pObject->GetFileName(); 
		int n = strFileName.ReverseFind('\\'); 
		int n1 = strFileName.GetLength(); 
		if(n>0) 
		{ 
			strName = strFileName.Right(n1-n-1); 
		} 
		 
		int nCurSel = pListBox->AddString(strName); 
		pListBox->SetItemData(nCurSel,(DWORD)pObject); 
		 
	} 
	 
	GetParent()->SendMessage(WM_FILECHANGED); 
}