본문 바로가기

Programming/MFC

BROWSEINFO 의 초기경로 설정

출처 창과방패의 싸움프로그래밍 | 호수니
원문 http://blog.naver.com/sasimy18/120035071935


static int CALLBACK BrowseCallbakProc(HWND hWnd, UINT uMsg, LPARAM lParam, LPARAM pData)

{

    switch(uMsg)

    {

    case BFFM_VALIDATEFAILED:

        CString strText;

        strText.Format("%s", reinterpret_cast<LPTSTR>(lParam));

        AfxMessageBox(strText);

        break;


    }

    return 0;

};



void CTest3Dlg::OnButton1()

{

    ITEMIDLIST *pildBrowse;

    char pszPathname[MAX_PATH];


    LPITEMIDLIST pidl = NULL;

    BROWSEINFO bInfo;

    ZeroMemory( &bInfo, sizeof(BROWSEINFO) );


    SHPathToPidl( "C:\\DownLoad", &pidl );        //이곳에 최상위 루트를 지정하시구요

    bInfo.hwndOwner = GetSafeHwnd();

    bInfo.pidlRoot = pidl;

    bInfo.pszDisplayName = pszPathname;

    bInfo.lpszTitle = "디렉토리를 선택하세요";

    bInfo.lpfn = BrowseCallbakProc;                    //에디트 박스에서 텍스트를 받아오기 위한 Callback 입니다.

    bInfo.ulFlags = BIF_BROWSEINCLUDEFILES | BIF_EDITBOX | BIF_VALIDATE;


    pildBrowse = ::SHBrowseForFolder(&bInfo);


    if( pildBrowse != NULL )

    {

        SHGetPathFromIDList(pildBrowse, pszPathname);

        CString m_strFolder = (LPCTSTR)pszPathname;

        MessageBox( m_strFolder );

    }

}



HRESULT CTest3Dlg::SHPathToPidl( LPCTSTR szPath, LPITEMIDLIST* ppidl )

{

    LPSHELLFOLDER pShellFolder = NULL;

    OLECHAR wszPath[MAX_PATH] = {0};

    ULONG nCharsParsed = 0;


    HRESULT hr = SHGetDesktopFolder( &pShellFolder );

    if( FAILED(hr) )

        return FALSE;


    MultiByteToWideChar( CP_ACP, MB_PRECOMPOSED, szPath, -1, wszPath, MAX_PATH );


    hr = pShellFolder->ParseDisplayName( NULL, NULL, wszPath, &nCharsParsed, ppidl, NULL );


    pShellFolder->Release();

    return hr;

}