文件夹对话框类和调用

#pragma once

// CMyFolderDlg 命令目标

class CMyFolderDlg : public CObject
{
public:
	CMyFolderDlg(LPCITEMIDLIST pidlRoot,LPTSTR lpszDisplayName,
		LPCTSTR lpszTitle,UINT nFlags=BIF_RETURNFSANCESTORS);
	virtual ~CMyFolderDlg();
	int DoModal();
	CString GetPathName();
protected:
	BROWSEINFO m_bi;
	LPCITEMIDLIST m_pidl;
};
。cpp
// MyFolderDlg.cpp : 实现文件
//

#include "stdafx.h"
#include "Demo154.h"
#include "MyFolderDlg.h"


// CMyFolderDlg

CMyFolderDlg::CMyFolderDlg(LPCITEMIDLIST pidlRoot,LPTSTR lpszDisplayName,
						   LPCTSTR lpszTitle,UINT nFlags/* =BIF_RETURNFSANCESTORS */)
{
	memset(&m_bi,0,sizeof(BROWSEINFO));
	m_bi.pidlRoot = pidlRoot;
	m_bi.pszDisplayName = lpszDisplayName;
	m_bi.lpszTitle = lpszTitle;
	m_bi.ulFlags = nFlags;
	m_pidl = NULL;
}

CMyFolderDlg::~CMyFolderDlg()
{
}


// CMyFolderDlg 成员函数
int CMyFolderDlg::DoModal()
{
	int nResult = 0;
	m_pidl = SHBrowseForFolder(&m_bi);
	if (m_pidl != NULL)
	{
		nResult = IDOK;
	} 
	else
	{
		nResult = IDCANCEL;
	}
	return nResult;
}
CString CMyFolderDlg::GetPathName()
{
	CString strPathName = _T("");
	TCHAR szPathName[MAX_PATH];
	if (::SHGetPathFromIDList(m_pidl,szPathName))
	{
		strPathName = szPathName;
		if (strPathName.Right(1) == _T("\\"))
		{
			strPathName = strPathName.Left(strPathName.GetLength()-1);
		}
	}
	return strPathName;

调用如下:
OnBnClickedButton5()
{
	// TODO: 在此添加控件通知处理程序代码
	CMyFolderDlg dlg(NULL,NULL,_T("文件列表"),BIF_RETURNFSANCESTORS);
	if (dlg.DoModal() == IDOK)
	{
// 		CString strPath = dlg.GetPathName();
// 		CString strText = _T("");
// 		strText.Format(_T("%s"),strPath);
// 		AfxMessageBox(strText);

		CString strPathName = dlg.GetPathName();
		DWORD dwFileAttriBures = ::GetFileAttributes(strPathName);
		CString strFileAttributes = _T("");
		if (dwFileAttriBures & FILE_ATTRIBUTE_NORMAL)
		{
			strFileAttributes += _T("无\n");
		}
		if (dwFileAttriBures & FILE_ATTRIBUTE_DIRECTORY)
		{
			strFileAttributes =+ _T("目录\n");
		}
		if (dwFileAttriBures & FILE_ATTRIBUTE_ARCHIVE)
		{
			strFileAttributes += _T("存档\n");
		}
		if (dwFileAttriBures & FILE_ATTRIBUTE_HIDDEN)
		{
			strFileAttributes += _T("隐藏\n");
		}
		if (dwFileAttriBures & FILE_ATTRIBUTE_READONLY)
		{
			strFileAttributes += _T("只读\n");
		}
		if (dwFileAttriBures & FILE_ATTRIBUTE_SYSTEM)
		{
			strFileAttributes += _T("系统\n");
		}
		if (dwFileAttriBures & FILE_ATTRIBUTE_TEMPORARY)
		{
			strFileAttributes += _T("临时\n");
		}
		CString strText = _T("");
		strText.Format(_T("文件属性:\n%s"),strFileAttributes);
		AfxMessageBox(strText);
	}
}


 
 

你可能感兴趣的:(个人心得)