C# webBrowser 开新窗口保持Session

首先为项目添加引用 Microsoft Internet Controls

public Form1()
{
InitializeComponent();

this.webBrowser1.AllowWebBrowserDrop = false; //可在属性里设置,很重要


//在新Form中的WebBrowser中打开
(this.webBrowser1.ActiveXInstance as SHDocVw.WebBrowser).NewWindow2 += new SHDocVw.DWebBrowserEvents2_NewWindow2EventHandler(Form1_NewWindow2);

//在主WebBrowser中打开
//(this.webBrowser1.ActiveXInstance as SHDocVw.WebBrowser).NewWindow3 += new SHDocVw.DWebBrowserEvents2_NewWindow3EventHandler(Form1_NewWindow3);
}

void Form1_NewWindow2(ref object ppDisp, ref bool Cancel)
    {
      Form5 frm = new Form5(); //事先建好一个带WebBrowser的窗体
      ppDisp = frm.webBrowser1.ActiveXInstance;
      frm.Show();
    }

void Form1_NewWindow3(ref object ppDisp, ref bool Cancel, uint dwFlags, string bstrUrlContext, string bstrUrl)
{
Cancel = true;
this.webBrowser1.Navigate(bstrUrl);
}


 

你可能感兴趣的:(WebBrowser)