日期:2008-06-07  浏览次数:20537 次

关键字:Add To Favorite, Import/Export Wizard, Shell DocObject View, Internet Explorer_Server


1、概述
除了“整理收藏夹”和“添加到收藏夹”对话框外,还有其它一些对话框是我们希望直接通过WebBrowser调用的,比如“导入/导出”对话框,用一般的方法很难调用。IShellUIHelper尽管提供了ImportExportFavorites方法,但结果只是显示一个选择文件的对话框,且只能导入/导出收藏夹而不能对Cookies操作。




2、契机
MSDN中有一篇叫“WebBrowser Customization”的文章,其中介绍了通过IDocHostUIHandler.ShowContextMenu方法自定义WebBrowser上下文菜单的方法。其原理是从“shdoclc.dll”的资源中创建菜单,作一些修改之后用TrackPopupMenu函数(注意在标志中包含TPM_RETURNCMD)将菜单弹出,然后把返回的Command ID发送给“Internet Explorer_Server”窗口进行处理。
......
// 显示菜单
int iSelection = ::TrackPopupMenu(hMenu,
? TPM_LEFTALIGN | TPM_RIGHTBUTTON | TPM_RETURNCMD,
? ppt->x,
? ppt->y,
? 0,
? hwnd,
? (RECT*)NULL);
// 发送Command ID到外壳窗口
LRESULT lr = ::SendMessage(hwnd, WM_COMMAND, iSelection, NULL);
......

好,如果找到所有上下文菜单的Command ID,不就可以随时调用了?确实是这样的。


3、实现
用eXeScope之类应用程序资源探索器打开“shdoclc.dll”便可以在菜单资源下找到上下文菜单的设计,如下图:



我们要做的,就是将这些ID发送到“Internet Explorer_Server”窗口进行处理。问题是WebBrowser其实是一个OLE容器,我们使用的CHTMLView又是更外层的封装,他们的m_hWnd成员变量并不是IE窗口的句柄,如何找到我们需要的句柄呢?请看下面的图:




根据图中显示的从属关系,顺藤摸瓜,最内层的窗口“Internet Explorer_Server”的句柄就是我们需要的东西。为了简化问题,我这里使用了来自MSDN Magazine资深专栏撰稿人Paul Dilascia的CFindWnd类,非常好用。

////////////////////////////////////////////////////////////////
// MSDN Magazine -- August 2003
// If this code works, it was written by Paul DiLascia.
// If not, I don't know who wrote it.
// Compiles with Visual Studio .NET on Windows XP. Tab size=3.
//
// ---
// This class encapsulates the process of finding a window with a given class name
// as a descendant of a given window. To use it, instantiate like so:
//
// CFindWnd fw(hwndParent,classname);
//
// fw.m_hWnd will be the HWND of the desired window, if found.
//
class CFindWnd {
private:
? //////////////////
? // This private function is used with EnumChildWindows to find the child
? // with a given class name. Returns FALSE if found (to stop enumerating).
? //
? static BOOL CALLBACK FindChildClassHwnd(HWND hwndParent, LPARAM lParam) {
??? CFindWnd *pfw = (CFindWnd*)lParam;
??? HWND hwnd = FindWindowEx(hwndParent, NULL, pfw->m_classname, NULL);
??? if (hwnd) {
????? pfw->m_hWnd = hwnd; // found: save it
????? return FALSE; // stop enumerating
??? }
??? EnumChildWindows(hwndParent, FindChildClassHwnd, lParam); // recurse
??? return TRUE; // keep looking
}
public:
? LPCSTR m_classname; // class name to look for
? HWND m_hWnd; // HWND if found
? // ctor does the work--just instantiate and go
? CFindWnd(HWND hwndParent, LPCSTR classname)
??? : m_hWnd(NULL), m_classname(classname)
? {
??? FindChildClassHwnd(hwndParent, (LPARAM)this);
? }
};

再写一个函数InvokeIEServerCommand,调用就很方便了,《Internet Explorer 编程简述(四)“添加到收藏夹”对话框》中最后给出的方法就是从这里来的。

void CMyHTMLView::InvokeIEServerCommand(int nID)
{
? CFindWnd FindIEWnd( m_wndBrowser.m_hWnd, "Internet Explorer_Server");
? ::SendMessage( FindIEWnd.m_hWnd, WM_COMMAND, MAKEWPARAM(LOword(nID), 0x0), 0 );
}

void CMyHTMLView::OnFavAddtofav()
{
? InvokeIEServerCommand(ID_IE_CONTEXTMENU_ADDFAV);//调用“添加到收藏夹”对话框
}


4、Command IDs
对所有的Command ID逐一尝试后我们发现:
1)不是所有的Command ID都可以用上面的方法调用;
2)不是所有的Command ID都是由“Internet Explorer_Server”窗口处理;
3)有一些Command ID是由上一级窗口“Shell DocObject View”处理。
所以我们还需要写一个函数。

void CMyHTMLView::InvokeShellDocObjCommand(int nID)
{
? CFindWnd FindIEWnd( m_wndBrowser.m_hWnd, "Shell DocObject View");
? ::SendMessage( FindIEWnd.m_hWnd, WM