如何创建没有 UI 的 WebBrowser 控件 (ActiveX / IWebBrowser2)?

发布于 2024-09-05 06:36:30 字数 1421 浏览 12 评论 0原文

我无法弄清楚如何使用 WebBrowser 控件而不让它在任务栏中创建一个窗口。

我直接使用 IWebBrowser2 ActiveX 控件,因为我需要使用一些高级功能,例如阻止下载 JAVA/ActiveX/图像等。这显然在 WPF 或 winforms WebBrowser 包装器中不可用(但这些包装器确实能够创建没有 UI 的控件)

这是我用于创建控件的代码:

Type webbrowsertype = Type.GetTypeFromCLSID(Iid_Clsids.CLSID_WebBrowser, true);

m_WBWebBrowser2 = (IWebBrowser2)System.Activator.CreateInstance(webbrowsertype);
m_WBWebBrowser2.Visible = false;

m_WBOleObject = (IOleObject)m_WBWebBrowser2;
int iret = m_WBOleObject.SetClientSite(this);
iret = m_WBOleObject.SetHostNames("me", string.Empty);

tagRECT rect = new tagRECT(0, 0, 0, 0);
tagMSG nullMsg = new tagMSG();
m_WBOleInPlaceObject = (IOleInPlaceObject)m_WBWebBrowser2;

//INPLACEACTIVATE the WB

iret = m_WBOleObject.DoVerb((int)OLEDOVERB.OLEIVERB_INPLACEACTIVATE, 
        ref nullMsg, this, 0, IntPtr.Zero, ref rect);

IConnectionPointContainer cpCont = (IConnectionPointContainer)m_WBWebBrowser2; 

Guid guid = typeof(DWebBrowserEvents2).GUID;
IConnectionPoint m_WBConnectionPoint = null;
cpCont.FindConnectionPoint(ref guid, out m_WBConnectionPoint);

m_WBConnectionPoint.Advise(this, out m_dwCookie);

该代码工作完美,但它在任务栏中显示了一个窗口。如果我省略 DoVerb(OLEDOVERB.OLEIVERB_INPLACEACTIVATE) 调用,则导航到网页将无法正常工作。 Navigate() 不会下载页面上的所有内容,也不会触发 DocumentComplete 事件。如果我添加 DoVerb(OLEIVERB_HIDE) ,那么我会得到与省略 DoVerb(OLEDOVERB.OLEIVERB_INPLACEACTIVATE) 调用相同的行为。

这似乎是一个非常基本的问题,但我在任何地方都找不到任何例子。

I cannot figure out how to use the WebBrowser control without having it create a window in the taskbar.

I am using the IWebBrowser2 ActiveX control directly because I need to use some of the advanced features like blocking downloading JAVA/ActiveX/images etc. That apparently is not available in the WPF or winforms WebBrowser wrappers (but these wrappers do have the ability to create the control with no UI)

Here is my code for creating the control:

Type webbrowsertype = Type.GetTypeFromCLSID(Iid_Clsids.CLSID_WebBrowser, true);

m_WBWebBrowser2 = (IWebBrowser2)System.Activator.CreateInstance(webbrowsertype);
m_WBWebBrowser2.Visible = false;

m_WBOleObject = (IOleObject)m_WBWebBrowser2;
int iret = m_WBOleObject.SetClientSite(this);
iret = m_WBOleObject.SetHostNames("me", string.Empty);

tagRECT rect = new tagRECT(0, 0, 0, 0);
tagMSG nullMsg = new tagMSG();
m_WBOleInPlaceObject = (IOleInPlaceObject)m_WBWebBrowser2;

//INPLACEACTIVATE the WB

iret = m_WBOleObject.DoVerb((int)OLEDOVERB.OLEIVERB_INPLACEACTIVATE, 
        ref nullMsg, this, 0, IntPtr.Zero, ref rect);

IConnectionPointContainer cpCont = (IConnectionPointContainer)m_WBWebBrowser2; 

Guid guid = typeof(DWebBrowserEvents2).GUID;
IConnectionPoint m_WBConnectionPoint = null;
cpCont.FindConnectionPoint(ref guid, out m_WBConnectionPoint);

m_WBConnectionPoint.Advise(this, out m_dwCookie);

This code works perfectly but it shows a window in the taskbar. If i omit the DoVerb(OLEDOVERB.OLEIVERB_INPLACEACTIVATE) call, then Navigating to a webpage is not working properly. Navigate() will not download everything on the page and it never fires the DocumentComplete event. If I add a DoVerb(OLEIVERB_HIDE) then I get the same behavior as if I omitted the DoVerb(OLEDOVERB.OLEIVERB_INPLACEACTIVATE) call.

This seems like a pretty basic question but I couldn't find any examples anywhere.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文