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

📄 filedlghelper.cpp

📁 自定义修改打开文件对话框
💻 CPP
字号:
////////////////////////////////////////////////////////////////
// If this code works, it was written by Paul DiLascia.
// If not, I don't know who wrote it.
// Compiles with Visual C++ 6.0 for Windows XP and probably Windows 2000 too.
//
#include "StdAfx.h"
#include "FileDlgHelper.h"

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

/////////////////
// test whether pathname is directory
//
static BOOL IsFolder(LPCTSTR pathname)
{
	struct _stat st;
	return _stat(pathname, &st)==0 && (st.st_mode & _S_IFDIR);
}

CFileDlgHelper::CFileDlgHelper()
{
}

CFileDlgHelper::~CFileDlgHelper()
{
}

//////////////////
// Initialize: subclass appropriate windows
//
void CFileDlgHelper::Init(CFileDialog* pDlg)
{
	// cubclass dialog
	ASSERT(pDlg);
	m_pDlg = pDlg; 
	m_dlghook.HookWindow(pDlg);

	// subclass dialog's owner/parent
	CWnd* pWnd = pDlg->GetParent();
	ASSERT(pWnd);
	pWnd = pWnd->GetParent();
	ASSERT(pWnd);
	m_ownerhook.Init(pWnd, pDlg);

	// hook up
	m_dlghook.m_pHelper = this;
	m_ownerhook.m_pHelper = this;
}

//////////////////
// Get name of item -- what you see in the list control
//
CString CFileDlgHelper::GetItemName(int i)
{
	return GetListCtrl()->GetItemText(i,0);
}

//////////////////
// Get display name of item in file open dialog. Flags tell how.
// SHGDN_FORPARSING gets the full path name even when user has
// checked "Hide extensions for known file types" in Explorer.
//
CString CFileDlgHelper::GetDisplayNameOf(int i, DWORD flags)
{
	CListCtrl* plc = GetListCtrl();
	ASSERT(plc);
	return GetDisplayNameOf((LPCITEMIDLIST)plc->GetItemData(i), flags);
}

// Overload to get from PIDL.
CString CFileDlgHelper::GetDisplayNameOf(LPCITEMIDLIST pidl, DWORD flags)
{
	CString path;

	// First, get PIDL of current folder by sending CDM_GETFOLDERIDLIST
	// Get length first, then allocate.
	int len = m_pDlg->GetParent()->SendMessage(CDM_GETFOLDERIDLIST, 0,NULL);
	if (len>0) {
		CComQIPtr<IMalloc> malloc;
		SHGetMalloc(&malloc);
		LPCITEMIDLIST pidlFolder = (LPCITEMIDLIST)malloc->Alloc(len);
		ASSERT(pidlFolder);
		m_pDlg->GetParent()->SendMessage(CDM_GETFOLDERIDLIST, len,
			(LPARAM)(void*)pidlFolder);

		// Now get IShellFolder for pidlFolder
		CComQIPtr<IShellFolder> ishDesk;
		SHGetDesktopFolder(&ishDesk);
		CComQIPtr<IShellFolder> ishFolder;
		HRESULT hr = ishDesk->BindToObject(pidlFolder, NULL,
			IID_IShellFolder, (void**)&ishFolder);
		if (!SUCCEEDED(hr))
			ishFolder = ishDesk;

		// finally, get the path name from pidlFolder
		path = GetDisplayNameOf(ishFolder, pidl, flags);

		malloc->Free((void*)pidlFolder);
	}
	return path;
}

// Overload to get from IShellFolder/pidl
CString CFileDlgHelper::GetDisplayNameOf(IShellFolder* ish,
	LPCITEMIDLIST pidl, DWORD flags)
{
	CString name;
	STRRET str;
	str.uType = STRRET_WSTR;
	if (SUCCEEDED(ish->GetDisplayNameOf(pidl, flags, &str))) {
		name = str.pOleStr;
	}
	return name;
}

//////////////////
// Find out if item is a folder.
//
BOOL CFileDlgHelper::IsItemFolder(int i)
{
	// Concatenate current folder name with item name to get full path name.
	// This won't work in general--the item could be a link or shell
	// object--but then IsFolder returns FALSE anyway, correctly.
	//
	CString path = m_pDlg->GetFolderPath();
	int len = path.GetLength();
	if (len==0 || path[len-1] != '\\')
		path += '\\';
	path += GetItemName(i);
	return IsFolder(path);
}

//////////////////
// Get underlying list control
//
CListCtrl* CFileDlgHelper::GetListCtrl()
{
	ASSERT(m_pDlg);
	CWnd* pWnd = m_pDlg->GetParent()->GetDlgItem(lst2);
	ASSERT(pWnd);
	CListCtrl* plc = (CListCtrl*)pWnd->GetDlgItem(1);
	ASSERT(plc);
#ifdef DEBUG
	char classname[32];
	::GetClassName(plc->m_hWnd, classname, sizeof(classname));
	ASSERT(strcmp(classname,"SysListView32")==0);
#endif
	return plc;
}

//////////////////
// virtual window proc to trap dialog messages
//
LRESULT CFileDialogHook::WindowProc(UINT msg, WPARAM wp, LPARAM lp)
{
	if (msg==WM_COMMAND || msg==WM_NOTIFY) {
		m_pHelper->m_bUpdateUI = TRUE;
	}
	return CSubclassWnd::WindowProc(msg, wp, lp);
}

void CFileDialogOwnerHook::Init(CWnd* pOwner, CFileDialog* pDlg)
{
	HookWindow(pOwner);
	m_pDlg = pDlg;
	m_hwndParent = pDlg->GetParent()->m_hWnd;
	ASSERT(m_hwndParent);
}

//////////////////
// virtual window proc to trap dialog parent messages
//
LRESULT CFileDialogOwnerHook::WindowProc(UINT msg,
	WPARAM wp, LPARAM lp)
{
	if (msg==WM_ENTERIDLE && (HWND)lp==m_hwndParent) {
		if (m_pHelper->m_bUpdateUI) {
			m_pDlg->UpdateDialogControls(m_pDlg, FALSE);
			m_pHelper->m_bUpdateUI=FALSE;
		}
	}
	return CSubclassWnd::WindowProc(msg, wp, lp);
}

⌨️ 快捷键说明

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