MFC中如何實作點擊按鈕可以選擇檔案夾,并獲取檔案夾中所有圖片的路徑,保存在txt中
uj5u.com熱心網友回復:
void CTextDlg::SelectDir()//手動選擇檔案夾
{
// TODO: 在此添加控制元件通知處理程式代碼
BROWSEINFO bi; //BROWSEINFO結構有很多成員引數可以自己設定
ZeroMemory(&bi,sizeof(BROWSEINFO)); //將以&bi為起始地址,大小為sizeof(BROWSEINFO)的記憶體區域用0填充
bi.ulFlags = BIF_NEWDIALOGSTYLE; //設定彈出的對話框屬性.
bi.lpszTitle="指定要處理的檔案夾"; //在視窗內顯示提示用戶的陳述句
LPITEMIDLIST pidl = SHBrowseForFolder(&bi); //Displays a dialog box enabling the user to select a Shell folder.
TCHAR * path = new TCHAR[MAX_PATH];
if(pidl != NULL)
{
SHGetPathFromIDList(pidl,path); //Converts an item identifier list to a file system path
GetDlgItem(IDC_EDIT1)->SetWindowText(path);
ReadDir(path);
}
delete path;
}
void CTextDlg::ReadDir( CString strDir )//得到選取檔案夾中的jpg圖片路徑
{
if(strDir.Right(1) != _T("\\"))
strDir += L"\\";
strDir =strDir+_T("*.*");
CFileFind finder;
CString strPath;
BOOL bWorking = finder.FindFile(strDir);
while(bWorking)
{
bWorking = finder.FindNextFile();
strPath = finder.GetFilePath();
if(finder.IsDirectory() && !finder.IsDots())
ReadDir(strPath); //遞回呼叫
else if(!finder.IsDirectory() && !finder.IsDots())
{
//strPaht就是所要獲取的檔案路徑
CString strTmp=strPath.Right(4);
strTmp.MakeLower();
if (strTmp == ".log")
{
listname.Add(strPath);//listname在頭檔案中定義為全域變數
}
}
}
finder.Close();
WriteDir();
}
void CTextDlg::WriteDir()//將得到的路徑寫入E://myfile.txt中
{
int nCount;
nCount = listname.GetSize();
char* pszFileName="E://myfile.txt";
CStdioFile myFile;
CFileException fileException;
if(myFile.Open(pszFileName,CFile::typeText|CFile::modeCreate|CFile::modeReadWrite),&fileException)
//E://myfile.txt不存在則創建
{
for (int i = 0;i < nCount;i ++)
{
CString strOrder;
strOrder = listname.GetAt(i);
CString strLine;
strLine.Format("%s\r\n",strOrder);
myFile.WriteString(strLine);
}
}
else
{
TRACE("Can't open file %s,error=%u/n",pszFileName,fileException.m_cause);
}
}
uj5u.com熱心網友回復:
里面的.log改為.jpg轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/98818.html
標籤:基礎類
上一篇:sprintf拼接char*時候,空指標被識別成“(null)”
下一篇:關于gsoap生成頭檔案報錯
