下面是代码:
IShellFolder* pShellFolderDesktop = NULL;
hr = SHGetDesktopFolder(&pShellFolderDesktop);
LPITEMIDLIST pidl = ILCreateFromPath(lpszPath);
IShellFolder* pShellFolder = NULL;
// pidl指出要浏览的folder路径
pShellFolderDesktop->BindToObject(pidl, NULL, IID_PPV_ARGS(&pShellFolder));
// hWndParent是自己创建的一个窗口,folder中的内容将会在该窗口中显示
pShellFolder->CreateViewObject(hWndParent, IID_PPV_ARGS(&pShellView->m_piView);
// 创建shell相关窗口,其实是ShellDll_DefView及子窗口SysListView(SysListView可能也有子窗口),该folder中的内容
// 其实就是在SysListView中显示的,而ShellDll_DefView会作为hWndParent的子窗口。
// 注意该函数的第3个参数,一定要是IShellBrowser的一个实例,而IShellBrowser又是一个纯虚类,不能直接创建该类的对象,
//只能先实现,然后再创建,CShellBrowser就是从该类IShellBrowser派生出来的,只需要实现相关接口就行了。
CShellBrowser* psb = new CShellBrowser;
pShellView->m_piView->CreateViewWindow(NULL, &fs, static_cast<IShellBrowser*>(psb), const_cast<LPRECT>(&rect), &psb->m_hWnd);
实际上,CreateViewWindow的第3个参数主要是为IShellView指定一个框架,可以这样理解,为视图指定一个框架,也可以这样理解,视图必须
在框架中显示,IShellBrowser的作用就像是浏览器窗口,而IShellView就像是网页。
附CShellBrowser:
// IServiceProvider可以没有
class CShellBrowser : public IShellBrowser, public IServiceProvider
{
public:
CShellBrowser(HINSTANCE hInstance);
~CShellBrowser(void);
// IUnknown methods
STDMETHOD(QueryInterface)(REFIID riid, void **ppvObject);
STDMETHOD_(ULONG, AddRef)(void);
STDMETHOD_(ULONG, Release)(void);
// IServiceProvider methods
STDMETHOD(QueryService)(REFGUID guidService, REFIID riid, void **ppvObject);
// IOleWindow methods
STDMETHOD(GetWindow)(HWND *phwnd);
STDMETHOD(ContextSensitiveHelp)(BOOL fEnterMode);
// IShellBrowser methods
STDMETHOD(BrowseObject)(PCUIDLIST_RELATIVE pidl, UINT wFlags);
STDMETHOD(EnableModelessSB)(BOOL fEnable);
STDMETHOD(GetControlWindow)(UINT id, HWND *lphwnd);
STDMETHOD(GetViewStateStream)(DWORD grfMode, IStream **ppStrm);
STDMETHOD(InsertMenusSB)(HMENU hmenuShared, LPOLEMENUGROUPWIDTHS lpMenuWidths);
STDMETHOD(OnViewWindowActive)(IShellView *ppshv);
STDMETHOD(QueryActiveShellView)(IShellView **ppshv);
STDMETHOD(RemoveMenusSB)(HMENU hmenuShared);
STDMETHOD(SendControlMsg)(UINT id, UINT uMsg, WPARAM wParam, LPARAM lParam, LRESULT *pret);
STDMETHOD(SetMenuSB)(HMENU hmenuShared, HOLEMENU holemenuRes, HWND hwndActiveObject);
STDMETHOD(SetStatusTextSB)(LPCWSTR lpszStatusText);
STDMETHOD(SetToolbarItems)(LPTBBUTTONSB lpButtons, UINT nButtons, UINT uFlags);
STDMETHOD(TranslateAcceleratorSB)(LPMSG lpmsg, WORD wID);
...
private:
ULONG m_ulRef;
}
CShellBrowser::CShellBrowser(HINSTANCE hInstance)
: m_ulRef(0)
, m_hInstance(hInstance)
CShellBrowser::~CShellBrowser(void)
// IUnknown methods
STDMETHODIMP CShellBrowser::QueryInterface(REFIID riid, void **ppvObject)
if (ppvObject == NULL)
return E_POINTER;
*ppvObject = NULL;
if (IsEqualIID(riid, IID_IUnknown))
*ppvObject = static_cast<IUnknown*>(static_cast<IShellBrowser*>(this));
else if (IsEqualIID(riid, IID_IOleWindow))
*ppvObject = static_cast<IOleWindow*>(this);
else if (IsEqualIID(riid, IID_IShellBrowser))
*ppvObject = static_cast<IShellBrowser*>(this);
else
return E_NOINTERFACE;
static_cast<IUnknown*>(*ppvObject)->AddRef();
return S_OK;
STDMETHODIMP_(ULONG) CShellBrowser::AddRef(void)
return ++m_ulRef;
STDMETHODIMP_(ULONG) CShellBrowser::Release(void)
if (--m_ulRef == 0UL)
{
m_pObject = NULL;
delete this;
return 0UL;
}
return m_ulRef;
// IServiceProvider methods
STDMETHODIMP CShellBrowser::QueryService(REFGUID guidService, REFIID riid, void **ppvObject)
if (IsEqualGUID(guidService, SID_SShellBrowser))
return QueryInterface(riid, ppvObject);
return E_NOINTERFACE;
// IOleWindow methods
STDMETHODIMP CShellBrowser::GetWindow(HWND *phwnd)
*phwnd = GetSafeHwnd();
STDMETHODIMP CShellBrowser::ContextSensitiveHelp(BOOL fEnterMode)
return E_NOTIMPL;
// IShellBrowser methods
STDMETHODIMP CShellBrowser::BrowseObject(PCUIDLIST_RELATIVE pidl, UINT wFlags)
STDMETHODIMP CShellBrowser::EnableModelessSB(BOOL fEnable)
STDMETHODIMP CShellBrowser::GetControlWindow(UINT id, HWND *lphwnd)
*lphwnd = NULL;
STDMETHODIMP CShellBrowser::GetViewStateStream(DWORD grfMode, IStream **ppStrm)
STDMETHODIMP CShellBrowser::InsertMenusSB(HMENU hmenuShared, LPOLEMENUGROUPWIDTHS lpMenuWidths)
STDMETHODIMP CShellBrowser::OnViewWindowActive(IShellView *ppshv)
STDMETHODIMP CShellBrowser::QueryActiveShellView(IShellView **ppshv)
STDMETHODIMP CShellBrowser::RemoveMenusSB(HMENU hmenuShared)
STDMETHODIMP CShellBrowser::SendControlMsg(UINT id, UINT uMsg, WPARAM wParam, LPARAM lParam, LRESULT *pret)
STDMETHODIMP CShellBrowser::SetMenuSB(HMENU hmenuShared, HOLEMENU holemenuRes, HWND hwndActiveObject)
STDMETHODIMP CShellBrowser::SetStatusTextSB(LPCWSTR lpszStatusText)
STDMETHODIMP CShellBrowser::SetToolbarItems(LPTBBUTTONSB lpButtons, UINT nButtons, UINT uFlags)
STDMETHODIMP CShellBrowser::TranslateAcceleratorSB(LPMSG lpmsg, WORD wID)
}
本文转自jetyi51CTO博客,原文链接:http://blog.51cto.com/jetyi/560745 ,如需转载请自行联系原作者