WPF WebBrowser:我如何访问进度和新窗口事件

发布于 2024-08-18 08:34:06 字数 289 浏览 10 评论 0原文

我正在构建一个使用 WebBrowser 控件的 WPF 应用程序。
我在以下几点上苦苦挣扎:

  1. 如何从控件获取下载的当前进度。 WinForms WebBrowser 控件引发 ProgressChange 事件 - 如何使用 WPF 变体复制此功能?

  2. 如何捕获尝试在新窗口中打开的链接。 Winforms WebBrowser 再次有一个 NewWindow 事件。我可以使用它来停止 IE 的启动并在同一窗口中打开链接。是否可以使用 WPF 变体来执行此操作?

I'm building an a WPF app that uses the WebBrowser control.
I'm struggling on a couple of points:

  1. How to get the current progress of a download from the control. The WinForms WebBrowser control raises ProgressChange events - how can I replicate this feature with the WPF variant?

  2. How to capture links that are trying to open in a new window. Again the Winforms WebBrowser had a NewWindow event. I could use this to stop IE being started and to open the link in the same window. Is it possible to do this with the WPF variant?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦明 2024-08-25 08:34:06

要获取 IWebBrowser2 接口,有一个简单的方法:

//The "browser" is a object of WebBrowser class.
SHDocVw.IWebBrowser2 axBrowser = typeof(WebBrowser).GetProperty("AxIWebBrowser2", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(browser, null) as SHDocVw.IWebBrowser2;

((SHDocVw.DWebBrowserEvents_Event)axBrowser).NewWindow += OnWebBrowserNewWindow;

private void OnWebBrowserNewWindow(string URL, int Flags, string TargetFrameName, ref object PostData, string Headers, ref bool Processed)
        {
            Processed = true;
            browser.Navigate(URL);
        }

WebBrowser 类有一个属性 AxIWebBrowser2,它保存底层 COM 浏览器对象,但它是“内部”的,因此我们可以通过反射得到它。

To get IWebBrowser2 interface, there is an easy way:

//The "browser" is a object of WebBrowser class.
SHDocVw.IWebBrowser2 axBrowser = typeof(WebBrowser).GetProperty("AxIWebBrowser2", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(browser, null) as SHDocVw.IWebBrowser2;

((SHDocVw.DWebBrowserEvents_Event)axBrowser).NewWindow += OnWebBrowserNewWindow;

private void OnWebBrowserNewWindow(string URL, int Flags, string TargetFrameName, ref object PostData, string Headers, ref bool Processed)
        {
            Processed = true;
            browser.Navigate(URL);
        }

The WebBrowser class has a property AxIWebBrowser2, and it holds the underlying COM browser object, but it is "internal", so we can get it by reflection.

离不开的别离 2024-08-25 08:34:06

找到我想要的信息后,我想我会为感兴趣的人更新这个问题。

http 的底部://msdn.microsoft.com/en-us/library/system.windows.controls.webbrowser(v=VS.90).aspx 有一条评论,标题为“获取本机 IWebBrowser2”。

这显示了如何到达所需的界面并且似乎运行良好。

编辑:在 MSDN 上的评论不断消失的情况下,在此处添加链接内容。

我们的托管包装器尚未公开本机 Web 浏览器控件的许多功能。以下代码片段显示如何从 WPF WebBrowser 控件获取 IWebBrowser2 接口。这允许访问对象上未以其他方式公开暴露给控件的方法。但请注意,此代码示例仅适用于完全受信任的代码。

首先,请参阅此处的 IWebBrowser2 文档: http://msdn.microsoft.com/en -us/library/aa752127.aspx ...

要编译此代码,请将 COM 引用添加到 System32\shdocvw.dllieframe.dll(以哪个为准)您有,具体取决于 IE 版本)。

[ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("6d5140c1-7436-11ce-8034-00aa006009fa")]
internal interface IServiceProvider
{
    [return: MarshalAs(UnmanagedType.IUnknown)]
    object QueryService(ref Guid guidService, ref Guid riid);
}

static readonly Guid SID_SWebBrowserApp = 
      new Guid("0002DF05-0000-0000-C000-000000000046");

// ...

IServiceProvider serviceProvider = (IServiceProvider)myWebBrowser.Document;
Guid serviceGuid = SID_SWebBrowserApp;

Guid iid = typeof(SHDocVw.IWebBrowser2).GUID;

SHDocVw.IWebBrowser2 myWebBrowser2 = 
  (SHDocVw.IWebBrowser2) serviceProvider.QueryService(ref serviceGuid, ref iid);

然后 myWebBrowser2 就可以进行交互了。

您还可以处理本机 Web 浏览器的事件 (http:// /msdn.microsoft.com/en-us/library/aa768309(VS.85).aspx) 通过生成的托管包装器,如下所示:

SHDocVw.DWebBrowserEvents_Event wbEvents = (SHDocVw.DWebBrowserEvents_Event)myWebBrowser2;
wbEvents.NewWindow += new SHDocVw.DWebBrowserEvents_NewWindowEventHandler(OnWebBrowserNewWindow);

void OnWebBrowserNewWindow(string URL, int Flags, string TargetFrameName, ref object PostData, string Headers, ref bool Processed)
{
    // Set Processed to cancel opening of the new window.
}

Having found the information I wanted I thought I'd update this question for anyone interested.

At the bottom of http://msdn.microsoft.com/en-us/library/system.windows.controls.webbrowser(v=VS.90).aspx there is a comment entitled "Getting to the native IWebBrowser2".

This shows how to get to the required interface and seems to works well.

EDIT: Adding content of the link here as the comments at MSDN keep disappearing on me..

There is much functionality of the native Web Browser control that our managed wrapper does not yet expose. The following code snippet shows how to get the IWebBrowser2 interface from the WPF WebBrowser control. This allows access to methods on the object that are not publicly exposed in other ways for the control. Do note, however, that this code sample will only work in fully trusted code.

First, see IWebBrowser2 documentation here: http://msdn.microsoft.com/en-us/library/aa752127.aspx ...

To compile this code, add a COM Reference to System32\shdocvw.dll or ieframe.dll (whichever you have, depending on version of IE).

[ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("6d5140c1-7436-11ce-8034-00aa006009fa")]
internal interface IServiceProvider
{
    [return: MarshalAs(UnmanagedType.IUnknown)]
    object QueryService(ref Guid guidService, ref Guid riid);
}

static readonly Guid SID_SWebBrowserApp = 
      new Guid("0002DF05-0000-0000-C000-000000000046");

// ...

IServiceProvider serviceProvider = (IServiceProvider)myWebBrowser.Document;
Guid serviceGuid = SID_SWebBrowserApp;

Guid iid = typeof(SHDocVw.IWebBrowser2).GUID;

SHDocVw.IWebBrowser2 myWebBrowser2 = 
  (SHDocVw.IWebBrowser2) serviceProvider.QueryService(ref serviceGuid, ref iid);

And then myWebBrowser2 is ready for interaction.

You can also handle the native web browser's events (http://msdn.microsoft.com/en-us/library/aa768309(VS.85).aspx) through the generated managed wrappers, like this:

SHDocVw.DWebBrowserEvents_Event wbEvents = (SHDocVw.DWebBrowserEvents_Event)myWebBrowser2;
wbEvents.NewWindow += new SHDocVw.DWebBrowserEvents_NewWindowEventHandler(OnWebBrowserNewWindow);

void OnWebBrowserNewWindow(string URL, int Flags, string TargetFrameName, ref object PostData, string Headers, ref bool Processed)
{
    // Set Processed to cancel opening of the new window.
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文