首页 > 解决方案 > 在 C# 中显示网页

问题描述

我想在我的 C# 项目的 Windows 窗体中显示网页:http: //vhg.cmp.uea.ac.uk/tech/jas/vhg2018/WebGLAv.html

网页截图

我在其中使用 WebBrowser 工具并编写此代码以在表单中显示此网页:

webBrowser1.Navigate("http://vhg.cmp.uea.ac.uk/tech/jas/vhg2018/WebGLAv.html");

但它没有显示完整的网页!

这显示这样

我应该怎么做?

标签: c#htmlwinformswebbrowser-control

解决方案


默认情况下,Windows 窗体应用程序使用 IE 包装器,并且不保证使用最新的 IE 版本。阅读本文以了解 IE 包装器背后发生的事情以及 Windows 仿真密钥的作用。

我的一个旧项目中的这段代码允许以编程方式为您的可执行进程更改 IE 的默认仿真版本:

private static readonly string BrowserEmulationRegistryKeyPath =
            @"Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION";

        /// <summary>
        /// Add the process name to internet explorer emulation key
>       /// i do this because the default IE wrapper dose not support the latest JS features
>       /// Add process to emulation key and set a DWord value (11001) for it means we want use IE.11 as WebBrowser component
        /// </summary>
        public bool EmulateInternetExplorer()
        {
            using (
                var browserEmulationKey = Registry.CurrentUser.OpenSubKey(BrowserEmulationRegistryKeyPath,
                    true))
            {
                if (browserEmulationKey == null)
                    Registry.CurrentUser.CreateSubKey(BrowserEmulationRegistryKeyPath);


                string processName = $"{Process.GetCurrentProcess().ProcessName}.exe";

                // Means emulation already added and we are ready to start
                if (browserEmulationKey?.GetValue(processName) != null)
                    return true;

                // Emulation key not exists and we must add it ( We return false because application restart to take effect of changes )
                if (browserEmulationKey != null)
                {
                    browserEmulationKey.SetValue(processName, 11001, RegistryValueKind.DWord);
                    browserEmulationKey.Flush();
                }
                return false;
            }
        }

如果您的网站无法在最新的 Internet Explorer(不兼容)中正确显示,您应该使用其他 Web 浏览器包装器,例如在您的 .Net 应用程序中嵌入 Chromium 的 cefSharp。


推荐阅读