根据图中显示的从属关系,顺藤摸瓜,最内层的窗口“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 ); } |
|