.net System.Windows.Forms.WebBrowser 在同一窗口或同一会话的新窗口中打开链接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/175836/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 10:15:11  来源:igfitidea点击:

System.Windows.Forms.WebBrowser open links in same window or new window with same session

.netsessionpopupbrowsernew-window

提问by Greg Bray

When using the .NET WebBrowser control how do you open a link in a new window using the the same session (ie.. do not start a new ASP.NET session on the server), or how do you capture the new window event to open the URL in the same WebBrowser control?

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

回答by Greg Bray

I just spent an hour looking for the answer, so I though I would post the results here. You can use the SHDocVwCtl.WebBrowser_V1 object to capture the NewWindow event.

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

NOTE: Code from http://www.experts-exchange.com/Programming/Languages/Visual_Basic/Q_21484555.html#discussion

注意:来自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();
}

回答by jvenema

Slightly cleaned up version of Greg's answer. It modifies the passed-in control's behavior rather than relying on a global variable. Usage:

稍微清理了格雷格答案的版本。它修改传入控件的行为,而不是依赖全局变量。用法:

InlinePopups(webBrowser1);

Code:

代码:

// interface to expose ActiveX methods
private SHDocVw.WebBrowser_V1 Web_V1;
private void InlinePopups(WebBrowser browser)
{
    // hooks to force new windows to open in the current instance
    Web_V1 = (SHDocVw.WebBrowser_V1)browser.ActiveXInstance;
    Web_V1.NewWindow += new SHDocVw.DWebBrowserEvents_NewWindowEventHandler((string URL, int Flags, string TargetFrameName, ref object PostData, string Headers, ref bool Processed) =>
    {
        Processed = true; // stop event from being processed

        // open in the existing window
        browser.Navigate(URL);
    });
}

Still needs the reference to %WINDIR%\system32\shdocvw.dll, of course.

当然,仍然需要对 %WINDIR%\system32\shdocvw.dll 的引用。