MySearchDlg.h
MySearchDlg.h: Header file//#pragma once//Cmysearchdlg dialog Class Cmysearchdlg:public cdialogex{//construct Public:cmysearchdlg (CWn d* pparent = NULL);//Standard constructor//dialog data enum {IDD = idd_mysearch_dialog};p rotected:virtual void DoDataExchange (CDataExchange * PDX);//DDX/DDV support void OnOK ();//implement Protected:hicon m_hicon;//generated message map function virtual BOOL OnInitDialog (); afx_msg void Onsysco Mmand (UINT NID, LPARAM LPARAM), afx_msg void OnPaint (), afx_msg hcursor Onquerydragicon ();D Eclare_message_map () Public: afx_msg void OnSize (UINT nType, int cx, int cy), afx_msg void Ondropfiles (Hdrop hdropinfo); afx_msg void Ondblclklistbox (); a fx_msg void OnEnChangeEdit1 (); afx_msg void OnBnClickedOK (); afx_msg void Onbnclickedcancel (); afx_msg void OnLbnSelchangeList1 (); afx_msg void OnBnClickedOk2 ();};
MySearchDlg.cpp
MySearchDlg.cpp: Implement file//#include "stdafx.h" #include "MySearch.h" #include "MySearchDlg.h" #include "afxdialogex.h"// #include <sys/stat.h>//#include <direct.h>//#include <iostream> #include <fstream>//# Include <shlwapi.h>//#pragma comment (lib, "Shlwapi.lib")//If this line is not present, link error will appear using namespace std; #ifdef _debug#define new Debug_new#endifint findcount = 0; CListBox *list = Null;bool iscontent = True;char *wtoc (wchar_t *wtext); wchar_t *ctow (char *stext); char *cstoc (CString CS); BOOL Isdir (const char* path); unsigned myfind (const string& filename, const string& tofind); void Findindir (const char *path, const string& tofind); void Findindir (const char *path, const string& tofind, int recursive); char *wtoc (wchar_t *wtext) {DWORD dwnum = Widec Hartomultibyte (CP_ACP, NULL, Wtext, -1,null, 0, NULL, FALSE);//The length of the width string to which the fifth parameter is set to NULL includes a trailing character char *pstext = Null;pstext = new Char[dwnum];if (!pstext) {delete []pstext;pstext = NULL;} WideCharToMultiByte (cp_aCP, NULL, Wtext, -1,pstext, dwnum, NULL, FALSE); return pstext;} wchar_t *ctow (char *stext) {DWORD dwnum = MultiByteToWideChar (CP_ACP, 0, Stext,-1, NULL, 0);//The length of the width string to which the fifth parameter is set to NULL includes a trailing character wchar_t *pwtext = Null;pwtext = new Wchar_t[dwnum];if (!pwtext) {delete []pwtext;pwtext = NULL;} unsigned nlen = MultiByteToWideChar (CP_ACP, 0, Stext,-1, Pwtext, dwnum+10); if (nlen >= 0) {Pwtext[nlen] = 0;} return pwtext;} Char *cstoc (CString cs) {wchar_t *t1 =cs. GetBuffer (CS. GetLength ()); Cs. ReleaseBuffer (); return WTOC (t1);} unsigned myfind (const string& filename, const string& tofind) {ifstream fin (filename.c_str (), Ios_base::bina RY); String str (1024*1024*2,0); Fin.read (&str[0],2*1024*1024); Fin.close (); String::size_type Beg = 0; String::size_type pos =-1; String::size_type find_size = Tofind.size (); if (0 = = find_size) {return 0;} unsigned count = 0; while ((Pos=str.find (Tofind,beg))!=string::npos) {Beg=pos+find_size; count++; } return count;} BOOL Isdir (const char* path)//{//cstring str_cap (path);//return pathisdirectory ((const WCHAR *) (Str_cap. AllocSysString ()))//}//traverse the drive function of the folder void Findindir (const char *path, const string& tofind) {CString strdir (path); if (Strdir = = _t ("")) {return; } else {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 ()) Findindir (Cstoc (strpath), tofind); Recursively calls else if (!finder. Isdirectory () &&!finder. Isdots ()) {if (!iscontent) {wchar_t *WC = Ctow ((char*) tofind.c_str ()); int dote = Strpath.reversefind (' \ \ '); CString DST = strpath.right (Strpath.getlength ()-dote -1); if (DST. Find (WC, 0)! =-1) {findcount++; if (Findcount >) return; list->addstring (strpath);}} else {std::string strtemp = Cstoc (strpath); int c =-1; if ((c = Myfind (strtemp, tofind))! = 0) {findcount++; if (Findcount >) return; CString str; Str. Format (L "found%d:%s", C, strpath); List->addstring (str); }}}}}//for the application "about" menu item CAboutDlg dialog box class Caboutdlg:public Cdialogex{public:caboutdlg ();//Dialog Data enum {I DD = Idd_aboutbox};p rotected:virtual void DoDataExchange (cdataexchange* pDX); DDX/DDV Support//Implement PROTECTED:DECLARE_MESSAGE_MAP ()}; Caboutdlg::caboutdlg (): CDialogEx (Caboutdlg::idd) {}void CAboutDlg::D odataexchange (cdataexchange* pDX) {CDialogEx: :D Odataexchange (PDX);} Begin_message_map (CAboutDlg, CDialogEx) End_message_map ()//Cmysearchdlg dialog box Cmysearchdlg::cmysearchdlg (CWnd* Pparent/*=null*/): CDialogEx (Cmysearchdlg::idd, pparent) {m_hicon = AfxGetApp ()->loadicon (IDR_MAINFRAME);} void Cmysearchdlg::D odataexchange (cdataexchange* pDX) {CdialoGEx::D odataexchange (PDX);} Begin_message_map (Cmysearchdlg, CDialogEx) On_wm_syscommand () On_wm_paint () On_wm_querydragicon () ON_EN_CHANGE (IDC_ EDIT1, &cmysearchdlg::onenchangeedit1) on_bn_clicked (IDOK, &cmysearchdlg::onbnclickedok) ON_BN_CLICKED ( IDCANCEL, &cmysearchdlg::onbnclickedcancel) on_wm_dropfiles () On_lbn_selchange (Idc_list1, &CMySearchDlg::O NLbnSelchangeList1) on_bn_clicked (IDOK2, &cmysearchdlg::onbnclickedok2) on_lbn_dblclk (Idc_list1, Ondblclklistbox) on_wm_size () End_message_map ()//CMYSEARCHDLG Message handler bool Cmysearchdlg::oninitdialog () {CDialogEx::O Ninitdialog ();//Add the "About ..." menu item to the System menu. Idm_aboutbox must be within the scope of the system command. ASSERT ((Idm_aboutbox & 0xfff0) = = Idm_aboutbox); ASSERT (Idm_aboutbox < 0xf000); cmenu* Psysmenu = GetSystemMenu (FALSE); if (psysmenu! = NULL) {BOOL bnamevalid; CString straboutmenu;bnamevalid = straboutmenu.loadstring (Ids_aboutbox); ASSERT (Bnamevalid), if (!straboutmenu.isempty ()) {Psysmenu->appendmenu (mf_separator);p Sysmenu->appendmenu ( Mf_string, Idm_aboutbOX, Straboutmenu);}} Sets the icon for this dialog box. When the application main window is not a dialog box, the framework will automatically///Perform this action SetIcon (M_hicon, TRUE);//Set Large icon SetIcon (M_hicon, FALSE);//Set small icon//TODO: Add additional initialization code here CButton *btn = (CButton *) GetDlgItem (IDOK); Btn->setfocus (); list = (CListBox *) GetDlgItem (IDC_LIST1); return TRUE; Returns true}void Cmysearchdlg::onsyscommand (UINT nid, LPARAM LPARAM) {if (NID & 0xfff0) = = Idm_aboutbox) Unless the focus is set to the control { CAboutDlg dlgabout;dlgabout.domodal ();} Else{cdialogex::onsyscommand (NID, LParam);}} void Cmysearchdlg::ondblclklistbox () {int index = List->getcursel (); if (index = =-1) {return;} CString str;list->gettext (index, str); int tmp = str. Find (': '); CString strFilePath = str. Right (str. GetLength ()-1-tmp);//Open the "Open with Select Program ..." dialog box int nret = (int) ShellExecute (This->m_hwnd, _t ("open"), strFilePath, N ULL, NULL, SW_SHOWNORMAL); if (Se_err_noassoc = = nret)//No related program is able to open the file {CString Strcmd;strcmd.format (_t ("Shell32, open As_rundll%s "), strFilePath); ShellExecute (NULL, _t ("open"), _t ("Rundll32.exe"), Strcmd, NULL, SW_SHOWNORMAL);} CString Strparam (_t ("/select,")); Strparam + = Strfilepath;tmp = Strfilepath.reversefind (' \ \ '); CString Strdir = Strfilepath.left (TMP); ShellExecute (0,_t ("open"), _t ("Explorer.exe"), Strparam,0,sw_normal); ShellExecute (This->m_hwnd, _t ("open"), strdir, NULL, NULL, SW_SHOWNORMAL);} If you add a Minimize button to the dialog box, you need the following code//to draw the icon. For MFC applications that use the document/view model,//This is done automatically by the framework. void Cmysearchdlg::onpaint () {if (Isiconic ()) {CPAINTDC DC (this);//device context for drawing SendMessage (WM_ICONERASEBKGND, Reinterpret_cast<wparam> (DC. GETSAFEHDC ()), 0);//Make the icon centered in the workspace 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 icon DC. DrawIcon (x, y, m_hicon);} Else{cdialogex::onpaint ();}} The system calls this function to get the cursor//display when the user drags the minimized window. Hcursor Cmysearchdlg::onquerydragicon () {return static_cast
MFC program (c + +) searches all files and folders