Source: http://www.codeguru.com/cpp/w-d/dislog/dialogforselectingFolders/Article.php/c2019/
A very useful class / header files selected directory:! ////PathDialog.h file //// Written by Nguyen Tan Hung
Tanhung@yahoo.com> // # include "stdafx.h" #include "pathdialog.h" #include
m_bi.lpfn = BrowseCallbackProc; m_bi.lpszTitle = lpszTitle;} /// CPathDialog message handlersCString CPathDialog :: GetPathName () {return CString (m_szPathName);} int CALLBACK CPathDialog :: BrowseCallbackProc (HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM PDATA) {cpathdialog * PDLG = (cpathdialog *) PDATA; Switch (umsg) {case bffm_initialized: {Rect rc; hwnd hwit; hfont hfont; pdlg-> m_hwnd = hWnd; if (pdlg-> m_lpszcaption! = null) {: : SetWindowText (hwnd, pDlg-> m_lpszCaption);} VERIFY (pDlg-> m_PathDialogSub.SubclassWindow (hwnd)); :: ShowWindow (:: GetDlgItem (hwnd, IDC_STATUSTEXT), SW_HIDE); :: GetWindowRect (:: (hwnd GetDlgItem , IDC_Foldertree, & RC); rc.bottom = rc.top - 4; rc.top = rc.bottom - 23; :: ScreenToClient (hwnd, (lppoint) & rc); :: ScreenToClient (hwnd, ((lppoint) & rc ) 1); hedit = :: CreateWindowEx (WS_EX_CLIENTEDGE, _T ("Edit"), _T (""), WS_CHILD | WS_TABSTOP | WS_VISIBLE | ES_AUTOHSCROLL, RC.LEFT, RC.TOP, RC.right-rc.Left, Rc.Bottom-rc.top, hwnd, null, null, nul L) ;: setWindowlong (HEDIT, GWL_ID, IDC_NEW_EDIT_PATH); :: ShowWindow (Hedit, SW_SHOW); HFONT = (HFONT) :: SendMessage (hwnd, wm_getfont, 0, 0) ;: SendMessage (Hedit, WM_SETFONT, WPARAM) hFont, MAKELPARAM (TRUE, 0)); LPCTSTR lpszPath = pDlg-> m_lpszInitialPath; TCHAR szTemp [MAX_PATH]; if (lpszPath == NULL) {:: GetCurrentDirectory (MAX_PATH, szTemp); lpszPath = szTemp;} // WParam is TRUE since you are passing a path // It would be FALSE if you were passing a pidl :: SendMessage (hwnd, BFFM_SETSELECTION, TRUE, (LPARAM) lpszPath); break;} case BFFM_SELCHANGED:.. {char szSelection [MAX_PATH ];
IF (! :: shgetpathfromidlist (lpitemidlist) || szselection [1]! = ':') {szselection [0] = '/ 0'; :: SendMessage (hwnd, bffm_enableok, 0, false); } else {:: SendMessage (hwnd, BFFM_ENABLEOK, 0, TRUE);} :: SendMessage (hwnd, BFFM_SETSTATUSTEXT, 0, (LPARAM) szSelection); :: SetWindowText (:: GetDlgItem (hwnd, IDC_NEW_EDIT_PATH), szSelection); break ;} default: break;} return 0;} int CPathDialog :: DoModal () {/ TCHAR szPathTemp [MAX_PATH]; m_bi.lpfn = BrowseCallbackProc; // address of callback function m_bi.lParam = (LPARAM) this; // pass address of object callback function m_bi.pszDisplayName = szPathTemp to; LPITEMIDLIST pidl; LPMALLOC pMalloc; int iResult = -1;! if (SUCCEEDED (SHGetMalloc (& pMalloc))) {// if ((m_pParentWnd = NULL) && m_pParentWnd-> IsWindowEnabled ()) // {// m_pparentWnd-> EnableWindow (false); // m_bparentdisabled = true; //} m_bgetsuccess = false; pidl = shbrowseForfolder (& m_bi); if (pid1! = Null) {// not need do this Because on Ok function did // bsucceeded = shgetpathfromidlist (PIDL, m_szpathname); // in C : pmalloc-> free (PIDL); // in c: // pmalloc-> lpvtbl-> free (Pmalloc, PIDL); // Pmalloc -> lpvtbl-> release (pmalloc);} if (m_bgetsuccess) {IResult = idok;} pmalloc-> release ();} if (m_bparentdisabled && (m_pparentwnd! = null) {m_pparentWnd-> enableWindow;} m_bParentDisabled = FALSE; return iResult;} BOOL CPathDialog :: IsFileNameValid (LPCTSTR lpFileName) {if (lpFileName == NULL) {return FALSE;} int nLen = _tcslen (lpFileName); if (nLen <= 0) {return FALSE;
} // check first char switch (lpfilename [0]) {case_t ('): case _t ('): case_t ('/ t'): Return False;} // Check Last Char Switch (lpfilename [NLEN-1]) {case _t ('): case _t (' '): Case_T (' / t '): Return False;} // Check All Int i = 0; While (LPFileName [i] ! = 0) {Switch (lpfilename [i]) {case _t ('//'): case _t (':'): case _t ('*'): case_t (' ? '): Case _t (' <'): case _t ('> '): Case_T (' | '): Return False;} i ;} return true;} const tchar C_FolderDoesNotexist [] = _T ("THE Folder: / N / N" "% S / N / N" "Do you want the folder to be created?"); const tchar c_szerrinvalidpath [] = _t ("the "/ n / n" "% S / N / N" "IS INVALID." "); const tchar c_szerrcreatepath [] = _t (" The folder: "/ n / n" "% s" "/ N / NCAN NOT BE CREATED." // Return-1: user break; // return 0: No error // Return 1: lppath is invalid // Return 2: Can not create lppathint cpathdialog :: MakeSurePathexists (LPCTSTR LPPATH) {CString strmsg; int rt; Try { // validate path iret = touch (lppath, true); if (iRet! = 0) {throw IRet;} f (_TACCESS (LPPATH, 0) == 0) {return (int) 0;} strmsg.format (c_folderdoesnotexist) , LPPATH); if (AFXMSGEBOX (STRMSG, MB_YESNO | MB_ICONQUESTION)! = iDYES) {return (int) -1;} // create path iret = touch (lppath, false); if (IRet! = 0) {throw IRet } Return 0;} catch (int Nerrcode) {copy 1: strmsg.format (c_szerrinvalidpath, lppath); Break;
case 2: default: strMsg.Format (c_szErrCreatePath, lpPath); break;} AfxMessageBox (strMsg, MB_OK | MB_ICONEXCLAMATION);} return iRet;} // return 0: no error // return 1: lpPath is invalid // return 2 : lppath can not be created (bvalidate == false) int cpathdialog :: touch (lpctstr lppath, bool bvalidate) {if (lppath == null) {return 1;} tchar szpath [max_path]; _tcscpy (szpath, lppath); INT Nlen = _TCSLEN (SZPATH); // path must be "x: / ..." IF ((Nlen <3) || ((Szpath [0] <_ t ('a') || _t ('Z' // ")); _TCSCAT (LPROOT, LPMOREPATH); RETURN 0;} /// After the PathDialog.h and PathDialog.cpp are added to the project, the calling code is as follows: #include" pathdialog.h "Void ctestpathdialogdlg :: OnBrowserbutton ) { CString StrinitialPath; CSTRING STRYOURCAPTION (_T ("Your Caption Here ..."); CSTRING STRYOTITETLE (_T ("Your Tilte Here ..."); m_ctrlpathname.getWindowText (StrinitialPath); CpathDialog DLG (Stryourcaption, STRYOTITLE, STRINIALPATH); IF (DLG.Domodal () == iDOK) { m_cTrlpathname.SetWindowText (DLG.GetPathName ()); } } Void ctestpathdialogdlg :: onokbutton () { Cstring strpath; m_ctrlpathname.getWindowText (StrPath); IF (cpathdialog :: makeesurepathexists (strpath) == 0) { CDIALOG :: Onok (); } } Supplementary Description: Selecting the Path dialog in this example without the "New Folder" function, you can refer to: http://www.vccode.com/file_show.php? ID = 2661.