xuejianhui

导航

VC 浏览文件和文件夹

// 浏览--文件:

// 浏览--文件
void DecSendData::OnBnClickedBtnScan()
{
    CString szPath = "";
    CFileDialog FDlg(TRUE , ".doc" , NULL , OFN_HIDEREADONLY , "word文件(*.doc)|*.doc|所有文件(*.*) |*.*||" );

    // 显示对话框并使用户可以进行选择
    if(IDOK == FDlg.DoModal())
    {
        // 返回选定文件的完整路径
        szPath.Replace("\\","\\\\");
        szPath = FDlg.GetPathName();

        // 界面显示获取的完整路径
        szDecSendDataPath = szPath;

        UpdateData(false);
    }
}

 

-----------------------------------------------------------------------------------------------------------------------------------

 

// 浏览--文件夹:

// 浏览--文件夹
void DecSendData::OnBnClickedBtnScan()
{
    CString szPath = GetDirectoryPath();
    if (szPath != "")
    {
       szDecSendDataPath = szPath;
       UpdateData(false);
    }
}

// 获取选择的路径
CString DecSendData::GetDirectoryPath()
{
    LPITEMIDLIST pidlRoot=NULL;
    SHGetSpecialFolderLocation(m_hWnd,CSIDL_DRIVES,&pidlRoot);
    BROWSEINFO bi;   //the input parameters needed
    CString strDisplayName;  
    bi.hwndOwner=GetSafeHwnd();   //get handle value of parent window 
    bi.pidlRoot=pidlRoot;   
    bi.pszDisplayName=strDisplayName.GetBuffer(MAX_PATH+1);   //get buffer pointer
    char szLan[32] = {0};
    sprintf(szLan, "%s", "文件夹");
    bi.lpszTitle=szLan;   //set title
    bi.ulFlags=BIF_RETURNONLYFSDIRS;   //set flag
    bi.lpfn=NULL;
    bi.lParam=0;
    bi.iImage=0;   //The above parameters are not related to the settings, better set up,
    //LPITEMIDLIST lpIDList= SHBrowseForFolder(&bi);    //open dialog
    LPITEMIDLIST lpIDList= SHBrowseForFolder(&bi);    //open dialog
    strDisplayName.ReleaseBuffer();   //correspond to GetBuffer()
    char pPath[MAX_PATH];
    CString Str;
    if (lpIDList)
    {
        SHGetPathFromIDList (lpIDList, pPath);
        Str=pPath;
    }
    return Str;
}

有关《文件加浏览(SHBrowseForFolder)》的资料见下面地址:

http://www.cnblogs.com/clingingboy/archive/2011/04/16/2018284.html

 

有关《VC打开浏览文件对话框、浏览文件夹对话框》:

http://www.cnblogs.com/hyangshi/articles/1615324.html

结束。。。。

posted on 2012-10-27 11:45  xuejianhui  阅读(542)  评论(0编辑  收藏  举报