📄 folderdialog.cpp
字号:
// FolderDialog.cpp: CFolderDialog 类
#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 BrowseDirectoryCallback(
HWND hWnd, UINT uMsg, LPARAM lParam, LPARAM lpData)
{
// Context was pointer to CFolderDialog instance
CFolderDialog* pFd = (CFolderDialog*)lpData;
// Let the class handle it
pFd->CallbackFunction(hWnd, uMsg, lParam);
// Always return 0 as per SHBrowseFolder spec
return 0;
}
/*
lpszFolderName:初始目录名
lpszDispName:对话框显示标题
*/
CFolderDialog::CFolderDialog(LPCTSTR lpszFolderName,LPCTSTR lpszDispName, DWORD dwFlags, CWnd* pParentWnd)
{
// Use the supplied initial folder if non-null.
if (lpszFolderName == NULL)
m_strInitialFolderName = _T("");
else
m_strInitialFolderName = lpszFolderName;
memset(&m_bi, '\0', sizeof(BROWSEINFO));
if (pParentWnd == NULL)
m_bi.hwndOwner = 0;
else
m_bi.hwndOwner = pParentWnd->GetSafeHwnd();
if (lpszDispName==NULL)
m_bi.lpszTitle=_T("请选择文件夹:");
else
m_bi.lpszTitle=lpszDispName;
// Fill in the rest of the structure
m_bi.pidlRoot = NULL;
m_bi.pszDisplayName = m_szDisplayName;
m_bi.ulFlags = dwFlags | BIF_STATUSTEXT|BIF_DONTGOBELOWDOMAIN;
m_bi.lpfn = BrowseDirectoryCallback;
m_bi.lParam = (LPARAM)this;
}
CFolderDialog::~CFolderDialog()
{
}
void CFolderDialog::CallbackFunction(HWND hWnd, UINT uMsg, LPARAM lParam)
{
// Save the window handle. The Set* functions need it and they may
// be called by the virtual funtions.
m_hDialogBox = hWnd;
// Dispatch the two message types to the virtual functions
switch (uMsg)
{
case BFFM_INITIALIZED:
OnInitDialog();
break;
case BFFM_SELCHANGED:
OnSelChanged((ITEMIDLIST*) lParam);
break;
}
}
int CFolderDialog::DoModal()
{
int nReturn = IDOK;
// initialize the result to the starting folder value
m_strFinalFolderName = m_strInitialFolderName;
ITEMIDLIST* piid = NULL;
// call the shell function
piid = ::SHBrowseForFolder(&m_bi);
// process the result
if (piid && ::SHGetPathFromIDList(piid, m_szPath))
{
m_strFinalFolderName = m_szPath;
nReturn = IDOK;
}
else
{
nReturn = IDCANCEL;
}
// Release the ITEMIDLIST if we got one
if (piid)
{
LPMALLOC lpMalloc;
VERIFY(::SHGetMalloc(&lpMalloc) == NOERROR);
lpMalloc->Free(piid);
lpMalloc->Release();
}
return nReturn;
}
//返回选择路径
CString CFolderDialog::GetPathName() const
{
return m_strFinalFolderName;
}
//使确认按钮有效
void CFolderDialog::EnableOK(BOOL bEnable)
{
::SendMessage(m_hDialogBox, BFFM_ENABLEOK, (bEnable ? 1 : 0), 0);
}
//设置选择路径,文件路径方式
void CFolderDialog::SetSelection(LPCTSTR pszSelection)
{
::SendMessage(m_hDialogBox, BFFM_SETSELECTION, TRUE, (LPARAM) pszSelection);
}
//设置选择路径,ID方式
void CFolderDialog::SetSelection(ITEMIDLIST* pIdl)
{
::SendMessage(m_hDialogBox, BFFM_SETSELECTION, FALSE, (LPARAM) pIdl);
}
//设置状态行方式为当前选择目录
void CFolderDialog::SetStatusText(LPCTSTR pszStatusText)
{
::SendMessage(m_hDialogBox, BFFM_SETSTATUSTEXT, 0, (LPARAM) pszStatusText);
}
CString CFolderDialog::ShortName(const CString& strName)
{
CString strShort;
if (strName.GetLength() <= 35)
strShort = strName;
else
strShort = strName.Left(35) + _T("...");
return strShort;
}
//初始化对话框
void CFolderDialog::OnInitDialog()
{
// Default handing of the init dialog message sets the selection to
// the initial folder
SetSelection(m_strInitialFolderName);
SetStatusText(ShortName(m_strInitialFolderName));
}
//选择改变
void CFolderDialog::OnSelChanged(ITEMIDLIST* pIdl)
{
::SHGetPathFromIDList(pIdl, m_szPath);
m_strFinalFolderName = m_szPath;
SetStatusText(ShortName(m_strFinalFolderName));
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -