加个事件就可以拉如下:
private void webBrowser_NewWindow(object sender, CancelEventArgs e)
{
//打开新窗口的方式是在已有的窗口内打开
webBrowser.Url = new Uri(((WebBrowser)sender).StatusText);
e.Cancel = true;
}
不行的可以hi我, - -,
扩充WebBrowser控件,最好的方案:
1、加入Micsofft Internet Controls引用:
项目右键->添加引用->COM->Micsofft Internet Controls,加入后,其为SHDocVw。
2、扩展WebBrowser控件:
public class WebBrowser : System.Windows.Forms.WebBrowser
{
public delegate void NewWindow3EventHandler(ref bool Cancel, string bstrUrl);
//发布NewWindow3事件
public event NewWindow3EventHandler NewWindow3;
protected override void OnHandleCreated(EventArgs e)
{
base.OnHandleCreated(e);
if (this.ActiveXInstance != null)
{
var browser = this.ActiveXInstance as SHDocVw.WebBrowser;
browser.NewWindow3 += WebBrowser_NewWindow3;
}
}
private void WebBrowser_NewWindow3(ref object ppDisp, ref bool Cancel, uint dwFlags, string bstrUrlContext, string bstrUrl)
{
var handler = NewWindow3;
if (handler != null)
handler(ref Cancel, bstrUrl);
}
}
3、在其NewWindow3事件中,置Cancel = true,再Nvaigate即可:
private void browser_NewWindow3(ref bool Cancel, string bstrUrl)
{
Cancel = true;
browser.Navigate(bstrUrl);
}