folderdialog.cpp

来自「我要下载源代码我要源代码我要下载代码我要下载源代码」· C++ 代码 · 共 129 行

CPP
129
字号
// FolderDialog.cpp: implementation of the CFolderDialog class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "DirDialog.h"
#include "FolderDialog.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CFolderDialog::	CFolderDialog(LPCSTR lpszFolderName, DWORD dwFlags,CWnd *pParentWnd)
{
	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();
	m_bi.pidlRoot = NULL;
	m_bi.pszDisplayName = m_szDisplayName;
	m_bi.lpszTitle = _T("Current selection");
	m_bi.ulFlags = dwFlags | BIF_STATUSTEXT;
	m_bi.lpfn = BrowseDirectoryCallback;
	m_bi.lParam = (LPARAM)this;

}

CFolderDialog::~CFolderDialog()
{

}

int CALLBACK BrowseDirectoryCallback(HWND hWnd, UINT uMsg, LPARAM lParam, LPARAM lpData)
{
	CFolderDialog *pFD = (CFolderDialog *) lpData;
	pFD->CallbackFunction(hWnd,uMsg,lParam);
	return 0;
}

void CFolderDialog::CallbackFunction(HWND hWnd, UINT uMsg, LPARAM lParam)
{

	m_hDialogBox = hWnd;
	switch(uMsg)
	{
	case BFFM_INITIALIZED:
		OnInitDialog();
		break;
	case BFFM_SELCHANGED:
		OnSelChanged( (ITEMIDLIST *) lParam);
		break;
	}
}

void CFolderDialog::OnInitDialog()
{
	SetSelection(m_strInitialFolderName);
	SetStatusText(m_strInitialFolderName);
}

void CFolderDialog::OnSelChanged(ITEMIDLIST *pIdl)
{
	::SHGetPathFromIDList(pIdl,m_szPath);
	m_strFinalFolderName = m_szPath;
	SetStatusText(m_strFinalFolderName);
}

void CFolderDialog::SetSelection(LPCTSTR pszSelection)
{
	::SendMessage(m_hDialogBox,BFFM_SELCHANGED,TRUE,(LPARAM) pszSelection);
}

void CFolderDialog::SetSelection(ITEMIDLIST *pIdl)
{
	::SendMessage(m_hDialogBox,BFFM_SELCHANGED,FALSE,(LPARAM) pIdl);
}

void CFolderDialog::SetStatusText(CString strStatusText)
{
	char *pszStatusText;
	pszStatusText = new char[strStatusText.GetLength() +1];
	strcpy(pszStatusText,strStatusText);

	::SendMessage(m_hDialogBox,BFFM_SETSTATUSTEXT,0,(LPARAM) pszStatusText);
}

int CFolderDialog::DoModal()
{
	int nReturn = IDOK;
	m_strFinalFolderName = m_strInitialFolderName;
	ITEMIDLIST *piid = NULL;

	piid = ::SHBrowseForFolder(&m_bi);

	if(piid && ::SHGetPathFromIDList(piid,m_szPath) )
	{
		m_strFinalFolderName = m_szPath;
		nReturn = IDOK;
	}
	else
		nReturn = IDCANCEL;

	if(piid)
	{
		LPMALLOC lpMalloc;
		VERIFY(::SHGetMalloc(&lpMalloc) == NOERROR);
		lpMalloc->Free(piid);
		lpMalloc->Release();
	}
	return nReturn;
}

CString CFolderDialog::GetPathName()
{
	return m_strFinalFolderName;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?