在MFC对话框中选择文件夹

  有一项目,基本操作为选择一个文件夹,并对该文件夹中的文件进行操作。在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));
 bi.hwndOwner = GetSafeHwnd();
 bi.pszDisplayName = name;
 bi.lpszTitle = "Select folder";
 //bi.ulFlags = BIF_USENEWUI;
 bi.ulFlags = BIF_RETURNFSANCESTORS;
 LPITEMIDLIST idl = SHBrowseForFolder(&bi);
 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); 
}
*******************************************************************************************
MSDN中的相关知识
*******************************************************************************************

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.

欢迎访问:乐园www.ly8.co

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值