MFC 기반 프로그램에서 파일 열기 대화창이 아닌 디렉토리 선택 대화창을 구현한 클래스 예제
사용법:
CFolderDialog dlg(AfxGetMainWnd());
if(dlg.DoModal() == IDOK)
{
CString dirPath = dlg.m_sPath;
AfxMessageBox(dirPath);
}
CFolderDialog.h
#pragma once
#include "afxdlgs.h"
class CFolderDialog : public CCommonDialog
{
public:
CFolderDialog(CWnd* pParentWnd);
CString m_sPath;
BROWSEINFO m_bi;
static INT CALLBACK BrowseCallbackProc( IN HWND hWnd, IN UINT uMsg, IN LPARAM lParam, IN LPARAM lpData );
int DoModal();
};
CFolderDialog.cpp
#include "stdafx.h"
#include "CFolderDialog.h"
CFolderDialog::CFolderDialog(CWnd* pParentWnd) : CCommonDialog(pParentWnd)
{
LPCTSTR pszTitle = _T( "Select the root folder for the browse dialog:" );
UINT uFlags = BIF_RETURNONLYFSDIRS | BIF_USENEWUI;
m_bi.hwndOwner = pParentWnd->GetSafeHwnd();
m_bi.pidlRoot = NULL;
m_bi.lpszTitle = pszTitle;
m_bi.ulFlags = uFlags;
m_bi.lpfn = (BFFCALLBACK)&BrowseCallbackProc;
m_bi.lParam = (LPARAM)this;
m_bi.pszDisplayName = new TCHAR[ MAX_PATH ];
ASSERT( m_bi.pszDisplayName != NULL );
}
int CFolderDialog::DoModal()
{
INT_PTR nRet = -1;
m_bi.hwndOwner = PreModal();
LPITEMIDLIST pItemIDList = ::SHBrowseForFolder( &m_bi );
if( pItemIDList )
{
TCHAR szFolPath[ MAX_PATH ];
if( ::SHGetPathFromIDList( pItemIDList, szFolPath ) )
nRet = IDOK;
m_sPath = CString(szFolPath);
}
else
nRet = IDCANCEL;
PostModal();
return ( nRet );
}
INT CALLBACK CFolderDialog::BrowseCallbackProc( IN HWND hWnd,
IN UINT uMsg,
IN LPARAM lParam,
IN LPARAM lpData )
{
INT nRet = 1;
return nRet;
}