想实现目录的选择。
就是选择的目录显示在编辑框中。

解决方案 »

  1.   

    //如下实现
    CString strTmpPath;
    int CALLBACK BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData)
    {
    TCHAR szDir[MAX_PATH];
    switch(uMsg){
    case BFFM_INITIALIZED:
    if (lpData){
    strcpy(szDir, strTmpPath.GetBuffer(strTmpPath.GetLength()));
    SendMessage(hwnd,BFFM_SETSELECTION,TRUE,(LPARAM)szDir);
    }
    break;
    case BFFM_SELCHANGED: {
       if (SHGetPathFromIDList((LPITEMIDLIST) lParam ,szDir)){
      SendMessage(hwnd,BFFM_SETSTATUSTEXT,0,(LPARAM)szDir);
       }
       break;
    }
    default:
       break;
    }
             
    return 0;
    }BOOL GetFolder(CString* strSelectedFolder,
       const char* lpszTitle,
       const HWND hwndOwner, 
       const char* strRootFolder, 
       const char* strStartFolder)
    {
    char pszDisplayName[MAX_PATH];
    LPITEMIDLIST lpID;
    BROWSEINFOA bi;

    bi.hwndOwner = hwndOwner;
    if (strRootFolder == NULL){
    bi.pidlRoot = NULL;
    }else{
       LPITEMIDLIST  pIdl = NULL;
       IShellFolder* pDesktopFolder;
       char          szPath[MAX_PATH];
       OLECHAR       olePath[MAX_PATH];
       ULONG         chEaten;
       ULONG         dwAttributes;    strcpy(szPath, (LPCTSTR)strRootFolder);
       if (SUCCEEDED(SHGetDesktopFolder(&pDesktopFolder)))
       {
       MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, szPath, -1, olePath, MAX_PATH);
       pDesktopFolder->ParseDisplayName(NULL, NULL, olePath, &chEaten, &pIdl, &dwAttributes);
       pDesktopFolder->Release();
       }
       bi.pidlRoot = pIdl;
    }
    bi.pszDisplayName = pszDisplayName;
    bi.lpszTitle = lpszTitle;
    bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_STATUSTEXT;
    bi.lpfn = BrowseCallbackProc;
    if (strStartFolder == NULL){
    bi.lParam = FALSE;
    }else{
    strTmpPath.Format("%s", strStartFolder);
    bi.lParam = TRUE;
    }
    bi.iImage = NULL;
    lpID = SHBrowseForFolderA(&bi);
    if (lpID != NULL){
    BOOL b = SHGetPathFromIDList(lpID, pszDisplayName);
    if (b == TRUE){
    strSelectedFolder->Format("%s",pszDisplayName);
    return TRUE;
    }
    }else{
    strSelectedFolder->Empty();
    }
    return FALSE;
    }//CGetFolderDlg为对话框程序主窗口类
    void CGetFolderDlg::OnGetfolder() 
    {
    CString strFolderPath;
    if (GetFolder(&strFolderPath, "Sample of  getting folder.", this->m_hWnd, NULL, NULL))
    {
    if (!strFolderPath.IsEmpty())
    {
    //strFolderPath便是
    }
    }
    }
      

  2.   

    CFileDialog中有一个OPENFILENAME类型的成员m_ofn
    设置m_ofn各个域的值可以对CFileDialog的行为作出各种各样的修改
    比如:设置缺省目录:CFileDialog openfile(true,"dat","d:\pcm\pcm1.dat");
    openfile.m_ofn.lpstrInitialDir = "d;\pcm\";
    openfile.DoModal();