Popup window webbrowser control

2020-06-23 09:18发布

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.

3条回答
家丑人穷心不美
2楼-- · 2020-06-23 09:26

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);
    }
}
查看更多
叛逆
3楼-- · 2020-06-23 09:30

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
    }
}
查看更多
做自己的国王
4楼-- · 2020-06-23 09:38

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.
查看更多
登录 后发表回答