?? flefd.cpp
字號:
// FleFd.cpp : implementation file
//
#include "stdafx.h"
#include "Chapter14.h"
#include "FleFd.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CFleFd dialog
CFleFd::CFleFd(CWnd* pParent /*=NULL*/)
: CDialog(CFleFd::IDD, pParent)
{
//{{AFX_DATA_INIT(CFleFd)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
}
void CFleFd::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CFleFd)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CFleFd, CDialog)
//{{AFX_MSG_MAP(CFleFd)
ON_BN_CLICKED(IDC_dir_sel, Ondirsel)
ON_BN_CLICKED(IDC_start, Onstart)
ON_BN_CLICKED(IDC_SAVE_FILE, OnSaveFile)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CFleFd message handlers
void CFleFd::Ondirsel()
{
// TODO: Add your control notification handler code here
LPBROWSEINFO lpbi=new BROWSEINFO;
lpbi->hwndOwner=GetSafeHwnd();//NULL;
lpbi->pidlRoot=NULL;
lpbi->pszDisplayName=NULL;
lpbi->lpszTitle="請選擇要遍歷的目錄位置:";
lpbi->ulFlags=BIF_RETURNONLYFSDIRS|BIF_STATUSTEXT;
lpbi->lpfn=NULL;
//顯示外殼文件夾以便用戶選擇
LPITEMIDLIST lpitemidlist=SHBrowseForFolder(lpbi);
if(lpitemidlist==NULL)
{
delete lpbi;
lpbi = NULL;
return;
}
char path[MAX_PATH];
//轉換項目標志府列表為一個系統文件路徑
SHGetPathFromIDList(lpitemidlist,path);
delete lpbi;
//獲取控件指針
CWnd* pWnd=(CWnd*)GetDlgItem(IDC_dir);
//設置其標題為當前路徑信息
pWnd->SetWindowText(path);
//更新對話框
UpdateData(FALSE);
}
void CFleFd::BrowseDir(CString& strDir)
{
//定義查找文件對象
CFileFind cff;
CString szDir = strDir;
CString str;
//當為根目錄時,最右側為'\'
if(szDir.Right(1) != "\\")
szDir += "\\";
//所有文件
szDir += "*.*";
BOOL bResult = cff.FindFile(szDir);
while(bResult)
{
bResult = cff.FindNextFile();
if(cff.IsDirectory() && !cff.IsDots())
{
//如果是一個子目錄,用遞歸繼續往深一層找
CString strPath=cff.GetFilePath();
BrowseDir(strPath);
//目錄數目自加
ldirCount++;
}
else if(!cff.IsDirectory() && !cff.IsDots())
{
//顯示當前訪問的文件
CWnd* pWnd = (CWnd*)GetDlgItem(IDC_dir);
str.Format("%s",cff.GetFilePath());
pWnd->SetWindowText(str);
//寫到文件
ExportFile.WriteString(str+"\n");
//文件數目自加
lfileCount++;
//為了能看的清楚,可以停留幾毫秒
// Sleep(10);
}
//置空
str="";
}
cff.Close();//關閉
}
void CFleFd::Onstart()
{
// TODO: Add your control notification handler code here
CWnd* pWnd=(CWnd*)GetDlgItem(IDC_dir);
CString path,strHint;
pWnd->GetWindowText(path);
//路徑信息不能為空
if(path.IsEmpty())
{
AfxMessageBox("目錄不能為空");
return;
}
//將文件路徑保存為文件
ExportFile.Open(strDirFile,CFile::modeCreate|CFile::modeWrite,NULL);
//計數器復位
lfileCount=0;
ldirCount=0;
BrowseDir(path);
//查找完畢提示
strHint.Format("共有文件夾%d 文件%d個",ldirCount,lfileCount);
AfxMessageBox(strHint);
//關閉文件
ExportFile.Close();
}
void CFleFd::OnSaveFile()
{
// TODO: Add your control notification handler code here
CString strFilter;
strFilter="Txt Files(*.txt)|*.txt||";
CFileDialog dlg( TRUE, NULL, NULL, OFN_EXPLORER|OFN_HIDEREADONLY|
OFN_ENABLESIZING|OFN_FILEMUSTEXIST,strFilter);
dlg.m_ofn.lStructSize = sizeof(OPENFILENAME);
if(dlg.DoModal() == IDOK )
{
strDirFile=dlg.GetPathName();
}
UpdateData(FALSE);
}
BOOL CFleFd::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
//初始化計數器
lfileCount=0;
ldirCount=0;
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -