.net c#弹出窗口浏览器控件

Popup window webbrowser control
2021-11-25
  •  译文(汉语)
  •  原文(英语)

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

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

但在资源管理器中它正在工作.而且我注意到详细链接只有在我在 Internet Explorer 中打开主页时才有效,否则如果我直接从 Internet Explorer 调用详细 URL,它也会给我同样的错误.

解决过程1

我最近遇到了一个非常相似的情况.就我而言,弹出式浏览器没有共享嵌入式浏览器的会话.我必须做的是捕获 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);
    }
}
解决过程2

这是动态版本.它不需要静态绑定 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
    }
}
解决过程3

精炼到 Middas 答案...

  • 添加 COM 参考 Microsoft Internet Controls.
  • 使用 Middas 代码.
  • 在 form_Load 中定义您的 Uri,您的所有弹出窗口都将直接更改您的 winform WebBrowser.

I am using a webbrowser control to get some information from a website. It has a detail link, which when clicked, opens a popup window and shows the details in the webbrowser.

How can I do these if click the link in webbrowser control (by program) open another window and showing execution error.

But in explorer it is working. And I noticed that detail link works only if I open the main page in Internet Explorer, otherwise if I call the detail URL directly from Internet Explorer, it also gives me the same error.

Solutions1

I recently ran across a very similar situation. In my case, the popup browser didn't share the session of the embedded browser. What I had to do was capture the NewWindow event and cancel it, then send the intended URL to the embedded browser. I needed to use the ActiveX browser instance because it gives you the URL that was attempting to launch. Here is my code:

You will need to add the Microsoft Internet Controls COM reference to your project for this to work.

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);
    }
}
Solutions2

this is dynamic version. it doesnt require statically bind com interop which is always problem in future versions of 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
    }
}
Solutions3

Refine to Middas answer...

  • add COM reference Microsoft Internet Controls.
  • use Middas Code.
  • in form_Load define your Uri and all your pop up will directly change your winform WebBrowser.
转载于:https://stackoverflow.com/questions/15239946/popup-window-webbrowser-control

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入