VC实现在浏览目录对话框中选择目录

    使用MFC中的CFileDialog类,我们可以很方便的在目录对话框中选择打开或保存文件,但是有时我们需要的是在其中选择一个文件夹,这在MFC中并没有提供.这里提供一个简单的类来实现这个要求,是我在别人的程序代码中找到的.这个类是CDirDlg.

    CDirDlg类的头文件DIR.H

#if!defined _THIS_IS_MY_DIRECTORY_SELECT_CLASS_
#define _THIS_IS_MY_DIRECTORY_SELECT_CLASS_
class CDirDlg  
{
public:
CDirDlg();
virtual ~CDirDlg();

CString GetDirectory(CWnd *pParent=NULL,LPCSTR lpszRoot="c://",LPCSTR lpszTitle="Please Pick a Directory";

static CString m_sRootDir;

};
int CALLBACK BrowseCallbackProc(HWND hwnd,UINT uMsg,LPARAM lp, LPARAM pData);

#endif

源文件DIR.CPP


#include "stdafx.h"
#include "dir.h" 

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/

CString CDirDlg::m_sRootDir;

//
// Construction/Destruction
//

CDirDlg::CDirDlg()
{

}

CDirDlg::~CDirDlg()
{

}

CString CDirDlg::GetDirectory(CWnd *pParent,LPCSTR lpszRoot,LPCSTR lpszTitle)
{
CString str;
BROWSEINFO bi;
    bi.hwndOwner=pParent->m_hWnd; //owner of created dialog box
    bi.pidlRoot=0; //unused
    bi.pszDisplayName=0; //buffer to receive name displayed by folder (not a valid path)
    bi.lpszTitle=lpszTitle; //title is "Browse for Folder", this is an instruction
bi.lpfn = BrowseCallbackProc; //callback routine called when dialog has been initialized
    bi.lParam=0; //passed to callback routine
    bi.ulFlags=
BIF_RETURNONLYFSDIRS | //only allow user to select a directory
BIF_STATUSTEXT | //create status text field we will be writing to in callback
// BIF_BROWSEFORCOMPUTER| //only allow user to select a computer
// BIF_BROWSEFORPRINTER | //only allow user to select a printer
// BIF_BROWSEINCLUDEFILES| //displays files too which user can pick
// BIF_DONTGOBELOWDOMAIN| //when user is exploring the "Entire Network" they
// are not allowed into any domain
0; 
m_sRootDir=lpszRoot;

LPITEMIDLIST lpItemId=::SHBrowseForFolder(&bi); 
if (lpItemId)
{
LPTSTR szBuf=str.GetBuffer(MAX_PATH);
::SHGetPathFromIDList(lpItemId, szBuf);
::GlobalFree(lpItemId);
str.ReleaseBuffer();
}

return str;
}

int CALLBACK BrowseCallbackProc(HWND hwnd,UINT msg,LPARAM lp, LPARAM pData)
{
TCHAR buf[MAX_PATH];

switch(msg) 
{
// when dialog is first initialized, change directory to one chosen above
case BFFM_INITIALIZED: 
strcpy(buf,CDirDlg::m_sRootDir);
::SendMessage(hwnd,BFFM_SETSELECTION,TRUE,(LPARAM)buf);
break;

// if you picked BIF_STATUSTEXT above, you can fill status here
case BFFM_SELCHANGED:
if (::SHGetPathFromIDList((LPITEMIDLIST) lp ,buf)) 
SendMessage(hwnd,BFFM_SETSTATUSTEXT,0,(LPARAM)buf);
break;
}
return 0;
}

使用方法:在你的工程中添加这两个文件进去,然后会在类视图中出现在CDirDlg类,该类中只有一个方法GetDirectory(),可以像下面这样用:
CDirDlg dlg;
m_strPath=dlg.GetDirectory(this,"我的电脑";

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值