⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 folderdialog.cpp

📁 visual c++程序设计实训源代码
💻 CPP
字号:
// 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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -