Popup window webbrowser control

前端 未结 3 1735
情话喂你
情话喂你 2021-01-03 06:28

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 webbrowse

3条回答
  •  旧时难觅i
    2021-01-03 06:48

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

提交回复
热议问题