// FolderDialog.h: interface for the CFolderDialog class.
//
//
#if !defined(AFX_FOLDERDIALOG_H__3E0B1DD2_0700_482B_83A9_62A35402D200__INCLUDED_)
#define AFX_FOLDERDIALOG_H__3E0B1DD2_0700_482B_83A9_62A35402D200__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include <shlobj.h>
class CFolderDialog
{
public:
CFolderDialog(LPCTSTR lpszFolderName = NULL,DWORD dwFlags = NULL,CWnd* pParentWnd = NULL);
virtual ~CFolderDialog();
DoModal();
CString GetPathName() const;
void CallbackBrowsFolder(HWND hWnd, UINT uMsg,LPARAM lParam);
protected:
void OnInitDialog();
void OnSelChanged(ITEMIDLIST* pIdl);
void SetSelFolder(LPCTSTR pszSelFolder);
void SetDisplayText(LPCTSTR pszDisplay);
CString GetShortDisplayText(CString strDisplayText);
private:
BROWSEINFO m_BrowInfo;
CString m_strInitFolderName;
char m_szDisplayFolderName[MAX_PATH];
char m_szFolderPath[MAX_PATH];
HWND m_hWnd;
public:
CString m_strFinalFolderName;
};
#endif // !defined(AFX_FOLDERDIALOG_H__3E0B1DD2_0700_482B_83A9_62A35402D200__INCLUDED_)
**************************
// FolderDialog.cpp: implementation of the CFolderDialog class.
//
//
#include "stdafx.h"
#include "FolderDialog.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//
// Construction/Destruction
//
static int CALLBACK CallBackFun(HWND hWnd, UINT uMsg,LPARAM lParam,LPARAM lpData)
{
CFolderDialog* pFd = (CFolderDialog*)lpData;
pFd->CallbackBrowsFolder(hWnd, uMsg, lParam);
return 0;
}
void CFolderDialog::CallbackBrowsFolder(HWND hWnd, UINT uMsg,LPARAM lParam)
{
m_hWnd = hWnd;
switch (uMsg)
{
case BFFM_INITIALIZED:
OnInitDialog();
break;
case BFFM_SELCHANGED:
OnSelChanged((ITEMIDLIST*) lParam);
break;
}
}
CFolderDialog::CFolderDialog(LPCTSTR lpszFolderName,DWORD dwFlags,CWnd* pParentWnd)
{
if (lpszFolderName == NULL)
m_strInitFolderName = _T("");
else
m_strInitFolderName = lpszFolderName;
memset(&m_BrowInfo, '/0', sizeof(BROWSEINFO));
if (pParentWnd == NULL)
m_BrowInfo.hwndOwner = 0;
else
m_BrowInfo.hwndOwner = pParentWnd->GetSafeHwnd();
m_BrowInfo.pidlRoot = NULL;
m_BrowInfo.pszDisplayName = m_szDisplayFolderName;
m_BrowInfo.lpszTitle = _T("参照先: ");
m_BrowInfo.ulFlags = dwFlags | BIF_STATUSTEXT;
m_BrowInfo.lpfn = CallBackFun;
m_BrowInfo.lParam = (LPARAM)this;
}
CFolderDialog::~CFolderDialog()
{
}
void CFolderDialog::OnInitDialog()
{
SetSelFolder(m_strInitFolderName);
SetDisplayText(m_strInitFolderName);
}
void CFolderDialog::SetSelFolder(LPCTSTR pszSelFolder)
{
::SendMessage(m_hWnd, BFFM_SETSELECTION, TRUE, (LPARAM) pszSelFolder);
}
CString CFolderDialog::GetPathName() const
{
return m_strFinalFolderName;
}
int CFolderDialog::DoModal()
{
int nRet = IDOK;
ITEMIDLIST* pItemList = NULL;
m_strFinalFolderName = m_strInitFolderName;
pItemList = ::SHBrowseForFolder(&m_BrowInfo);
if (pItemList && ::SHGetPathFromIDList(pItemList, m_szFolderPath))
{
m_strFinalFolderName = m_szFolderPath;
nRet = IDOK;
}
else
nRet = IDCANCEL;
if (pItemList)
{
LPMALLOC lpMalloc;
VERIFY(::SHGetMalloc(&lpMalloc) == NOERROR);
lpMalloc->Free(pItemList);
lpMalloc->Release();
}
return nRet;
}
void CFolderDialog::OnSelChanged(ITEMIDLIST* pIdl)
{
::SHGetPathFromIDList(pIdl, m_szFolderPath);
m_strFinalFolderName = m_szFolderPath;
SetDisplayText(m_strFinalFolderName);
}
void CFolderDialog::SetDisplayText(LPCTSTR pszDisplay)
{
::SendMessage(m_hWnd, BFFM_SETSTATUSTEXT,TRUE, (LPARAM) pszDisplay);
}
CString CFolderDialog::GetShortDisplayText(CString strDisplayText)
{
CString str;
if (strDisplayText.GetLength()<30)
str = strDisplayText;
else
{
str = strDisplayText.Left(30);
str += "...";
return str;
}
return str;
}