弹出窗口webbrowser控件

我正在使用webbrowser控件从网站获取一些信息。 它有一个详细信息链接,单击该链接后会打开一个弹出窗口,并在Web浏览器中显示详细信息。

如果单击webbrowser控件中的链接(按程序)打开另一个窗口并显示执行错误,我该怎么办?

但在探索者中它是有效的。 我注意到只有当我在Internet Explorer中打开主页时,详细链接才有效,否则如果我直接从Internet Explorer调用详细URL,它也会给我同样的错误。

我最近碰到了一个非常相似的情况。 在我的例子中,弹出浏览器没有共享嵌入式浏览器的会话。 我必须做的是捕获NewWindow事件并取消它,然后将预期的URL发送到嵌入式浏览器。 我需要使用ActiveX浏览器实例,因为它会为您提供尝试启动的URL。 这是我的代码:

您需要将Microsoft Internet Controls COM引用添加到项目中才能使其生效。

public partial class Form1 : Form { public Form1() { InitializeComponent(); // this assumes that you've added an instance of WebBrowser and named it webBrowser to your form SHDocVw.WebBrowser_V1 axBrowser = (SHDocVw.WebBrowser_V1)webBrowser.ActiveXInstance; // listen for new windows axBrowser.NewWindow += axBrowser_NewWindow; } void axBrowser_NewWindow(string URL, int Flags, string TargetFrameName, ref object PostData, string Headers, ref bool Processed) { // cancel the PopUp event Processed = true; // send the popup URL to the WebBrowser control webBrowser.Navigate(URL); } } 

这是动态版。 它不需要静态绑定com interop,这在windows的未来版本中总是存在问题。

  public partial class Form10 : Form { public Form10() { InitializeComponent(); webBrowser1.Navigate("about:blank"); dynamic ax = this.webBrowser1.ActiveXInstance; ax.NewWindow += new NewWindowDelegate(this.OnNewWindow); this.webBrowser1.Navigate("http://google.com"); } private delegate void NewWindowDelegate(string URL, int Flags, string TargetFrameName, ref object PostData, string Headers, ref bool Processed); private void OnNewWindow(string URL, int Flags, string TargetFrameName, ref object PostData, string Headers, ref bool Processed) { Processed = true; //your own logic } } 

改编为Middas答案……

  • 添加COM参考Microsoft Internet Controls。
  • 使用Middas Code。
  • 在form_Load中定义你的Uri,所有弹出窗口将直接改变你的winform WebBrowser。