用 WebBrowser 加载 url, 我们有时并不希望它在新窗体中打开,因为它用选择系统默认的浏览器打开,这样就脱离了你的
WebBrowser,你也就不能在控制它了。
下面有两种解决的方法:
第一种:
在你的 DocumentCompleted 事件中添加下面的代码;
foreach (HtmlElement archor in this.wxForm.Document.Links)
{
archor.SetAttribute("target", "_self");//改变 Link 中的链接指向,让它定向到本窗体
}
foreach (HtmlElement form in this.wxForm.Document.Forms)
{
form.SetAttribute("target", "_self");//改变 Form 提交指向,让它定向到本窗体
}
然后在设置 NewWindow 事件, e.Cancel = true;
你现在可以测试一下啦.
第二种方式,比较复杂的一种需要引用一下系统 dll, "SHDocVw.dll"
public Form1()
{
InitializeComponent();
this.wxForm.Navigate("about:blank");
(this.wxForm.ActiveXInstance as SHDocVw.WebBrowser).NewWindow3 += new DWebBrowserEvents2_NewWindow3EventHandler(Form1_NewWindow3);
}
void Form1_NewWindow3(ref object ppDisp, ref bool Cancel, uint dwFlags, string bstrUrlContext, string bstrUrl)
{
Cancel = true;
this.wxForm.Navigate(bstrUrl);
}