// MyFindDlg.cpp : implementation file // #include "stdafx.h" #include "myExplorer.h" #include "MyFindDlg.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // MyFindDlg dialog MyFindDlg::MyFindDlg(CWnd* pParent /*=NULL*/) : CDialog(MyFindDlg::IDD, pParent) { //{{AFX_DATA_INIT(MyFindDlg) m_sBaseFolder = _T(""); m_sFileMask = _T("*.*"); //}}AFX_DATA_INIT m_bSearching = false; } void MyFindDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(MyFindDlg) DDX_Control(pDX, IDC_LIST1, m_list); DDX_Text(pDX, IDC_PATH, m_sBaseFolder); DDX_Text(pDX, IDC_FILE, m_sFileMask); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(MyFindDlg, CDialog) //{{AFX_MSG_MAP(MyFindDlg) ON_BN_CLICKED(IDC_SEARCH, OnSearch) ON_BN_CLICKED(IDC_BUTTON1, OnConfigDir) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // MyFindDlg message handlers void MyFindDlg::OnSearch() { // TODO: Add your control notification handler code here if (m_bSearching)//如果上次查询正在进行,取消上次查询 { m_finder.StopSearch(); return; } else { m_list.DeleteAllItems(); } myFind::CFindOpts opts; UpdateData(); // 设置 CFindOpts对象 opts.sBaseFolder = m_sBaseFolder; opts.sFileMask.Format("*%s*", m_sFileMask); opts.bSubfolders = 1;//是否查找子目录 opts.FindNormalFiles();//常规文件设置 m_bSearching = true; GetDlgItem(IDC_SEARCH)->SetWindowText("停止搜索"); m_finder.RemoveAll(); m_finder.Find(opts); //开始递归查找 GetDlgItem(IDC_SEARCH)->SetWindowText("开始搜索"); m_bSearching = false; SetStatus(m_finder.GetFileCount(),NULL); } BOOL MyFindDlg::OnInitDialog() { CDialog::OnInitDialog(); // TODO: Add extra initialization here m_finder.SetCallback(FileFinderProc, this); m_list.InsertColumn(0,"文件名",0,140); m_list.InsertColumn(1,"所在文件夹",0,500); return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE } void MyFindDlg::FileFinderProc(myFind *pFinder, DWORD dwCode, void *pCustomParam) { CString sText, sNewFile; MSG msg; MyFindDlg *pDlg = (MyFindDlg *)pCustomParam; switch (dwCode) { case FF_FOUND://找到的是文件 sNewFile = pFinder->GetFilePath(pFinder->GetFileCount() - 1); //获取文件名 pDlg->AddFileToList(sNewFile); //添加到列表框 break; case FF_FOLDER://找到的是文件夹 pDlg->SetStatus(pFinder->GetFileCount(), pFinder->GetSearchingFolder()); } // 处理消息,防止不响应 while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); } } void MyFindDlg::SetStatus(int nCount, LPCTSTR szFolder) { CString sStatus; if (szFolder != NULL) sStatus.Format("(%d) - %s", nCount, szFolder); else sStatus.Format("%d 个文件找到了", nCount); GetDlgItem(IDC_STSTATUS)->SetWindowText(sStatus); } void MyFindDlg::AddFileToList(LPCTSTR szFilename) { int nIndex; myPath path(szFilename); // File name nIndex = m_list.InsertItem(m_list.GetItemCount(), path.GetFileName(), 0); // File location m_list.SetItemText(nIndex, 1, path.GetLocation()); } CString MyFindDlg::GetListFilename(int nIndex) { return m_list.GetItemText(nIndex, 1) + m_list.GetItemText(nIndex, 0); } void MyFindDlg::OnConfigDir() { // TODO: Add your control notification handler code here CString sFolder; LPMALLOC pMalloc; // Gets the Shell's default allocator if (::SHGetMalloc(&pMalloc) == NOERROR) { BROWSEINFO bi; char pszBuffer[MAX_PATH]; LPITEMIDLIST pidl; bi.hwndOwner = GetSafeHwnd(); bi.pidlRoot = NULL; bi.pszDisplayName = pszBuffer; bi.lpszTitle = _T("Select a directory..."); bi.ulFlags = BIF_RETURNFSANCESTORS | BIF_RETURNONLYFSDIRS; bi.lpfn = NULL; bi.lParam = 0; // This next call issues the dialog box. if ((pidl = ::SHBrowseForFolder(&bi)) != NULL) { if (::SHGetPathFromIDList(pidl, pszBuffer)) { // At this point pszBuffer contains the selected path sFolder = pszBuffer; } // Free the PIDL allocated by SHBrowseForFolder. pMalloc->Free(pidl); } // Release the shell's allocator. pMalloc->Release(); } GetDlgItem(IDC_PATH)->SetWindowText(sFolder); }