📄 filesstructuredlg.cpp
字号:
// FilesStructureDlg.cpp : implementation file
//
#include "stdafx.h"
#include "FilesStructure.h"
#include "FilesStructureDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CFilesStructureDlg dialog
int CFilesStructureDlg::nSpace = 0;
CFilesStructureDlg::CFilesStructureDlg(CWnd* pParent /*=NULL*/)
: CDialog(CFilesStructureDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CFilesStructureDlg)
m_strDirctName = _T("");
m_strReportFileName = _T("");
m_bIncludeHidden = FALSE;
m_bIncludeSystem = FALSE;
m_bOnlyDirectory = TRUE;
m_strStatistic = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CFilesStructureDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CFilesStructureDlg)
DDX_Control(pDX, IDC_BTN_GENERATE_REPORT, m_ctrlGenerateReport);
DDX_Text(pDX, IDC_EDT_DIRECTORY_NAME, m_strDirctName);
DDX_Text(pDX, IDC_EDT_REPORT_NAME, m_strReportFileName);
DDX_Check(pDX, IDC_CHK_INCLUDE_HIDDEN, m_bIncludeHidden);
DDX_Check(pDX, IDC_CHK_INCLUDE_SYSTEM, m_bIncludeSystem);
DDX_Check(pDX, IDC_CHK_ONLY_DIRECTORY, m_bOnlyDirectory);
DDX_Text(pDX, IDC_STC_STATISTIC, m_strStatistic);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CFilesStructureDlg, CDialog)
//{{AFX_MSG_MAP(CFilesStructureDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BTN_OPEN_DIRECTORY, OnBtnOpenDirectory)
ON_BN_CLICKED(IDC_BTN_GENERATE_REPORT, OnBtnGenerateReport)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CFilesStructureDlg message handlers
BOOL CFilesStructureDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
m_ctrlGenerateReport.EnableWindow(FALSE);
// GetDlgItem(IDC_STC_STATUS) ->ShowWindow(SW_SHOW);
GetDlgItem(IDC_STC_STATUS) ->SetWindowText("Please open directory first!");
GetDlgItem(IDC_STC_BROWSED)->ShowWindow(SW_HIDE);
GetDlgItem(IDC_STC_STATISTIC)->ShowWindow(SW_HIDE);
m_HBrowseHandle = NULL;
m_nBrowsedFiles = 0;
return TRUE; // return TRUE unless you set the focus to a control
}
void CFilesStructureDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CFilesStructureDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CFilesStructureDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
/////////////////////////////////////////////////////////////////////
//功 能:打开一个要生成目录结构的文件夹
//参 数:
//返回值:
/////////
void CFilesStructureDlg::OnBtnOpenDirectory()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
BROWSEINFO browse;
TCHAR szDisplayName[MAX_PATH]="";
LPITEMIDLIST pidl;
LPMALLOC pMalloc = NULL;
ZeroMemory(&browse, sizeof(browse));
browse.hwndOwner = GetSafeHwnd();
browse.pszDisplayName = szDisplayName;
browse.lpszTitle = TEXT("Please select a folder:");
browse.ulFlags = BIF_RETURNONLYFSDIRS;
pidl = SHBrowseForFolder(&browse);
if (pidl)
{//1
//自动获得目录结构文件名
m_strReportFileName = browse.pszDisplayName;
int npos = m_strReportFileName.Find(':');
//文件名中不能含有“:”,主要对选择盘符时的情况
if( npos != -1 )
{//2
m_strReportFileName.Delete(npos, 1);
}//2
m_strReportFileName += ".txt";
SHGetPathFromIDList(pidl, szDisplayName); //设置文件夹名,包含路径.
m_strDirctName = szDisplayName;
m_ctrlGenerateReport.EnableWindow(TRUE);
GetDlgItem(IDC_STC_STATUS) ->SetWindowText("Assign a name then Generate Report!");
UpdateData(FALSE);
} //1
// free the pidl using the shell's task allocator.
if (SHGetMalloc(&pMalloc) == NOERROR)
pMalloc->Free((void *)pidl);
GetDlgItem(IDC_STC_BROWSED)->ShowWindow(SW_HIDE);
GetDlgItem(IDC_STC_STATISTIC)->ShowWindow(SW_HIDE);
}
/////////////////////////////////////////////////////////////////////
//功 能:生成目录结构
//参 数:
//返回值:
/////////
void CFilesStructureDlg::OnBtnGenerateReport()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
m_nBrowsedFiles = 0;
if(!m_fFile.Open(m_strReportFileName, CFile::modeCreate|CFile::modeWrite|
CFile::typeText) )
{
AfxMessageBox("Can not create file.");
return;
}
GetDlgItem(IDC_STC_STATUS) ->ShowWindow(SW_SHOW);
GetDlgItem(IDC_STC_STATUS) ->SetWindowText("Processing, Please Waiting...");
//增加标题
m_fFile.WriteString("------------------FILE'S STRUCTURE AND LIST------------------\n");
//时间戳
COleDateTime time;
time = COleDateTime::GetCurrentTime();
CString strTemp = time.Format(_T("%Y-%m-%d %H:%M:%S" ));
m_fFile.WriteString(" Created Time is: ");
m_fFile.WriteString(strTemp);
m_fFile.WriteString("\n");
m_fFile.WriteString("-------------------------------------------------------------");
m_fFile.WriteString("\n");
m_fFile.WriteString(m_strDirctName);
m_fFile.WriteString("\n");
//处理路径分隔符“\”
CString strName = m_strDirctName;
int nPos = strName.Find("\\", 0);
do
{
if(nPos != -1)
strName.Insert(nPos, "\\");
nPos = strName.Find("\\", nPos+2);
}while( nPos != -1 );
//先停止线程(如果线程已启动)再启动
//*
EndThreadBrowse();
BeginBrowseThread();
GetDlgItem(IDC_STC_BROWSED)->ShowWindow(SW_SHOW);
GetDlgItem(IDC_STC_STATISTIC)->ShowWindow(SW_SHOW);
//*/
/* no thread
nSpace = 0;
UpdateData(FALSE);
RecurFind((LPCTSTR)m_strDirctName);
GetDlgItem(IDC_STC_STATUS) ->SetWindowText("Completed!");
GetDlgItem(IDC_STC_BROWSED)->ShowWindow(SW_SHOW);
GetDlgItem(IDC_STC_STATISTIC)->ShowWindow(SW_SHOW);
m_fFile.Close();
//*/
UpdateData(FALSE);
}
/////////////////////////////////////////////////////////////////////
//功 能:递归方式查找文件
//参 数:strName:待查找的文件路径名
//返回值:
/////////
void CFilesStructureDlg::RecurFind(LPCTSTR strName)
{
CFileFind finder;
// build a string with wildcards
CString strWildcard(strName);
strWildcard += _T("\\*.*");
BOOL bWorking = finder.FindFile(strWildcard);
while (bWorking)
{
CString strLineText = "";
bWorking = finder.FindNextFile();
if (finder.IsDots())
continue;
if(!m_bIncludeHidden && finder.IsHidden())
continue;
if(!m_bIncludeSystem && finder.IsSystem())
continue;
if(m_bOnlyDirectory && !finder.IsDirectory())
continue;
//打印空格及格线
for(int i=0; i<nSpace; i++)
{
strLineText += "│";
strLineText += " ";
}
strLineText += "├─";
//打印文件名
CString strTemp;
strTemp = finder.GetFileName();
strLineText += strTemp;
if(finder.IsSystem())
strLineText += " *SYSTEM*";
if(finder.IsHidden())
strLineText += " *HIDDEN*";
//如果是文件夹,输出创建日期
if(finder.IsDirectory())
{
CTime tTime;
finder.GetCreationTime(tTime);
// strTemp = Format("%c");
strTemp = tTime.Format(_T("%Y-%m-%d %H:%M:%S" ));
strLineText += " (FOLDER. Ctreated Time: ";
strLineText += strTemp;
strLineText += ")";
}
strLineText += "\n";
m_fFile.WriteString(strLineText);
// if it's a directory, recursively search it
if (finder.IsDirectory())
{
nSpace++;
strTemp = finder.GetFilePath();
RecurFind(strTemp);
}
m_nBrowsedFiles++;
strTemp.Format("%d", m_nBrowsedFiles);
GetDlgItem(IDC_STC_STATISTIC)->SetWindowText(strTemp);
}
nSpace--;
finder.Close();
}
/////////////////////////////////////////////////////////////////////
//功 能:启动文件扫描线程
//参 数:strName:待查找的文件路径名
//返回值:启动成功,返回true,否则false
/////////
BOOL CFilesStructureDlg::BeginBrowseThread()
{
if( !IsBrowseHandleRunning() )
{
DWORD threadId;
m_HBrowseHandle = ::CreateThread(NULL, 0, BrowseThredProc, this, 0, &threadId);
return m_HBrowseHandle != NULL;
}
return false;
}
/////////////////////////////////////////////////////////////////////
//功 能:判断文件扫描线程是否正在运行
//参 数:
//返回值:是,返回true
/////////
BOOL CFilesStructureDlg::IsBrowseHandleRunning()
{
return m_HBrowseHandle != NULL;
}
/////////////////////////////////////////////////////////////////////
//功 能:结束线程
//参 数:dwWaitTime:结束线程等待时间,默认值100ms
//返回值:成功结束,返回true
/////////
BOOL CFilesStructureDlg::EndThreadBrowse(DWORD dwWaitTime)
{
if( IsBrowseHandleRunning() )
{
if( ::WaitForSingleObject(m_HBrowseHandle, dwWaitTime) != WAIT_OBJECT_0 )
if( !::TerminateThread(m_HBrowseHandle, 0) )
return false;
::CloseHandle(m_HBrowseHandle);
m_HBrowseHandle = NULL;
return true;
}
return false;
}
/////////////////////////////////////////////////////////////////////
//功 能:文件扫描线程处理主函数
//参 数:
//返回值:
/////////
UINT CFilesStructureDlg::ThreadBrowse()
{
RecurFind((LPCTSTR)m_strDirctName);
GetDlgItem(IDC_STC_STATUS) ->SetWindowText("Completed!");
nSpace = 0;
m_fFile.Close();
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -