【问题标题】:How can I open a link from one WebBrowser control in another WebBrowser control?如何在另一个 WebBrowser 控件中打开一个 WebBrowser 控件的链接?
【发布时间】:2013-10-03 22:15:36
【问题描述】:

我创建了一个简单的网页 html 抓取工具,用于从本地在线新闻网站检索新闻标题超链接,并在全屏宽度 x 150 像素的 WebBrowser 控件 (HeadlineLinkBrowser) 上滚动显示它们。

每次选择标题链接时,都会打开一个新的默认网络浏览器(在本例中为 Chrome)窗口并显示新闻报道。

我可以访问或修改超链接点击事件,或者我可以使用任何其他方法,以便让超链接导航到应用程序中的另一个 WebBrowser 控件 (StoryTextBrowser),而不是打开新的 Chrome 窗口?

我对 C# 开发相对较新,并且是 WPF 的完整新手..

【问题讨论】:

  • 为什么不创建一个带有 FRAMESET TAG 的 HTML 页面,包含两个框架,一个框架包含一个简单的网页 html 抓取器,另一个框架作为目标。然后在 WebBrowser 中加载包含此 Frameset 的 HTML。

标签: c# .net wpf xaml webbrowser-control


【解决方案1】:

这是一个示例 WPF 应用程序,它在 WPF WebBrowser 的另一个实例中打开所有导航链接(包括新窗口链接)。它使用底层的WebBrowser ActiveX 控件到达那里并依赖于 SHDocVw.dll COM 互操作程序集。首先生成 SHDocVw.dll:tlbimp.exe ieframe.dll,然后添加为项目的引用。请注意manualNavigation 如何用于区分用户导航和程序导航(通过webBrowser.Navigate)。

C#

using System;
using System.Windows;
using System.Windows.Controls;
using System.Reflection;

namespace WpfWebBrowser
{
    // http://stackoverflow.com/q/19170109/1768303

    public partial class MainWindow : Window
    {
        static object GetActiveXInstance(WebBrowser wb) {
            // get the underlying WebBrowser ActiveX object;
            // this code depends on SHDocVw.dll COM interop assembly,
            // generate SHDocVw.dll: "tlbimp.exe ieframe.dll",
            // and add as a reference to the project

            return wb.GetType().InvokeMember("ActiveXInstance",
                BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.NonPublic,
                null, wb, new object[] { }) as SHDocVw.WebBrowser;
        }

        public MainWindow()
        {
            InitializeComponent();

            this.Loaded += (s, e) =>
            {
                var axWbMainV1 = (SHDocVw.WebBrowser_V1)GetActiveXInstance(this.wbMaster);
                var axWbSlaveV1 = (SHDocVw.WebBrowser_V1)GetActiveXInstance(this.wbSlave);

                var manualNavigation = false;

                // Use WebBrowser_V1 events as BeforeNavigate2 doesn't work with WPF WebBrowser
                axWbMainV1.BeforeNavigate += (string URL, int Flags, string TargetFrameName, ref object PostData, string Headers, ref bool Cancel) =>
                {
                    if (!manualNavigation)
                        return;
                    Cancel = true;
                    axWbMainV1.Stop();
                    axWbSlaveV1.Navigate(URL, Flags, TargetFrameName, PostData, Headers);
                };

                axWbMainV1.FrameBeforeNavigate += (string URL, int Flags, string TargetFrameName, ref object PostData, string Headers, ref bool Cancel) =>
                {
                    if (!manualNavigation)
                        return;
                    Cancel = true;
                    axWbMainV1.Stop();
                    axWbSlaveV1.Navigate(URL, Flags, TargetFrameName, PostData, Headers);
                };

                axWbMainV1.NavigateComplete += (string URL) =>
                {
                    manualNavigation = true;
                };

                axWbMainV1.FrameNavigateComplete += (string URL) =>
                {
                    manualNavigation = true;
                };

                axWbMainV1.NewWindow += (string URL, int Flags, string TargetFrameName, ref object PostData, string Headers, ref bool Processed) =>
                {
                    if (!manualNavigation)
                        return;
                    Processed = true;
                    axWbMainV1.Stop();
                    axWbSlaveV1.Navigate(URL, Flags, String.Empty, PostData, Headers);
                };

                manualNavigation = false;
                axWbMainV1.Navigate("http://www.w3.org/");
            };
        }
    }
}

XAML

<Window x:Class="WpfWebBrowser.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Width="800" Height="600">
    <StackPanel>
        <WebBrowser Margin="4" Name="wbMaster" Height="300"/>
        <WebBrowser Margin="4" Name="wbSlave" Height="300"/>
    </StackPanel>
</Window>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-20
    • 1970-01-01
    • 2013-08-04
    • 2012-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多