#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);
}
}