Syl*_*rag 419 .net c# webbrowser-control winforms
有没有相对简单的方法将现代浏览器插入.NET应用程序?
据我了解,该WebBrowser
控制是一个包装IE,这不会是一个问题,但它看起来是一个很老版本的IE浏览器,以及所有在CSS螺丝起坐方面需要,潜在的安全风险(如果渲染引擎没有打补丁,我真的可以期待修复多个缓冲区溢出问题吗?)和其他问题.
我正在使用Visual Studio C#(快速版 - 它在这里有什么不同吗?)
我想在我的应用程序中集成一个好的Web浏览器.在某些情况下,我只是使用它来处理用户注册过程,与我的网站的一些功能和该订单的其他东西的接口,但我有另一个应用程序,需要更多错误...控制.
我需要:
我正在考虑Chrome,因为它属于BSD许可证,但我会对最新版本的IE感到满意.
尽可能地,我想保持简单.最好的情况是,如果一个人可以修补现有的WebBrowser
控制,这已经占我所需要的约70%,但我不认为这是可能的.
我找到了一个用于Mozilla的activeX控件(http://www.iol.ie/~locka/mozilla/control.htm),但看起来它是一个旧版本,所以它不一定是一个改进.
我愿意接受建议
chi*_*tom 166
结帐CefSharp .Net绑定,这个项目我开始了一段时间,谢天谢地被社区所接受,变成了一些美妙的东西.
该项目包含了Chromium Embedded Framework,并已用于许多重大项目,包括Rdio的Windows客户端,Facebook Messenger for Windows和Github for Windows.
它具有WPF和Winforms的浏览器控件,并具有大量功能和扩展点.基于Chromium,它的速度也非常快.
从NuGet抓住它:Install-Package CefSharp.Wpf
或Install-Package CefSharp.WinForms
查看示例并提出您的想法/反馈/请求:https://github.com/cefsharp/CefSharp
BSD许可
Chr*_*s S 83
如果你不知道,Chrome会使用(一个分支)Webkit,这也是Safari使用的.这里有几个相同的问题:
webkit一个不是很好,因为其他答案说明,一个版本不再有效(谷歌代码一个),而Mono一个是实验性的.这会是很好,如果有人做出的努力,使它一个体面的.NET包装,但它不是任何人似乎想要做的事 - 这是奇怪,因为它现在有了HTML5和许多其他功能的支持,该IE(8 )引擎缺乏.
更新(2014年)
有一个新的双许可项目,允许您将Chrome嵌入到名为Awesomium的.NET应用程序中.它附带了一个.NET api,但需要相当多的黑客进行渲染(示例将浏览器窗口绘制到缓冲区,将缓冲区绘制为图像并刷新计时器).
我认为这是Origin在战地3中使用的浏览器.
更新(2016)
现在有DotnetBrowser,Awesomium的商业替代品.它基于Chromium.
for*_*atc 62
我几天前一直在测试C#Web浏览器组件的替代品,这是我的列表:
1.使用较新的IE版本8,9:
优点:
缺点:
这不需要太多工作,你可以获得一些HTML5和CSS3支持虽然IE9缺乏一些最好的CSS3和HTML5功能.但我相信你可以让IE10以同样的方式运行.问题是目标系统必须安装IE10,并且由于仍然在Windows 7上进行预览,我建议不要使用它.
OpenWebKitSharp是基于WebKit.NET 0.5项目的webkit引擎的.net包装器.WebKit是Chrome/Safari使用的布局引擎
优点:
缺点:
OpenWebKit虽然很多功能还没有实现,但是很好用,我在使用visual studio时遇到了一些问题,它在这里抛出空对象引用,然后在设计模式下,有一些js问题.每个人使用它几乎会立即注意到js警报什么都不做.像mouseup,mousedown等等事件不起作用,js拖放是有缺陷的等等..
我在安装它时遇到了一些困难,因为它需要安装VC可再发行组件的特定版本,所以在异常后我查看了事件日志,发现了VC的版本并安装了它.
3. GeckoFX
优点:
缺点:
GeckoFX是一个跨平台的Webrowser控件,用于嵌入到WinForms应用程序中.这可以在Windows上使用.NET,在Linux上使用mono.Gecko是Firefox使用的布局引擎.
我发现GeckoFX没有积极开发的信息很少,这当然不是真的,当然它总是落后于Firefox的一两个版本,但这是正常的,我对活动和控件本身印象非常深刻.它完成了我需要的一切,但是我需要一些时间才能让它运行,这是一个让它运行的小教程:
如果您真的必须使用Chrome,请查看这款名为Awesomium的产品,它可以免费用于非商业项目,但许可证是几千美元用于商业用途.
Ash*_*ani 38
我遇到了同样的问题,WebBrowser正在使用旧版本的IE,通过一些谷歌搜索我遇到了以下代码,它改变了注册表并使WebBrowser使用最新的IE版本:
public enum BrowserEmulationVersion
{
Default = 0,
Version7 = 7000,
Version8 = 8000,
Version8Standards = 8888,
Version9 = 9000,
Version9Standards = 9999,
Version10 = 10000,
Version10Standards = 10001,
Version11 = 11000,
Version11Edge = 11001
}
public static class WBEmulator
{
private const string InternetExplorerRootKey = @"Software\Microsoft\Internet Explorer";
public static int GetInternetExplorerMajorVersion()
{
int result;
result = 0;
try
{
RegistryKey key;
key = Registry.LocalMachine.OpenSubKey(InternetExplorerRootKey);
if (key != null)
{
object value;
value = key.GetValue("svcVersion", null) ?? key.GetValue("Version", null);
if (value != null)
{
string version;
int separator;
version = value.ToString();
separator = version.IndexOf('.');
if (separator != -1)
{
int.TryParse(version.Substring(0, separator), out result);
}
}
}
}
catch (SecurityException)
{
// The user does not have the permissions required to read from the registry key.
}
catch (UnauthorizedAccessException)
{
// The user does not have the necessary registry rights.
}
return result;
}
private const string BrowserEmulationKey = InternetExplorerRootKey + @"\Main\FeatureControl\FEATURE_BROWSER_EMULATION";
public static BrowserEmulationVersion GetBrowserEmulationVersion()
{
BrowserEmulationVersion result;
result = BrowserEmulationVersion.Default;
try
{
RegistryKey key;
key = Registry.CurrentUser.OpenSubKey(BrowserEmulationKey, true);
if (key != null)
{
string programName;
object value;
programName = Path.GetFileName(Environment.GetCommandLineArgs()[0]);
value = key.GetValue(programName, null);
if (value != null)
{
result = (BrowserEmulationVersion)Convert.ToInt32(value);
}
}
}
catch (SecurityException)
{
// The user does not have the permissions required to read from the registry key.
}
catch (UnauthorizedAccessException)
{
// The user does not have the necessary registry rights.
}
return result;
}
public static bool SetBrowserEmulationVersion(BrowserEmulationVersion browserEmulationVersion)
{
bool result;
result = false;
try
{
RegistryKey key;
key = Registry.CurrentUser.OpenSubKey(BrowserEmulationKey, true);
if (key != null)
{
string programName;
programName = Path.GetFileName(Environment.GetCommandLineArgs()[0]);
if (browserEmulationVersion != BrowserEmulationVersion.Default)
{
// if it's a valid value, update or create the value
key.SetValue(programName, (int)browserEmulationVersion, RegistryValueKind.DWord);
}
else
{
// otherwise, remove the existing value
key.DeleteValue(programName, false);
}
result = true;
}
}
catch (SecurityException)
{
// The user does not have the permissions required to read from the registry key.
}
catch (UnauthorizedAccessException)
{
// The user does not have the necessary registry rights.
}
return result;
}
public static bool SetBrowserEmulationVersion()
{
int ieVersion;
BrowserEmulationVersion emulationCode;
ieVersion = GetInternetExplorerMajorVersion();
if (ieVersion >= 11)
{
emulationCode = BrowserEmulationVersion.Version11;
}
else
{
switch (ieVersion)
{
case 10:
emulationCode = BrowserEmulationVersion.Version10;
break;
case 9:
emulationCode = BrowserEmulationVersion.Version9;
break;
case 8:
emulationCode = BrowserEmulationVersion.Version8;
break;
default:
emulationCode = BrowserEmulationVersion.Version7;
break;
}
}
return SetBrowserEmulationVersion(emulationCode);
}
public static bool IsBrowserEmulationSet()
{
return GetBrowserEmulationVersion() != BrowserEmulationVersion.Default;
}
}
Run Code Online (Sandbox Code Playgroud)
您只需要创建一个类并将此代码放入其中,然后在程序启动时运行以下代码:
if (!WBEmulator.IsBrowserEmulationSet())
{
WBEmulator.SetBrowserEmulationVersion();
}
Run Code Online (Sandbox Code Playgroud)
VB.NET:
Imports Microsoft.Win32
Imports System
Imports System.Collections.Generic
Imports System.IO
Imports System.Linq
Imports System.Security
Imports System.Text
Imports System.Threading.Tasks
Public Enum BrowserEmulationVersion
[Default] = 0
Version7 = 7000
Version8 = 8000
Version8Standards = 8888
Version9 = 9000
Version9Standards = 9999
Version10 = 10000
Version10Standards = 10001
Version11 = 11000
Version11Edge = 11001
End Enum
Public Class WBEmulator
Private Const InternetExplorerRootKey As String = "Software\Microsoft\Internet Explorer"
Public Shared Function GetInternetExplorerMajorVersion() As Integer
Dim result As Integer
result = 0
Try
Dim key As RegistryKey
key = Registry.LocalMachine.OpenSubKey(InternetExplorerRootKey)
If key IsNot Nothing Then
Dim value As Object = If(key.GetValue("svcVersion", Nothing), key.GetValue("Version", Nothing))
Dim Version As String
Dim separator As Integer
Version = value.ToString()
separator = Version.IndexOf(".")
If separator <> -1 Then
Integer.TryParse(Version.Substring(0, separator), result)
End If
End If
Catch ex As SecurityException
'The user does Not have the permissions required to read from the registry key.
Catch ex As UnauthorizedAccessException
'The user does Not have the necessary registry rights.
Catch
End Try
GetInternetExplorerMajorVersion = result
End Function
Private Const BrowserEmulationKey = InternetExplorerRootKey + "\Main\FeatureControl\FEATURE_BROWSER_EMULATION"
Public Shared Function GetBrowserEmulationVersion() As BrowserEmulationVersion
Dim result As BrowserEmulationVersion
result = BrowserEmulationVersion.Default
Try
Dim key As RegistryKey = Registry.CurrentUser.OpenSubKey(BrowserEmulationKey, True)
If key IsNot Nothing Then
Dim programName As String
Dim value As Object
programName = Path.GetFileName(Environment.GetCommandLineArgs()(0))
value = key.GetValue(programName, Nothing)
If value IsNot Nothing Then
result = CType(Convert.ToInt32(value), BrowserEmulationVersion)
End If
End If
Catch ex As SecurityException
'The user does Not have the permissions required to read from the registry key.
Catch ex As UnauthorizedAccessException
'The user does Not have the necessary registry rights.
Catch
End Try
GetBrowserEmulationVersion = result
End Function
Public Shared Function SetBrowserEmulationVersion(BEVersion As BrowserEmulationVersion) As Boolean
Dim result As Boolean = False
Try
Dim key As RegistryKey = Registry.CurrentUser.OpenSubKey(BrowserEmulationKey, True)
If key IsNot Nothing Then
Dim programName As String = Path.GetFileName(Environment.GetCommandLineArgs()(0))
If BEVersion <> BrowserEmulationVersion.Default Then
'if it's a valid value, update or create the value
key.SetValue(programName, CType(BEVersion, Integer), RegistryValueKind.DWord)
Else
'otherwise, remove the existing value
key.DeleteValue(programName, False)
End If
result = True
End If
Catch ex As SecurityException
' The user does Not have the permissions required to read from the registry key.
Catch ex As UnauthorizedAccessException
' The user does Not have the necessary registry rights.
End Try
SetBrowserEmulationVersion = result
End Function
Public Shared Function SetBrowserEmulationVersion() As Boolean
Dim ieVersion As Integer
Dim emulationCode As BrowserEmulationVersion
ieVersion = GetInternetExplorerMajorVersion()
If ieVersion >= 11 Then
emulationCode = BrowserEmulationVersion.Version11
Else
Select Case ieVersion
Case 10
emulationCode = BrowserEmulationVersion.Version10
Case 9
emulationCode = BrowserEmulationVersion.Version9
Case 8
emulationCode = BrowserEmulationVersion.Version8
Case Else
emulationCode = BrowserEmulationVersion.Version7
End Select
End If
SetBrowserEmulationVersion = SetBrowserEmulationVersion(emulationCode)
End Function
Public Shared Function IsBrowserEmulationSet() As Boolean
IsBrowserEmulationSet = GetBrowserEmulationVersion() <> BrowserEmulationVersion.Default
End Function
End Class
Run Code Online (Sandbox Code Playgroud)
您可以像以下一样使用它:
If Not WBEmulator.IsBrowserEmulationSet() Then
WBEmulator.SetBrowserEmulationVersion()
End If
Run Code Online (Sandbox Code Playgroud)
小智 26
您可以使用注册表为webbrowser控件设置IE版本.转到:HKLM\SOFTWARE \微软\的Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION,并添加"yourApplicationName.exe"与browser_emulation的价值要查看browser_emulation的价值,参考链接:http://msdn.microsoft.com/en-us /library/ee330730%28VS.85%29.aspx#browser_emulation
小智 15
我知道这不是一个'替换'WebBrowser控件,但我在显示一个使用BootStrap 3+进行布局等的页面时遇到了一些糟糕的渲染问题,然后我发现了一个帖子,建议我使用以下内容.显然它是特定于IE并告诉它使用客户端机器上发现的最新变体进行渲染(所以它不会使用IE7,因为我认为是默认的)
所以就这样说:
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
Run Code Online (Sandbox Code Playgroud)
位于文档头部的某个位置.
如果它当然不是你的文档,那么这显然无济于事(虽然我个人认为如果你正在阅读不是由你自己通过webbrowser控件创建的页面,那么它是一个安全漏洞 - 为什么不只是使用webbrowser!)
祝好运.
Vla*_*mir 14
看看DotNetBrowser库.它提供了基于Chromium的WPF和WinForms浏览器控件,它们很容易嵌入到.NET应用程序中.它支持所有现代Web标准,包括HTML5,CSS3和JavaScript.呈现的页面与Google Chrome中的内容完全相同.
该库继承了Chromium的多进程架构 - 每个Web页面都在一个单独的Chromium进程中呈现,即使在插件崩溃或网页上发生任何其他意外错误之后,应用程序仍将继续工作.
以下是DotNetBrowser提供的一些其他有用功能:可以监听加载事件,处理网络活动,配置代理,模拟用户操作,使用cookie,访问和修改DOM,监听DOM事件,从.NET调用JavaScript反之亦然,使用网络摄像头和麦克风的网页上,建立了基于的WebRTC通信,和更多.
有关更多详细信息,请查看API参考.
下面的代码片段演示了如何创建BrowserView,将其嵌入到Form中,以及如何加载URL:
using System.Windows.Forms;
using DotNetBrowser;
using DotNetBrowser.WinForms;
namespace WinForms.DotNetBrowser
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
BrowserView browserView = new WinFormsBrowserView();
Controls.Add((Control) browserView);
browserView.Browser.LoadURL("http://www.youtube.com");
}
}
}
Run Code Online (Sandbox Code Playgroud)
运行上面的示例后,您将获得以下输出:
该图书馆是商业图书馆,但它可以免费用于开源和学术项目.商业许可证包括针对不同团队规模的支持包.也可以购买图书馆的源代码.
除了自己的页面外,该组件还可以作为NuGet包和Visual Studio Marketplace中的VSIX包使用.
我尝试了一些Webkit变体,但根据我的经验,没有什么能比OpenWebkitSharp的优势更好.喜欢它喜欢它.. HTML5和CSS3分数尽可能接近谷歌Chrome.非常好的API和事件模型.如果您发现"未实现"的API很可能是因为它们未在Webkit中实现.其他一切都很棒.
OpenWebKitSharp是基于WebKit.NET 0.5项目的webkit引擎的.net包装器.它为0.5版本增加了更多功能,并提供了比构建更多的方法.OpenWebKitSharp支持Cairo build(0.5)和webkit的每晚构建(默认为Nightly).在版本1.5稳定版和更多版本中,包含夜间版本并在构建后自动复制.在早期版本中,这发生在cairo构建中.OpenWebKitSharp目前支持GTLite Navigator,这是一款快速,稳定,灵活的Web浏览器.
小智 6
试试EO.WebBrowser.它是新的,基于最新版本的Chrome浏览器.关于它的最好的部分是它将所有内容打包在一个.NET dll中,因此它不仅非常易于使用和部署,而且同样的DLL支持32位和64位,因为它是.NET dll.
2018年5月更新
如果您将应用程序定位为在Windows 10上运行,那么现在您可以使用Windows Community Toolkit将Edge浏览器嵌入到.net应用程序中。
WPF示例:
1)安装Windows Community Toolkit Nuget软件包
Install-Package Microsoft.Toolkit.Win32.UI.Controls
Run Code Online (Sandbox Code Playgroud)
2)XAML代码
<Window
x:Class="WebViewTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:WPF="clr-namespace:Microsoft.Toolkit.Win32.UI.Controls.WPF;assembly=Microsoft.Toolkit.Win32.UI.Controls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WebViewTest"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="MainWindow"
Width="800"
Height="450"
mc:Ignorable="d">
<Grid>
<WPF:WebView x:Name="wvc" />
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
3)CS代码:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
// You can also use the Source property here or in the WPF designer
wvc.Navigate(new Uri("https://www.microsoft.com"));
}
}
Run Code Online (Sandbox Code Playgroud)
WinForms示例:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// You can also use the Source property here or in the designer
webView1.Navigate(new Uri("https://www.microsoft.com"));
}
}
Run Code Online (Sandbox Code Playgroud)
请参考此链接以获取更多信息。
归档时间: |
|
查看次数: |
409947 次 |
最近记录: |