在VC中做一个选择文件夹的对话框

[入库:2005年8月18日] [更新:2007年3月24日]

本文简介:选择自 neptunex 的 blog

vc中没有现成的函数来选择一个文件夹,但这是经常会用到的,怎么办?
自动动手,丰衣足食!

使用shbrowseforfolder,代码如下:

#include <shlobj.h>
  
int selfolder(hwnd hparent, cstring &strfolder)
{
    strfolder.empty();
 
    lpmalloc lpmalloc;
 
    if (::shgetmalloc(&lpmalloc) != noerror) return 0;
 
    char szdisplayname[_max_path];
    char szbuffer[_max_path];
    browseinfo browseinfo;
    browseinfo.hwndowner = hparent;
    browseinfo.pidlroot = null; // set root at desktop
    browseinfo.pszdisplayname = szdisplayname;
    browseinfo.lpsztitle = "select a folder";
    browseinfo.ulflags = bif_returnfsancestors|bif_returnonlyfsdirs;
    browseinfo.lpfn = null;
    browseinfo.lparam = 0;
 
    lpitemidlist lpitemidlist;
    if ((lpitemidlist = ::shbrowseforfolder(&browseinfo)) != null)
    {
        // get the path of the selected folder from the    item id list.
        if (::shgetpathfromidlist(lpitemidlist, szbuffer))
        {
            // at this point, szbuffer contains the path the user chose.
            if (szbuffer[0] == '\0') return 0;
 
            // we have a path in szbuffer! return it.
            strfolder = szbuffer;
            return 1;
        }
        else return 1; // strresult is empty
 
        lpmalloc->free(lpitemidlist);
        lpmalloc->release();
    }
   
 return 1;
}

本文关键:在VC中做一个选择文件夹的对话框
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top