유용한 정보

[VC++]폴더 선택 다이얼로그

DevReff 2024. 12. 26. 20:03




728x90
반응형

/*
** 폴더 선택 Dialog를 실행한다.
** 선택된 폴더의 값은 cstrBaseFolderName에 설정된다.
** lpMsg : 설명줄에 표시될 문자열
** lpCheck : 만약 체크 박스가 있다면 체크 박스 상태를 반환한다.
** 반환값 : 성공(TRUE), 실패(FALSE)
*/
static WNDPROC gCBProc;
static BOOL gbDMFolderDlgCheckFlag = TRUE;
static BOOL gbDMShowFolderDlgCheckFlag = FALSE;

// Subclass procedure
LRESULT APIENTRY DMCheckBoxSubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
if(uMsg==WM_LBUTTONUP)
{
if((SendMessage(hwnd,BM_GETCHECK,0,0))==1)
{
gbDMFolderDlgCheckFlag = FALSE;
}
else
{
gbDMFolderDlgCheckFlag = TRUE;
}
}

return CallWindowProc(gCBProc, hwnd, uMsg, wParam, lParam);
}
//#endif

static int CALLBACK ShellFolderDlgCallback(HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData)
{
if (uMsg == BFFM_INITIALIZED) // 초기화 될 때
{
// 시작 폴더 선택 메시지
::SendMessage(hwnd, BFFM_SETSELECTION, TRUE, lpData);
#ifdef STR_MAIN_PROGRAM_TITLE
SetWindowText(hwnd, STR_MAIN_PROGRAM_TITLE);
#endif
// 폴더 옵션 체크 박스 만들기
#ifdef _MINIRAM_
if(gbDMShowFolderDlgCheckFlag)
{
//edit=CreateWindowEx(0,"EDIT","Yogesh M Joshi.",WS_CHILD|WS_VISIBLE|WS_BORDER|
// ES_AUTOHSCROLL,0,100,100,50,hwnd,0,ghInstance,NULL);
HWND hWndCheckBox = CreateWindowEx(0,
_T("BUTTON"),
_T("게시물 제목으로 폴더를 만듭니다."),
WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN | BS_AUTOCHECKBOX,
20, 28, 300, 20,
hwnd,
0,
AfxGetInstanceHandle(),
NULL);
//Sets the fonts of edit and static controls
SetFont(hWndCheckBox, _T("MS Sans Serif"), 8);
// Subclass the checkbox control.
gCBProc = (WNDPROC) SetWindowLong(hWndCheckBox, GWL_WNDPROC, (LONG) DMCheckBoxSubclassProc);
//Sets the checkbox to checked position
SendMessage(hWndCheckBox, BM_SETCHECK, (WPARAM)gbDMFolderDlgCheckFlag, 0);
}
#endif

}

return 0;
}

BOOL DoOpenFolderDlg(CString& cstrBaseFolderName, LPCTSTR lpMsg, HWND hParentWnd/*=NULL*/, LPBOOL lpCheck/*=NULL*/)
{
BROWSEINFO bi;
ZeroMemory(&bi, sizeof(bi));

//TCHAR szParam[4096] = {0,};
TCHAR szDisplayName[4096] = {0x00, };
memset(szDisplayName, 0x00, sizeof(szDisplayName));

CoInitialize(NULL);

//::MessageBox(NULL, _T("1"), _T("DoOpenFolderDlg"), MB_ICONINFORMATION);
bi.hwndOwner = hParentWnd ? hParentWnd : AfxGetApp()->GetMainWnd()->m_hWnd;
bi.pidlRoot = NULL;
bi.pszDisplayName = szDisplayName;
bi.lpszTitle = lpMsg;

bi.ulFlags = BIF_VALIDATE | BIF_RETURNONLYFSDIRS | BIF_EDITBOX| BIF_NEWDIALOGSTYLE |BIF_SHAREABLE | BIF_USENEWUI;
//bi.ulFlags = BIF_RETURNONLYFSDIRS;
bi.lpfn = ShellFolderDlgCallback;
bi.lParam = (LPARAM)(LPCTSTR)cstrBaseFolderName;

SetForegroundWindow(bi.hwndOwner);

gbDMFolderDlgCheckFlag = FALSE;

//::MessageBox(NULL, _T("1_2"), _T("DoOpenFolderDlg"), MB_ICONINFORMATION);
LPITEMIDLIST pidl = SHBrowseForFolder(&bi);
//cstrBaseFolderName = szParam;
//::MessageBox(NULL, _T("2"), _T("DoOpenFolderDlg"), MB_ICONINFORMATION);

TCHAR szPath[4096] = {0, };
memset(szPath, 0x00, sizeof(szPath));

if (NULL != pidl)
{
SHGetPathFromIDList(pidl, szPath);
if (szPath[0] == '\0')
{
CString cstrTitle, cstrTmp;
AfxGetMainWnd()->GetWindowText(cstrTitle);
// SHGetPathFromIDList failed, or // SHBrowseForFolder failed.
cstrTmp.LoadString(IDS_STR_CANT_GET_FOLDER);
MessageBox(bi.hwndOwner, cstrTmp, cstrTitle, MB_ICONINFORMATION);
return FALSE;
}
cstrBaseFolderName = szPath;
if(lpCheck) (*lpCheck) = gbDMFolderDlgCheckFlag;
}
else return FALSE;

//::MessageBox(NULL, _T("3"), _T("DoOpenFolderDlg"), MB_ICONINFORMATION);
return TRUE;
}

 

출처: https://use1348.tistory.com/30 [유용한 정보:티스토리]