www.pudn.com > FTP¿Í»§¶Ë³ÌÐòµÄÔ´³ÌÐò.zip > SubFolder.h


// SubFolder.h : Declaration of the CSubFolder 
// Copyright © 1999 Ziff-Davis, Inc. 
// Written by Ignacio Alvarez 
 
#ifndef __SUBFOLDER_H_ 
#define __SUBFOLDER_H_ 
 
#include "resource.h"       // main symbols 
 
// CSubFolder 
class ATL_NO_VTABLE CSubFolder :  
	public CComObjectRootEx, 
	public CComCoClass, 
	public ISubFolder, 
	public IShellFolder, 
	public IPersistFolder 
{ 
public: 
	CSubFolder() 
	{ 
		Folder = NULL; 
		FolderPIDL = NULL; 
		SHGetMalloc(&ShellMalloc); 
	} 
 
	~CSubFolder() 
	{ 
		ShellMalloc->Free(FolderPIDL); 
		ShellMalloc->Release(); 
		if(Folder) 
			Folder->Release(); 
	} 
 
 
DECLARE_REGISTRY_RESOURCEID(IDR_SUBFOLDER) 
DECLARE_NOT_AGGREGATABLE(CSubFolder) 
 
DECLARE_PROTECT_FINAL_CONSTRUCT() 
 
BEGIN_COM_MAP(CSubFolder) 
	COM_INTERFACE_ENTRY(ISubFolder) 
	COM_INTERFACE_ENTRY(IShellFolder) 
	COM_INTERFACE_ENTRY(IPersistFolder) 
END_COM_MAP() 
 
// ISubFolder 
private: 
	LPSHELLFOLDER Folder; 
	LPITEMIDLIST FolderPIDL; 
	LPMALLOC ShellMalloc; 
 
public: 
 
	STDMETHOD(ParseDisplayName) ( HWND hwndOwner, 
		LPBC pbcReserved, LPOLESTR lpszDisplayName, 
		ULONG * pchEaten, LPITEMIDLIST * ppidl, ULONG *pdwAttributes) 
	{ 
		if(Folder) // Calls the ParseDisplayName method of the target folder 
			return Folder->ParseDisplayName(hwndOwner, pbcReserved, lpszDisplayName, pchEaten, ppidl, pdwAttributes); 
		ATLTRACENOTIMPL(_T("ParseDisplayName")); 
	}; 
 
	STDMETHOD(EnumObjects) (HWND hwndOwner, DWORD grfFlags, LPENUMIDLIST * ppenumIDList) 
	{ 
		*ppenumIDList = NULL; 
		if(Folder) // Calls the EnumObjects method of the target folder 
			return Folder->EnumObjects(hwndOwner, grfFlags, ppenumIDList); 
		ATLTRACENOTIMPL(_T("EnumObjects")); 
	}; 
 
	STDMETHOD(BindToObject) ( LPCITEMIDLIST pidl, LPBC pbcReserved, 
						REFIID riid, LPVOID * ppvOut) 
	{ 
		*ppvOut = NULL; 
		__try 
		{ 
			if(Folder)	// Checks if the IShellFolder interface is valid 
			{ 
				if(FolderPIDL)	// Checks if the PIDL is valid 
					if(IsEqualIID(riid, IID_IShellFolder))	// Checks if the requested interface of the object is IShellFolder 
					{ 
						CComObject  * new_object= new CComObject ; // Creates an instance of CSubFolder 
						LPITEMIDLIST FolderID; 
						int	i = sizeofPIDL(FolderPIDL);							// Gets the size of the parent PIDL 
						int j = sizeofPIDL(pidl);								// Gets the size of the object PIDL 
						FolderID = (LPITEMIDLIST) ShellMalloc->Alloc(i + j - 2);// Allocates enough space for both PIDLs 
						CopyMemory(FolderID, FolderPIDL, i);					// Copies parent PIDL 
						CopyMemory((((LPBYTE)FolderID) + i - 2), pidl, j);		// Appends object PIDL 
						new_object->Initialize(FolderID);						// Initializes the extension passing it the absolute location of the folder 
						ShellMalloc->Free(FolderID);							// Frees the PIDL 
						return new_object->QueryInterface (riid, ppvOut);		// Returns the IShellFolder interface of CSubFolder 
					} 
				return Folder->BindToObject(pidl, pbcReserved, riid, ppvOut); 
			} 
		} 
		__except(1) 
		{ 
		} 
		ATLTRACENOTIMPL(_T("BindToObject")); 
	}; 
 
	STDMETHOD(BindToStorage) ( LPCITEMIDLIST pidl, LPBC pbcReserved, 
						REFIID riid, LPVOID * ppvObj) 
	{ 
		*ppvObj = NULL; 
		if(Folder) // Calls the BindToStorage method of the target folder 
			return Folder->BindToStorage(pidl, pbcReserved, riid, ppvObj); 
		ATLTRACENOTIMPL(_T("BindToStorage")); 
	}; 
 
	STDMETHOD(CompareIDs) ( LPARAM lParam, LPCITEMIDLIST pidl1, LPCITEMIDLIST pidl2) 
	{ 
		if(Folder) // Calls the CompareIDs method of the target folder 
			return Folder->CompareIDs(lParam, pidl1, pidl2); 
		ATLTRACENOTIMPL(_T("CompareIDs")); 
	}; 
 
	STDMETHOD(CreateViewObject) ( HWND hwndOwner, REFIID riid, LPVOID * ppvOut) 
	{ 
		if(Folder) // Calls the CreateViewObject method of the target folder 
			return Folder->CreateViewObject(hwndOwner, riid, ppvOut); 
		ATLTRACENOTIMPL(_T("CreateViewObject")); 
	}; 
 
	STDMETHOD(GetAttributesOf) ( UINT cidl, LPCITEMIDLIST * apidl, 
								ULONG * rgfInOut) 
	{ 
		if(Folder) // Calls the GetAttributesOf method of the target folder 
			return Folder->GetAttributesOf(cidl, apidl, rgfInOut); 
		ATLTRACENOTIMPL(_T("GetAttributesOf")); 
	}; 
 
	STDMETHOD(GetUIObjectOf) ( HWND hwndOwner, UINT cidl, LPCITEMIDLIST * apidl, 
					REFIID riid, UINT * prgfInOut, LPVOID * ppvOut) 
	{ 
		*ppvOut = NULL; 
		if(Folder) // Calls the GetUIObjectOf method of the target folder 
			return Folder->GetUIObjectOf(hwndOwner, cidl, apidl, riid, prgfInOut, ppvOut); 
		ATLTRACENOTIMPL(_T("GetUIObjectOf")); 
	}; 
 
	STDMETHOD(GetDisplayNameOf) ( LPCITEMIDLIST pidl, DWORD uFlags, LPSTRRET lpName) 
	{ 
		if(Folder) // Calls the GetDisplayNameOf method of the target folder 
			return Folder->GetDisplayNameOf(pidl, uFlags, lpName); 
		ATLTRACENOTIMPL(_T("GetDisplayNameOf")); 
	}; 
 
	STDMETHOD(SetNameOf) ( HWND hwndOwner, LPCITEMIDLIST pidl, 
					LPCOLESTR lpszName, DWORD uFlags, 
					LPITEMIDLIST * ppidlOut) 
	{ 
		if(Folder) // Calls the SetNameOf method of the target folder 
			return Folder->SetNameOf(hwndOwner, pidl, lpszName, uFlags, ppidlOut); 
		ATLTRACENOTIMPL(_T("SetNameOf")); 
	}; 
 
	STDMETHOD(GetClassID)(LPCLSID lpClassID) 
	{ 
		ATLTRACENOTIMPL(_T("GetClassID")); 
	}; 
 
	// Implementation of IPersistFolder::Initialize() 
	// This function is called every time a subfolder needs to be opened 
	// Here, the IShellFolder interface of the target folder is retrieved 
	STDMETHOD(Initialize)(LPCITEMIDLIST pidl) 
	{ 
		LPSHELLFOLDER Desktop; 
		__try 
		{ 
			BOOL Visible = FALSE; 
			EnumWindows(EnumWProc, (long)&Visible); 
			if(Visible)	// If there is a visible BaseBar class window 
			{ 
				int i = sizeofPIDL(pidl); 
				FolderPIDL = (LPITEMIDLIST) ShellMalloc->Alloc(i); 
				CopyMemory(FolderPIDL, pidl, i);// Copies the PIDL of the folder 
				SHGetDesktopFolder(&Desktop); 
				Desktop->BindToObject(pidl, NULL, IID_IShellFolder, (void**)&Folder); // Retrieves the IShellFolder interface of the target folder and stores it in the Folder variable 
				Desktop->Release(); 
			} 
		} 
		__except(1) 
		{ 
			Folder = NULL; 
		} 
		return NOERROR; 
	}; 
 
 
 
}; 
 
#endif //__SUBFOLDER_H_