.net - System.Windows.Forms.WebBrowser 在同一窗口或具有同一 session 的新窗口中打开链接

标签 .net session popup browser new-window

使用 .NET WebBrowser 控件时,如何使用同一 session 在新窗口中打开链接(即,不要在服务器上启动新的 ASP.NET session ),或者如何捕获新的链接在同一 WebBrowser 控件中打开 URL 的窗口事件?

最佳答案

我只花了一个小时寻找答案,所以我想我会在这里发布结果。您可以使用SHDocVwCtl.WebBrowser_V1对象来捕获NewWindow事件。

注意:代码来自http://www.experts-exchange.com/Programming/Languages/Visual_Basic/Q_21484555.html#discussion

//-------------------------------VB.NET Version:-------------------------------

Dim WithEvents Web_V1 As SHDocVwCtl.WebBrowser_V1

Private Sub Form_Load()
    Set Web_V1 = WebBrowser1.Object
End Sub

Private Sub Web_V1_NewWindow(ByVal URL As String, ByVal Flags As Long, ByVal TargetFrameName As String, PostData As Variant, ByVal Headers As String, Processed As Boolean)
    Processed = True
    WebBrowser1.Navigate URL
End Sub


//-------------------------------C# Version-------------------------------

private SHDocVw.WebBrowser_V1 Web_V1; //Interface to expose ActiveX methods

private void Form1_Load(object sender, EventArgs e)
{
    //Setup Web_V1 interface and register event handler
    Web_V1 = (SHDocVw.WebBrowser_V1)this.webBrowser1.ActiveXInstance;
    Web_V1.NewWindow += new SHDocVw.DWebBrowserEvents_NewWindowEventHandler(Web_V1_NewWindow);
}

private void Web_V1_NewWindow(string URL, int Flags, string TargetFrameName, ref object PostData,string Headers, ref bool Processed)
{
    Processed = true; //Stop event from being processed

    //Code to open in same window
    this.webBrowser1.Navigate(URL);

    //Code to open in new window instead of same window
    //Form1 Popup = new Form1();
    //Popup.webBrowser1.Navigate(URL);
    //Popup.Show();
}

关于.net - System.Windows.Forms.WebBrowser 在同一窗口或具有同一 session 的新窗口中打开链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/175836/

相关文章:

c# - N 层应用程序设计

java - Java EE 应用程序的 session 处理

javascript - 如何防止 Google Chrome 阻止我的弹出窗口?

.net - Azure Function 不适用于参数化 API

.net - 将自动裁剪的图像库

Javascript:从cookie中读出 session ID

javascript - 使用 Meteor session 切换模板

intellij-idea 如何在快速文档弹出窗口和永久固定窗口大小(宽度和高度)上显示所有内容

c++ - 没有互联网弹出窗口 Qt 和 QML(移动设备)

c# - 有没有一种方法可以使用对象的形状而不是继承来对通用函数参数进行类型检查?