GetFolder : Shell Extension Folder Browser Function (如何获得windows folder 路径) <ZT>

Folder browser dialog - also enumerates network drives...

Overview

This is a very standard technique used to retrieve folder information from Windows using the Shell extension functions SHBrowseForFolderA, SHGetPathFromIDList and SHGetDesktopFolder.

The Code

All you need to do is to call my function GetFolder. However, for the more curious among you that want to know more of the details, there are only two functions used here: one to cause the display of the standard folder browser dialog and a callback function that handles the processing of events while the dialog is being displayed). Here are the basic steps in my code.

GetFolder Steps

  1. Call SHGetDesktopFolder to get the IShellFolder interface for the desktop folder
  2. Call the IShellFolder.ParseDisplayName to get the identifier list
  3. Allocate and fill out a BROWSEINFOA structure with the desired parameters (e.g., pidl from the IShellFolder.ParseDisplayName, callback function that the shell will call with the folder names, etc.). The callback function is called BrowseCallbackProc.
  4. Call SHBrowseForFolderA to display the folder browse dialog (passing it the BROWSEINFOA structure which defines how that dialog should appear)
  5. Upon return from the SHBrowseForFolderA function, I then call the SHGetPathFromIDList function in order to retrieve the name of the user-selected folder.

BrowseCallbackProc Function

In this function I only need to handle the BFFM_INITIALIZED and the BFFM_SELCHANGED messages. Even then, all I'm doing is updating a field on the dialog to reflect the currently selected folder. This is the text that I retrieve in the last step of the GetFolder function.

1 //*********************************************************************************
2  // Function name - GetFolder
3  // Description - Get a folder path
4 // 泥蜞 祛滂翳赅鲨? - 25.09.2000
5 // 叔?祛滂翳鲨痤忄磬 - S. Sokolenko
6 // In -
7 // strSelectedFolder - reference to string for store folder path
8 // Out -
9 // lpszTitle - title for caption
10 // hwndOwner - reference to parent window
11 // strRootFolder - root folder
12 // strStartFolder - current foldet
13 // Return - TRUE if user select OK, else FALSE.
14 //*********************************************************************************
15 CString strTmpPath;
16
17 int CALLBACK BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData)
18 {
19 TCHAR szDir[MAX_PATH];
20 switch(uMsg){
21 case BFFM_INITIALIZED:
22 if (lpData){
23 strcpy(szDir, strTmpPath.GetBuffer(strTmpPath.GetLength()));
24 SendMessage(hwnd,BFFM_SETSELECTION,TRUE,(LPARAM)szDir);
25 }
26 break;
27 case BFFM_SELCHANGED: {
28 if (SHGetPathFromIDList((LPITEMIDLIST) lParam ,szDir)){
29 SendMessage(hwnd,BFFM_SETSTATUSTEXT,0,(LPARAM)szDir);
30 }
31 break;
32 }
33 default:
34 break;
35 }
36
37 return 0;
38 }
39
40 BOOL GetFolder(CString* strSelectedFolder,
41 const char* lpszTitle,
42 const HWND hwndOwner,
43 const char* strRootFolder,
44 const char* strStartFolder)
45 {
46 char pszDisplayName[MAX_PATH];
47 LPITEMIDLIST lpID;
48 BROWSEINFOA bi;
49
50 bi.hwndOwner = hwndOwner;
51 if (strRootFolder == NULL){
52 bi.pidlRoot = NULL;
53 }else{
54 LPITEMIDLIST pIdl = NULL;
55 IShellFolder* pDesktopFolder;
56 char szPath[MAX_PATH];
57 OLECHAR olePath[MAX_PATH];
58 ULONG chEaten;
59 ULONG dwAttributes;
60
61 strcpy(szPath, (LPCTSTR)strRootFolder);
62 if (SUCCEEDED(SHGetDesktopFolder(&pDesktopFolder)))
63 {
64 MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, szPath, -1, olePath, MAX_PATH);
65 pDesktopFolder->ParseDisplayName(NULL, NULL, olePath, &chEaten, &pIdl, &dwAttributes);
66 pDesktopFolder->Release();
67 }
68 bi.pidlRoot = pIdl;
69 }
70 bi.pszDisplayName = pszDisplayName;
71 bi.lpszTitle = lpszTitle;
72 bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_STATUSTEXT;
73 bi.lpfn = BrowseCallbackProc;
74 if (strStartFolder == NULL){
75 bi.lParam = FALSE;
76 }else{
77 strTmpPath.Format("%s", strStartFolder);
78 bi.lParam = TRUE;
79 }
80 bi.iImage = NULL;
81 lpID = SHBrowseForFolderA(&bi);
82 if (lpID != NULL){
83 BOOL b = SHGetPathFromIDList(lpID, pszDisplayName);
84 if (b == TRUE){
85 strSelectedFolder->Format("%s",pszDisplayName);
86 return TRUE;
87 }
88 }else{
89 strSelectedFolder->Empty();
90 }
91 return FALSE;
92 }
93
94 void CGetFolderDlg::OnGetfolder()
95 {
96 CString strFolderPath;
97 if (GetFolder(&strFolderPath, "Sample of getting folder.", this->m_hWnd, NULL, NULL)){
98 if (!strFolderPath.IsEmpty()){
99 m_strFolderPath = strFolderPath;
100 UpdateData(FALSE);
101 }
102 }
103 }

 

posted on 2010-06-15 23:32  Imagination  阅读(818)  评论(0编辑  收藏  举报

导航