弹出窗口的浏览器控件
本文关键字:浏览器 控件 窗口 | 更新日期: 2023-09-27 18:02:33
我正在使用浏览器控件从网站获取一些信息。它有一个细节链接,当点击时,打开一个弹出窗口,并在浏览器中显示详细信息。
如果点击浏览器控件中的链接(通过程序)打开另一个窗口并显示执行错误,我该如何做这些。
但是在浏览器中它是工作的。我注意到detail链接只有当我在ie中打开主页时才能工作,否则如果我直接从ie中调用detail 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互操作,这在未来的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
}
}
Refine to midas answer…
- 添加COM参考Microsoft Internet Controls.
- 使用midas Code
- 在form_Load中定义你的Uri,你所有的弹出窗口将直接改变你的winform WebBrowser。