您的位置:首页 > 其它

CFolderDialog,CFolderPickerDialog选择文件夹及获得当前程序路径

2010-05-11 14:54 393 查看
CFolderDialog是从
CCommonDialog类派生而来,在选择文件夹时非常好用,使用也很简单,下载地址在这里,原地址在这里




只要.h和.cpp放入工程中,并且在头文件里加#include "FolderDlg.h"就可以了。


在要弹出文件夹选择的地方


CString FoldPath;//可以自己指定文件夹路径如_T(“C://down”)
CString m_strFolderPath;//保存获得的文件夹路径
CFolderDialog dlg(_T( "选择要转换的文件夹" ), FoldPath, this);
//如果要出现新建文件夹按钮的话 可以这样CFolderDialog dlg(_T( "选择要转换的文件夹" ), FoldPath, this,BIF_NEWDIALOGSTYLE);
if( dlg.DoModal() == IDOK  )
    {    
        m_strFolderPath  = dlg.GetFolderPath();    //获得路径
//文件夹路径 D:/xxxx
        m_strTemp = m_strFolderPath;
    }
    else
    {
        return;
    }


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

常常需要获得程序运行时所在目录,可以用下面的简单方法

TCHAR szPath[MAX_PATH];
CString FoldPath;
    GetModuleFileName(NULL,LPWCH(szPath),MAX_PATH);//获得包括程序名的完整路径C:/down/a.txt
    PathRemoveFileSpec(szPath);       //去掉文件名的路径C:/down
    FoldPath.Format(szPath);       //


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

以上2个方法配合使用就可以实现指定要打开的文件夹位置

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

2014.12.6

在VS2010以上版本中,MS添加了原生的文件夹选择类CFolderPickerDialog,也还比较好用,举例如下

CString workingDirectory;
	// OPTOINAL: Let's initialize the directory to the users home directory, assuming a max of 256 characters for path name:
	wchar_t temp[256];

	GetEnvironmentVariable(_T("userprofile"), temp, 256);

	workingDirectory = temp;
	CFolderPickerDialog dlg(workingDirectory, 0, NULL, 0); 

	if (dlg.DoModal()) 

	{

		m_strFile = dlg.GetPathName() ;
		AfxMessageBox(m_strFile);
		

	}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: