现在的位置: 首页 > 综合 > 正文

在MFC对话框中选择文件夹

2013年09月22日 ⁄ 综合 ⁄ 共 6281字 ⁄ 字号 评论关闭

可参考  http://www.zhangyun.net/?p=84  有对ITEMIDLIST 简单介绍

 

有一项目,基本操作为选择一个文件夹,并对该文件夹中的文件进行操作。在MFC中没有找到可以选择文件夹(目录)的类和控件,只好在源码中使用固定路径,每换一个文件夹,都要修改源码,并重新编译。不愿再懒下去了,就在网上搜了一下,找到了解决的办法。
  在对话框中加一按钮,该按钮对应代码如下(在win2000 sp4 + VC6.0下调试通过):
void CGetDirTestDlg::OnBrowse()
{
 // TODO: Add your control notification handler code here
 CString str;
 BROWSEINFO bi;
 char name[MAX_PATH];
 ZeroMemory(&bi,sizeof(BROWSEINFO));     //作用为给所有参数都设为0,即NULL
 bi.hwndOwner = GetSafeHwnd();              //调用该对话框的父窗口句柄
 bi.pszDisplayName = name;                      //用来保存用户选中的目录字符串的内存地址。该缓冲区的大小缺省是定义的MAX_PATH常量宏。
 bi.lpszTitle = "Select folder";                   //该浏览文件夹对话框对话框的显示文本,用来提示该浏览文件夹对话框的功能、作用和目的。

 //bi.ulFlags = BIF_USENEWUI;
 bi.ulFlags = BIF_RETURNFSANCESTORS;
 LPITEMIDLIST idl = SHBrowseForFolder(&bi);         //SHBrowseForFolder用来显示一个让用户可以选择文件夹的对话框,返回值是指向选定的文件夹相对应于所选择根目录地址的路径标识符指针。定                                                                               //LPITEMIDLIST  包含了各条目的ID信息
 if(idl == NULL)
  return;
 SHGetPathFromIDList(idl, str.GetBuffer(MAX_PATH));
 str.ReleaseBuffer();                                 

 m_strPath = str;//为对话框中与一编辑框对应的CString型变量,保存并显示选中的路径。
 if(str.GetAt(str.GetLength()-1)!='//')
  m_strPath+="//";
 UpdateData(FALSE);        //用来刷新m_strPath所对应的CEdit控件显示选中的文件夹路径
}

*******************************************************************************************
MSDN中的相关知识
*******************************************************************************************
Visual C++(VC)中,BROWSEINFO结构中包含有用户选中目录的重要信息。

  (1)BROWSEINFO结构

  ●定义

  typedef struct_browseinfo

  {

  HWND hwndOwner;

  LPCITEMIDLIST pidlRoot;

  LPSTR pszDisplayName;

  LPCSTR lpszTitle;

  UINT ulFlags;

  BFFCALLBACK lpfn;

  LPARAM lParam;

  int iImage;

  }BROWSEINFO,*PBROWSEINFO,*LPBROWSEINFO;

  ●成员变量

  hwndOwner:浏览文件夹对话框的父窗体句柄。

  pidlRoot:ITEMIDLIST结构的地址,包含浏览时的初始根目录,而且只有被指定的目录和其子目录才显示在浏览文件夹对话框中。该成员变量可以是NULL,在此时桌面目录将被使用。

  pszDisplayName:用来保存用户选中的目录字符串的内存地址。该缓冲区的大小缺省是定义的MAX_PATH常量宏。

  lpszTitle:该浏览文件夹对话框对话框的显示文本,用来提示该浏览文件夹对话框的功能、作用和目的。

  ulFlags:该标志位描述了对话框的选项。它可以为0,也可以是以下常量的任意组合:

  BIF_BROWSEFORCOMPUTER:返回计算机名。除非用户选中浏览器中的一个计算机名,否则该对话框中的“OK”按钮为灰色。

  BIF_BROWSEFORPRINTER:返回打印机名。除非选中一个打印机名,否则“OK”按钮为灰色。

  BIF_BROWSEINCLUDEFILES:浏览器将显示目录,同时也显示文件。

  BIF_DONTGOBELOWDOMAIN:在树形视窗中,不包含域名底下的网络目录结构。

  BIF_EDITBOX:浏览对话框中包含一个编辑框,在该编辑框中用户可以输入选中项的名字。

  BIF_RETURNFSANCESTORS:返回文件系统的一个节点。仅仅当选中的是有意义的节点时,“OK”按钮才可以使用。

  BIF_RETURNONLYFSDIRS:仅仅返回文件系统的目录。例如:在浏览文件夹对话框中,当选中任意一个目录时,该“OK”按钮可用,而当选中“我的电脑”或“网上邻居”等非有意义的节点时,“OK”按钮为灰色。

  BIF_STATUSTEXT:在对话框中包含一个状态区域。通过给对话框发送消息使回调函数设置状态文本。

  BIF_VALIDATE:当没有BIF_EDITBOX标志位时,该标志位被忽略。如果用户在编辑框中输入的名字非法,浏览对话框将发送BFFM_VALIDATEFAILED消息给回调函数。

  lpfn:应用程序定义的浏览对话框回调函数的地址。当对话框中的事件发生时,该对话框将调用回调函数。该参数可用为NULL。

  lParam:对话框传递给回调函数的一个参数指针。

  iImage:与选中目录相关的图像。该图像将被指定为系统图像列表中的索引值。

 

 

 
详细:
对于SHGetPathFromIDList函数,string length和structure length可以被修改并导致快捷方式出错。

SHGetPathFromIDList
功能是把项目标志符列表转换为文件系统路径:
BOOL SHGetPathFromIDList(
  LPCITEMIDLIST pidl,
  LPSTR pszPath
);

参数:
pidl---相对 namespace 的根指定一个文件或目录地点的一张项目标识符表的地址 ( 桌面 ) ;
pszPath---接收文件系统路径的缓冲地址,大小至少是MAX_PATH的字符长度

这里是对一个快捷方式文件的解析:【转自www.bitsCN.com

 

 

BROWSEINFO

 

typedef struct _browseinfo {

    HWND hwndOwner;

    LPCITEMIDLIST pidlRoot;

    LPSTR pszDisplayName;

    LPCSTR lpszTitle;

    UINT ulFlags;

    BFFCALLBACK lpfn;

    LPARAM lParam;

    int iImage;

} BROWSEINFO, *PBROWSEINFO, *LPBROWSEINFO;

Contains parameters for the SHBrowseForFolder function and receives information about the folder selected by the user.

hwndOwner

Handle to the owner window for the dialog box.

pidlRoot

Address of an ITEMIDLIST structure specifying the location of the root folder from which to browse. Only the specified folder and its subfolders appear in the dialog box. This member can be NULL; in that case, the namespace root (the desktop folder) is used.

pszDisplayName

Address of a buffer to receive the display name of the folder selected by the user. The size of this buffer is assumed to be MAX_PATH bytes.

lpszTitle

Address of a null-terminated string that is displayed above the tree view control in the dialog box. This string can be used to specify instructions to the user.

ulFlags

Flags specifying the options for the dialog box. This member can include zero or a combination of the following values:

BIF_BROWSEFORCOMPUTER
 Only return computers. If the user selects anything other than a computer, the OK button is grayed.
 
BIF_BROWSEFORPRINTER
 Only return printers. If the user selects anything other than a printer, the OK button is grayed.
 
BIF_BROWSEINCLUDEFILES
 The browse dialog will display files as well as folders.
 
BIF_DONTGOBELOWDOMAIN
 Do not include network folders below the domain level in the tree view control.
 
BIF_EDITBOX
 Version 4.71. The browse dialog includes an edit control in which the user can type the name of an item.
 
BIF_RETURNFSANCESTORS
 Only return file system ancestors. If the user selects anything other than a file system ancestor, the OK button is grayed.
 
BIF_RETURNONLYFSDIRS
 Only return file system directories. If the user selects folders that are not part of the file system, the OK button is grayed.
 
BIF_STATUSTEXT
 Include a status area in the dialog box. The callback function can set the status text by sending messages to the dialog box.
 
BIF_VALIDATE
 Version 4.71. If the user types an invalid name into the edit box, the browse dialog will call the application's BrowseCallbackProc with the BFFM_VALIDATEFAILED message. This flag is ignored if BIF_EDITBOX is not specified.
 

lpfn

Address of an application-defined function that the dialog box calls when an event occurs. For more information, see the BrowseCallbackProc function. This member can be NULL.

lParam

Application-defined value that the dialog box passes to the callback function, if one is specified.

iImage

Variable to receive the image associated with the selected folder. The image is specified as an index to the system image list.

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

SHBrowseForFolder

 

WINSHELLAPI LPITEMIDLIST WINAPI SHBrowseForFolder(

    LPBROWSEINFO lpbi

);

Displays a dialog box that enables the user to select a shell folder.

·   Returns the address of an item identifier list that specifies the location of the selected folder relative to the root of the namespace. If the user chooses the Cancel button in the dialog box, the return value is NULL.

lpbi

Address of a BROWSEINFO structure that contains information used to display the dialog box.

The calling application is responsible for freeing the returned item identifier list by using the shell's task allocator.

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

ITEMIDLIST

 

typedef struct _ITEMIDLIST {

    SHITEMID mkid;

} ITEMIDLIST, * LPITEMIDLIST;

typedef const ITEMIDLIST * LPCITEMIDLIST;

Contains a list of item identifiers.

mkid

List of item identifiers.

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/qizilong/archive/2009/06/16/4271981.aspx

抱歉!评论已关闭.