CpathDialog class learning method

xiaoxiao2021-03-05  20

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 // # if defined (AFX_PATHDIALOG_H__0F70BC86_11DB_11D4_B012_0000E8DD8DAA__INCLUDED _) # define AFX_PATHDIALOG_H__0F70BC86_11DB_11D4_B012_0000E8DD8DAA__INCLUDED_ # if _MSC_VER> 1000 # pragma once # endif // _MSC_VER> 1000 // PathDialog.h: header file // # include "shlobj.h" class CPathDialog; // CPathDialogSub - intercepts messages from child controlsclass CPathDialogSub: public CWnd {friend CPathDialog ; public: CPathDialog * m_pPathDialog; protected: afx_msg void OnOK (); // OK button clicked afx_msg void OnChangeEditPath (); DECLARE_MESSAGE_MAP () private:}; /// CPathDialog dialogclass CPathDialog {friend CPathDialogSub; // Constructionpublic: CPathDialog (LPCTSTR lpszCaption = NULL, LPCTSTR lpszTitle = NULL, LPCTSTR lpszInitialPath = NULL, CWnd * pParent = NULL); CString GetPathName (); virtual int DoModal (); static Touch (LPCTSTR lpPath, BOOL bValidate = TRUE); static int MakeSurePathExists (LPCTSTR lpPath ); static BOOL IsFileNameValid (LPCTSTR lpFileName); static int ConcatPath (LPTSTR lpRoot, LPCTSTR lpMorePath); private: static int CALLBACK BrowseCallbackProc (HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM pData); LPCTSTR m_lpszCaption; LPCTSTR m_lpszInitialPath; TCHAR m_szPathName [MAX_PATH] ; BROWSEINFO m_bi; HWND m_hWnd; CWnd * m_pParentWnd; BOOL m_bParentDisabled; BOOL m_bGetSuccess; CPathDialogSub m_PathDialogSub;};.! // {{AFX_INSERT_LOCATION}} // Microsoft Visual C will insert additional declarations immediately before the previous line # endif // defined (AFX_PATHDIALOG_H__0F70BC86_11DB_11D4_B012_0000E8DD8DAA__INCLUDED _) // CPP file: ///pathdialog.h file //// Written by nguyen Tan Hung <

Tanhung@yahoo.com> // # include "stdafx.h" #include "pathdialog.h" #include #ifdef _debug # define new debug_new # undef this_filestatic char this_file [] = __file __; # Endif # define IDC_FOLDERTREE 0x3741 # define IDC_TITLE 0x3742 # define IDC_STATUSTEXT 0x3743 # define IDC_NEW_EDIT_PATH 0x3744 // Class CDlgWndBEGIN_MESSAGE_MAP (CPathDialogSub, CWnd) ON_BN_CLICKED (IDOK, OnOK) ON_EN_CHANGE (IDC_NEW_EDIT_PATH, OnChangeEditPath) END_MESSAGE_MAP () void CPathDialogSub :: OnOK () {:: GetWindowText ( :: GetDlgItem (m_hWnd, IDC_NEW_EDIT_PATH), m_pPathDialog-> m_szPathName, MAX_PATH); if (CPathDialog :: MakeSurePathExists (m_pPathDialog-> m_szPathName) == 0) {m_pPathDialog-> m_bGetSuccess = TRUE; :: EndDialog (m_pPathDialog-> m_hWnd, IDOK);} else {:: SetFocus (:: GetDlgItem (m_hWnd, IDC_NEW_EDIT_PATH));}} void CPathDialogSub :: OnChangeEditPath () {:: GetWindowText (:: GetDlgItem (m_hWnd, IDC_NEW_EDIT_PATH), m_pPathDialog-> m_szPathName, MAX_PATH) Bool BenableokButton = (_tcslen (m_ppathdialog-> m_szpathname)> 0); SendMessage (B FFM_ENABLEOK, 0, bEnableOKButton);} /// CPathDialog dialogCPathDialog :: CPathDialog (LPCTSTR lpszCaption, LPCTSTR lpszTitle, LPCTSTR lpszInitialPath, CWnd * pParent) {m_hWnd = NULL; m_PathDialogSub.m_pPathDialog = this; m_bParentDisabled = FALSE; // Get the true parent of the dialog m_pParentWnd = CWnd :: GetSafeOwner (pParent); m_lpszCaption = lpszCaption; m_lpszInitialPath = lpszInitialPath; memset (& m_bi, 0, sizeof (BROWSEINFO)); m_bi.hwndOwner = (m_pParentWnd == NULL) NULL: m_pParentWnd->? Getsafehwnd (); m_bi.pszdisplayName = 0; m_bi.pidlroot = 0; m_bi.ulflags = bif_returnonlyfsdirs | bif_statustext;

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.

转载请注明原文地址:https://www.9cbs.com/read-38908.html

New Post(0)