我怎样才能打开从一个WebBrowser控件在另一个WebBrowser控件的链接? [英] How can I open a link from one WebBrowser control in another WebBrowser control?
问题描述
我创建了一个简单的网页的HTML刮刀从本地在线新闻网站获取新闻标题链接并显示它们跨越全屏幕宽x 150像素WebBrowser控件(HeadlineLinkBrowser)滚动
I've created a simple webpage html scraper to retrieve news headline hyperlinks from a local online news site and display them scrolling across a full-screen-width x 150px WebBrowser control (HeadlineLinkBrowser).
标题链接被选中时,都会显示一个新的默认Web浏览器(在这种情况下,Chrome浏览器)窗口打开和新闻故事。
Each time a headline link is selected, a new default web browser (in this case Chrome) window opens and the news story is displayed.
我可以访问或修改超链接点击事件,或是否有任何其他的方法,我可以使用,才能有超链接导航到的另一个WebBrowser控件的(StoryTextBrowser)在应用程序中而不是打开一个新的浏览器窗口?
Can I access or modify the hyperlink click event, or is there any other approach I can use, in order to have the hyperlink navigate to another WebBrowser control (StoryTextBrowser) within the application rather than opening a new Chrome window?
我是比较新的C#开发以及一个完整的福利局与WPF ..
I'm relatively new to C# development and a complete newb with WPF..
推荐答案
下面是一个示例WPF应用程序,它会打开所有的导航链接(包括新窗口链接)在WPF的另一个实例 web浏览器
。它使用的基本 web浏览器
ActiveX控件到那里,并依赖于SHDOCVW.DLL COM互操作程序集。首先生成SHDOCVW.DLL: tlbimp.exe是ieframe.dll
,然后添加作为参考项目。请注意如何 manualNavigation
用于用户导航和导航程序来区分(通过 webBrowser.Navigate
)。
Here's a sample WPF app which opens all navigation links (including the new window links) in another instance of WPF WebBrowser
. It uses the underlying WebBrowser
ActiveX control to get there and depends on SHDocVw.dll COM interop assembly. Generate SHDocVw.dll first: tlbimp.exe ieframe.dll
, then add as a reference to the project. Note how manualNavigation
is used to differentiate between user navigation and program navigation (via 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>
这篇关于我怎样才能打开从一个WebBrowser控件在另一个WebBrowser控件的链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!