在 CWebBrowser 控件中查找





5.00/5 (1投票)
2000年6月29日

161337
如何在 CWebBrowser 控件中显示“查找”窗口。
引言
我被分配编写的一个应用程序需要在一个 CWebBrowser
控件中显示可能超过 10,000 行的报告。 生成这些 10,000 行有时需要几个小时,因此决定在生成报告时逐行显示它会很好。 作为 COM 的新手,我在 CodeProject 消息区域中发帖提问“如何?”。 随后 Colin Davies (Colin Davies) 提供了一段代码。 在 10,000 行的报告中,用户可能需要花费很长时间才能找到他们想要的内容!
IE5 具有“编辑 - 查找(在此页上)”... CTRL+F 选项,可以搜索当前页面。 因此,我研究了在我的 CWebBrowser
控件中实现此功能,并发现对于 COM 新手来说并不像最初听起来那么容易。 无论如何,在搜索了一些令人困惑的 MSDN 文章后,我编写了一些代码来生成这段代码片段,它也可以修改为显示“查看源代码”和“选项”窗口。
源代码列表
#include <initguid.h> DEFINE_GUID(CGID_IWebBrowser,0xED016940L, 0xBD5B,0x11cf,0xBA, 0x4E,0x00, 0xC0,0x4F,0xD7,0x08,0x16); //1 : FIND, 2: VIEWSOURCE, 3 : OPTIONS DWORD nCmdID = 1; LPDISPATCH lpDispatch = NULL; LPOLECOMMANDTARGET lpOleCommandTarget = NULL; //m_htmlview is the member variable //for the CWebBrowser2 control. lpDispatch = m_htmlview.GetDocument(); ASSERT(lpDispatch); // Get an IDispatch pointer for the // IOleCommandTarget interface. lpDispatch->QueryInterface( IID_IOleCommandTarget, (void**)&lpOleCommandTarget); ASSERT(lpOleCommandTarget); lpDispatch->Release(); // Invoke the given command id for // the WebBrowser control lpOleCommandTarget->Exec( &CGID_IWebBrowser, nCmdID, 0, NULL, NULL); // Release the command target lpOleCommandTarget->Release();