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

📄 browseforfolder.cpp

📁 创建巨型文件的参考源代码
💻 CPP
字号:
// ShellBrowser.cpp: implementation of the CShellBrowser class.

#include "stdafx.h"
#include "BrowseForFolder.h"
#include <shlobj.h>
#include <shlwapi.h>

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

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

CBrowseForFolder::CBrowseForFolder(const HWND hParent /* NULL */, const LPITEMIDLIST pidl /* NULL */, const int nTitleID /* 0 */ )
{
	ZeroMemory(m_szSelected, MAX_PATH);
	m_hwnd = NULL;
	SetOwner(hParent);
	SetRoot(pidl);
	m_bi.lpszTitle = NULL;
	SetTitle(nTitleID);
	m_bi.lpfn = BrowseCallbackProc;
	m_bi.lParam = reinterpret_cast < long > (this);
	m_bi.pszDisplayName = m_szSelected;
}

// /////////////////////////////////////////////////////////////////////////////
// ///////////////////////////////////////////////////////////////////////////////
CBrowseForFolder::CBrowseForFolder(const HWND hParent, const LPITEMIDLIST pidl, const CString& strTitle)
{
	ZeroMemory(m_szSelected, MAX_PATH);
	m_hwnd = NULL;
	SetOwner(hParent);
	SetRoot(pidl);
	m_bi.lpszTitle = NULL;
	SetTitle(strTitle);
	m_bi.lpfn = BrowseCallbackProc;
	m_bi.lParam = reinterpret_cast < long > (this);
	m_bi.pszDisplayName = m_szSelected;
}

// /////////////////////////////////////////////////////////////////////////////
// ///////////////////////////////////////////////////////////////////////////////
CBrowseForFolder::~CBrowseForFolder(void)
{
}

// /////////////////////////////////////////////////////////////////////////////
//    Implementation
// /////////////////////////////////////////////////////////////////////////////

void CBrowseForFolder::SetOwner(const HWND hwndOwner)
{
	if(m_hwnd != NULL)
		return;

	m_bi.hwndOwner = hwndOwner;
}

// /////////////////////////////////////////////////////////////////////////////
// ///////////////////////////////////////////////////////////////////////////////
void CBrowseForFolder::SetRoot(const LPITEMIDLIST pidl)
{
	if(m_hwnd != NULL)
		return;

	m_bi.pidlRoot = pidl;
}

// /////////////////////////////////////////////////////////////////////////////
// ///////////////////////////////////////////////////////////////////////////////
CString CBrowseForFolder::GetTitle(void) const
{
	return m_bi.lpszTitle;
}

// /////////////////////////////////////////////////////////////////////////////
// ///////////////////////////////////////////////////////////////////////////////
void CBrowseForFolder::SetTitle(const CString& strTitle)
{
	if(m_hwnd != NULL)
		return;

	m_pchTitle = std::auto_ptr < TCHAR > (new TCHAR[static_cast<size_t>(strTitle.GetLength()) + 1]);
	_tcscpy(m_pchTitle.get(), strTitle);
	m_bi.lpszTitle = m_pchTitle.get();
}

// /////////////////////////////////////////////////////////////////////////////
// ///////////////////////////////////////////////////////////////////////////////
bool CBrowseForFolder::SetTitle(const int nTitle)
{
	if(nTitle <= 0)
		return false;

	CString strTitle;
	///////////////

	if(!strTitle.LoadString (static_cast<size_t>(nTitle)))
	{
		return false;
	}

	SetTitle(strTitle);
	return true;
}

// /////////////////////////////////////////////////////////////////////////////
// ///////////////////////////////////////////////////////////////////////////////
void CBrowseForFolder::SetFlags(const UINT ulFlags)
{
	if(m_hwnd != NULL)
		return;

	m_bi.ulFlags = ulFlags;
}

// /////////////////////////////////////////////////////////////////////////////
// ///////////////////////////////////////////////////////////////////////////////
CString CBrowseForFolder::GetSelectedFolder(void) const
{
	return m_szSelected;
}

// /////////////////////////////////////////////////////////////////////////////
// ///////////////////////////////////////////////////////////////////////////////
bool CBrowseForFolder::SelectFolder(void)
{
	bool			bRet = false;
	LPITEMIDLIST	pidl;
	///////////////////////////

	if((pidl = ::SHBrowseForFolder(&m_bi)) != NULL)
	{
		m_strPath.Empty();
		if(m_bi.ulFlags & BIF_RETURNONLYFSDIRS) // do we select simple files/dirs?
		{
			if(SUCCEEDED(::SHGetPathFromIDList(pidl, m_szSelected)))
			{
				bRet = true;
				m_strPath = m_szSelected;
			}
		}

		LPMALLOC	pMalloc;
		//////////////////

		// Retrieve a pointer to the shell's IMalloc interface

		if(SUCCEEDED(SHGetMalloc(&pMalloc)))
		{

			// free the PIDL that SHBrowseForFolder returned to us.

			pMalloc->Free(pidl);

			// release the shell's IMalloc interface

			(void) pMalloc->Release();
		}
	}

	m_hwnd = NULL;

	return bRet;
}

// /////////////////////////////////////////////////////////////////////////////
// ///////////////////////////////////////////////////////////////////////////////
void CBrowseForFolder::OnInit(void) const
{
}

// /////////////////////////////////////////////////////////////////////////////
// ///////////////////////////////////////////////////////////////////////////////
void CBrowseForFolder::OnSelChanged(const LPITEMIDLIST pidl) const
{
	(void) pidl;
}

// /////////////////////////////////////////////////////////////////////////////
// ///////////////////////////////////////////////////////////////////////////////
void CBrowseForFolder::EnableOK(const bool bEnable) const
{
	if(m_hwnd == NULL)
		return;
	(void) SendMessage(m_hwnd, BFFM_ENABLEOK, static_cast<WPARAM>(bEnable), NULL);
}

// /////////////////////////////////////////////////////////////////////////////
// ///////////////////////////////////////////////////////////////////////////////
void CBrowseForFolder::SetSelection(const LPITEMIDLIST pidl) const
{
	if(m_hwnd == NULL)
		return;
	(void) SendMessage(m_hwnd, BFFM_SETSELECTION, FALSE, reinterpret_cast < long > (pidl));
}

// /////////////////////////////////////////////////////////////////////////////
// ///////////////////////////////////////////////////////////////////////////////
void CBrowseForFolder::SetSelection(const CString& strPath) const
{
	if(m_hwnd == NULL)
		return;
	(void) SendMessage(m_hwnd, BFFM_SETSELECTION, TRUE, reinterpret_cast < long > (LPCTSTR(strPath)));
}

// /////////////////////////////////////////////////////////////////////////////
// ///////////////////////////////////////////////////////////////////////////////
void CBrowseForFolder::SetStatusText(const CString& strText) const
{
	if(m_hwnd == NULL)
		return;
	(void) SendMessage(m_hwnd, BFFM_SETSTATUSTEXT, NULL, reinterpret_cast < long > (LPCTSTR(strText)));
}

// /////////////////////////////////////////////////////////////////////////////
// ///////////////////////////////////////////////////////////////////////////////
int __stdcall CBrowseForFolder::BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData)
{
	CBrowseForFolder*	pbff = reinterpret_cast < CBrowseForFolder * > (lpData);
	//////////////////////////////////////////////////////////////////////////

	pbff->m_hwnd = hwnd;
	if(uMsg == BFFM_INITIALIZED)
		pbff->OnInit();
	else if(uMsg == BFFM_SELCHANGED)
		pbff->OnSelChanged(reinterpret_cast < LPITEMIDLIST > (lParam));
	else if(uMsg == BFFM_VALIDATEFAILED)
		pbff->EnableOK(FALSE);

	return 0;
}

⌨️ 快捷键说明

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