且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

web浏览器内部框架在默认浏览器中打开

更新时间:2023-09-21 23:23:22

我得出的结论是,.NET WebBrowser组件是在这方面几乎无用。我试着读 WebBrowserNavigatingEventArgs.TargetFrameName ,但它会返回iframe元素只有在HTML的名称属性文件有它。否则,它会吐出一个空字符串。非框架链接返回null本来吨更加有用。

I've come to the conclusion that the .NET WebBrowser component is almost useless in this regard. I've tried to read WebBrowserNavigatingEventArgs.TargetFrameName but it will return the name attribute of the iframe element only if the HTML document has it. Otherwise it will spit out an empty "" string. Returning null on non-frame links would have been tons more useful.

所以,我发现这样做的唯一的解决方法是使用AxWebBrowser控制,并专门听取了BeforeNavigate2事件。我没有测试过,就像我应该有,但现在看来,在 DWebBrowserEvents2_BeforeNavigate2Event 设置为 64 每一次的标志属性是用户触发。

So the only fix I've found for this is using the AxWebBrowser control, and specifically listening to the BeforeNavigate2 event. I haven't tested as much as I should have, but it appears that the "flags" property in DWebBrowserEvents2_BeforeNavigate2Event is set to 64 each time it's user triggered.

private void axWebBrowser1_BeforeNavigate2(object sender, AxSHDocVw.DWebBrowserEvents2_BeforeNavigate2Event e)
{
    // 64 means user triggered
    if ((int)e.flags == 64 && e.uRL.ToString() != "about:blank")
    {
    	e.cancel = true;
    	System.Diagnostics.Process.Start(e.uRL.ToString());
    }
}

MSDN文档说,标记是IE7 +唯一的参数,所以我不知道对IE6的机器会发生什么......

MSDN docs says that flags is an IE7+ only parameter, so I have no idea what happens on IE6 machines...

从.NET 使用Internet Explorer有关于AxWebBrowser一些真正有价值的信息。

Using Internet Explorer from .NET has some really valuable information regarding AxWebBrowser.