📄 filefinddlg.cpp
字号:
// FileFindDlg.cpp : implementation file
//
#include "stdafx.h"
#include "FileFind.h"
#include "FileFindDlg.h"
#include "DirDialog.h"
#include "winreg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
THREADPARAM m_param;
CMutex m_mutexThreadCount,m_mutexThreadObj,m_mutexThreadParam,m_mutexPath;
UINT uThreadCount=0;
CStringArray m_strPathArray;
CEvent m_event(FALSE,FALSE,NULL,NULL);
UINT GetFilePathThreadProc(LPVOID pParam)
{
if(pParam==NULL)
AfxEndThread(NULL);
THREADPARAM * m_pParam=(THREADPARAM *)pParam;
CString strPath=m_pParam->strPath;
CString strFileName=m_pParam->strFileName;
CListCtrl *m_pListInfo=(CListCtrl *)m_pParam->m_pListInfo;
m_mutexThreadCount.Lock();
uThreadCount++;
m_mutexThreadCount.Unlock();
HANDLE hFile;
WIN32_FIND_DATA *pInfo=new WIN32_FIND_DATA;
hFile = ::FindFirstFile(strPath+"\\*.*",pInfo);
if(hFile==INVALID_HANDLE_VALUE)
{
delete pInfo;
m_mutexThreadCount.Lock();
uThreadCount--;
if(uThreadCount==0)
m_event.SetEvent();
m_mutexThreadCount.Unlock();
return 0;
}
do{
if(pInfo->cFileName[0]=='.')
continue;
char cFileName[MAX_PATH];
strcpy(cFileName,pInfo->cFileName);
CString strFile=cFileName;
TRACE0(strFile+"\n");
if(pInfo->dwFileAttributes==FILE_ATTRIBUTE_DIRECTORY)
{
m_mutexThreadParam.Lock();
m_param.strPath=strPath+"\\"+strFile;
m_param.strFileName=strFileName;
AfxBeginThread(GetFilePathThreadProc, &m_param, THREAD_PRIORITY_NORMAL);
m_mutexThreadParam.Unlock();
}
else
{
if(strFile==strFileName){
m_mutexPath.Lock();
m_strPathArray.Add(strPath+"\\"+strFile);
TRACE0(strPath);
m_mutexPath.Unlock();
m_mutexThreadCount.Lock();
uThreadCount--;
if(uThreadCount==0)
m_event.SetEvent();
m_mutexThreadCount.Unlock();
return 0;
}
}
}
while(::FindNextFile( hFile,pInfo));
::FindClose(hFile);
delete pInfo;
m_mutexThreadCount.Lock();
uThreadCount--;
if(uThreadCount==0)
m_event.SetEvent();
m_mutexThreadCount.Unlock();
return 0;
}
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()
/////////////////////////////////////////////////////////////////////////////
// CFileFindDlg dialog
CFileFindDlg::CFileFindDlg(CWnd* pParent /*=NULL*/)
: CDialog(CFileFindDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CFileFindDlg)
m_strFileName = _T("");
m_strPath = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CFileFindDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CFileFindDlg)
DDX_Control(pDX, IDC_LIST_COMBO, m_ListInfo);
DDX_Text(pDX, IDC_EDIT_FILE_NAME, m_strFileName);
DDX_Text(pDX, IDC_EDIT_ROOT_DIR, m_strPath);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CFileFindDlg, CDialog)
//{{AFX_MSG_MAP(CFileFindDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_FIND, OnButtonFind)
ON_BN_CLICKED(IDC_BUTTON_VIEW, OnButtonView)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CFileFindDlg message handlers
BOOL CFileFindDlg::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_ListInfo.InsertColumn(0,"已找到的文件:",LVCFMT_LEFT,200);
ListView_SetExtendedListViewStyle
(m_ListInfo.m_hWnd, LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | LVS_EX_HEADERDRAGDROP|LVS_EX_TRACKSELECT);
return TRUE; // return TRUE unless you set the focus to a control
}
void CFileFindDlg::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 CFileFindDlg::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 CFileFindDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CFileFindDlg::GetFilePath()
{
m_event.ResetEvent();
uThreadCount=0;
m_strPathArray.RemoveAll();
UpdateData(TRUE);
m_param.strPath=m_strPath;
m_param.strFileName=m_strFileName;
m_param.m_pListInfo=&m_ListInfo;
AfxBeginThread(GetFilePathThreadProc, &m_param, THREAD_PRIORITY_NORMAL);
::WaitForSingleObject(m_event.m_hObject,INFINITE);
if(m_strPathArray.GetUpperBound()==-1){
AfxMessageBox("没找到文件",MB_OK|MB_ICONINFORMATION);return;}
for(int i=0;i<m_strPathArray.GetSize();i++)
{
int index=m_ListInfo.InsertItem(0xffff,m_strPathArray.GetAt(i),0);
}
}
void CFileFindDlg::OnButtonFind()
{
LPTSTR str=new char[100];
::GetWindowsDirectory(
str, // address of buffer for Windows directory
strlen(str) // size of directory buffer
);
GetFilePath();
}
void CFileFindDlg::OnButtonView()
{
CDirDialog dlg;
if(dlg.DoBrowse(this)==IDOK)
m_strPath=dlg.m_strPath;
UpdateData(FALSE);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -