WPF web浏览器:我如何获得进展,新窗口事件 [英] WPF WebBrowser: How I do access progress and new window events

查看:563
本文介绍了WPF web浏览器:我如何获得进展,新窗口事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要建一个一个WPF应用程序,使用WebBrowser控件。
我挣扎的一对夫妇点:

  1. 如何从控制下载的当前进度。在的WinForms WebBrowser控件引发ProgressChange事件 - ?我怎么可以复制此功能与WPF变异

  2. 如何捕捉那些试图在新窗口中打开链接。同样,web浏览器的WinForms了NewWindow事件。我可以用这个来阻止IE浏览器正在启动并打开在同一个窗口中的链接。是否有可能与WPF变异做到这一点?

解决方案

已经找到我想要的,我想我会更新这个问题,有兴趣的。

信息

http://msdn.microsoft.com/en-us/library/system.windows.controls.webbrowser(v=VS.90).aspx有一个题为进入当地的IWebBrowser2。

评论

这显示了如何获取到所需的接口,似乎效果很好。

修改:在这里添加链接的内容在MSDN的意见不断消失在我身上。

还有就是本地Web浏览器控件,我们的托管包装还没有公开的多的功能。下面code片段展示了如何从WPF的WebBrowser控件的IWebBrowser2 接口。这将允许访问该对象的方法,这些方法没有公开暴露在其他方面的控制。但是千万注意,这code样品将只能在完全受信任的code。

首先,看看IWebBrowser2的文档在这里: http://msdn.microsoft.com /en-us/library/aa752127.aspx ...

要编译这个code,添加COM引用 SYSTEM32 \ shdocvw.dll中 ieframe.dll (无论你有,这取决于版本的IE)。

  [ComImport,InterfaceType(ComInterfaceType.InterfaceIsIUnknown)
[的Guid(6d5140c1-7436-11ce-8034-00aa006009fa)]
内部接口的IServiceProvider
{
    [返回:的MarshalAs(UnmanagedType.IUnknown)
    对象QueryService的(REF的Guid guidService,裁判的Guid RIID);
}

静态只读的Guid SID_SWebBrowserApp =
      新的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,裁判IID);
 

然后myWebBrowser2准备的互动。

您还可以处理本地网络浏览器的事件( HTTP ://msdn.microsoft.com/en-us/library/aa768309(VS.85)的.aspx )通过生成的托管的包装,像这样的:

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

无效OnWebBrowserNewWindow(字符串URL,诠释旗,串TargetFrameName,引用对象的PostData,串标头,REF BOOL加工)
{
    //设置处理,以消除开放的新窗口。
}
 

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?

解决方案

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.
}

这篇关于WPF web浏览器:我如何获得进展,新窗口事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆