DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> AJAX入門 >> AJAX詳解 >> 用VC調用Shell中的目錄浏覽對話框
用VC調用Shell中的目錄浏覽對話框
編輯:AJAX詳解     
First Example:


有時我們需要獲得某個文件夾的路徑,而不是文件的路徑,這時需要用API函數SHBrowseForFolder來實現。

函數原型為:

LPITEMIDLIST SHBrowseForFolder(LPBROWSEINFO lpbi);

其中LPBROWSEINFO為BROWSEINFO結構的指針。

BROWSEINFO結構如下:

typedef struct _browseinfo {

HWND hwndOwner;
LPCITEMIDLIST pidlRoot;
LPSTR pszDisplayName;
LPCSTR lpszTitle;
UINT ulFlags;
BFFCALLBACK lpfn;
LPARAM lParam;
int iImage;

} BROWSEINFO;

調用例子如下:

void CMakemifDlg::OnRecPcpath()
{
CString sFolderPath;
BROWSEINFO bi;
char Buffer[MAX_PATH];
//初始化入口參數bi開始
bi.hwndOwner = NULL;
bi.pidlRoot = NULL;
bi.pszDisplayName = Buffer;//此參數如為NULL則不能顯示對話框
bi.lpszTitle = "修改接收路徑";
bi.ulFlags = 0;
bi.lpfn = NULL;
bi.iImage=IDI_ICON2;
//初始化入口參數bi結束
LPITEMIDLIST pIDList = SHBrowseForFolder(&bi);//調用顯示選擇對話框
if(pIDList)
{
SHGetPathFromIDList(pIDList, Buffer);
//取得文件夾路徑到Buffer裡
sFolderPath = Buffer;//將路徑保存在一個CString對象裡
}
LPMALLOC lpMalloc;
if(FAILED(SHGetMalloc(&lpMalloc))) return;
//釋放內存
lpMalloc->Free(pIDList);
lpMalloc->Release();
}



===================================================================================

Another Example:

 

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();
    }

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved