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

发布于 2024-07-06 09:58:06 字数 112 浏览 4 评论 0原文

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

萤火眠眠 2024-07-13 09:58:06

我只花了一个小时寻找答案,所以我想我会在这里发布结果。 您可以使用 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();
}

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.

NOTE: Code from 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();
}
半山落雨半山空 2024-07-13 09:58:06

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

InlinePopups(webBrowser1);

代码:

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

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

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.

可可 2024-07-13 09:58:06

将 shdocvw.dll 的引用添加到您的项目后
如果您没有将 actuasl 对象添加到工具箱(显示为“Microsoft Browser”)
然后在代码顶部定义对象:

Dim WithEvents Web_V1 As SHDocVw.WebBrowser_V1

After adding the reference to shdocvw.dll to your project
if you are not adding the actuasl object to your toolbox (shwos as "Microsoft Browser")
then define the object at the top of your code with:

Dim WithEvents Web_V1 As SHDocVw.WebBrowser_V1

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文