webBrowser使用固定浏览器版本,屏蔽错误提示框简单实例

using Microsoft.Win32;
using mshtml;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    [ComVisible(true)]
    public partial class EnterpriseInfo : Form
    {
        #region 字段

        private string url;
        #endregion

        #region 构造函数
        public EnterpriseInfo(string urlInfo)
        {
            InitializeComponent();
            this.url = urlInfo;
            Init();
        }
        #endregion

        #region 方法
        private void Init()
        {
            this.webBrowserInfo.Navigate(url);
            LoadWebBrowser();
            SetWebBrowserFeatures(11);
        }

        /// <summary>
        /// 初始化web
        /// </summary>
        private void LoadWebBrowser()
        {
            webBrowserInfo.DocumentCompleted += WebBrowser_DocumentCompleted;
            webBrowserInfo.NewWindow += WebBrowser_NewWindow;
            webBrowserInfo.Navigated += WebBrowser_Navigated;
            webBrowserInfo.ObjectForScripting = this;
            webBrowserInfo.ScriptErrorsSuppressed = true; //设置不弹出脚本错误提示框
        }

        /// <summary>  
        /// 修改注册表信息来兼容当前程序  
        /// </summary>  
        static void SetWebBrowserFeatures(int ieVersion)
        {
            // don't change the registry if running in-proc inside Visual Studio  
            if (LicenseManager.UsageMode != LicenseUsageMode.Runtime)
                return;
            //获取程序及名称  
            var appName = System.IO.Path.GetFileName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);
            //得到浏览器的模式的值  
            UInt32 ieMode = GeoEmulationModee(ieVersion);
            var featureControlRegKey = @"HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\FeatureControl\";

            var featureControlRegKey2= @"HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Internet Explorer\Main\FeatureControl\";

            //设置浏览器对应用程序(appName)以什么模式(ieMode)运行  
            Registry.SetValue(featureControlRegKey + "FEATURE_BROWSER_EMULATION",
                appName, ieMode, RegistryValueKind.DWord);
            Registry.SetValue(featureControlRegKey2 + "FEATURE_BROWSER_EMULATION",
                appName, ieMode, RegistryValueKind.DWord);

            // enable the features which are "On" for the full Internet Explorer browser  
            Registry.SetValue(featureControlRegKey + "FEATURE_ENABLE_CLIPCHILDREN_OPTIMIZATION",
                appName, 1, RegistryValueKind.DWord);

            GetBrowserVersion();
            //Registry.SetValue(featureControlRegKey + "FEATURE_AJAX_CONNECTIONEVENTS",  
            //    appName, 1, RegistryValueKind.DWord);  


            //Registry.SetValue(featureControlRegKey + "FEATURE_GPU_RENDERING",  
            //    appName, 1, RegistryValueKind.DWord);  


            //Registry.SetValue(featureControlRegKey + "FEATURE_WEBOC_DOCUMENT_ZOOM",  
            //    appName, 1, RegistryValueKind.DWord);  


            //Registry.SetValue(featureControlRegKey + "FEATURE_NINPUT_LEGACYMODE",  
            //    appName, 0, RegistryValueKind.DWord);  
        }

        /// <summary>  
        /// 获取浏览器的版本  
        /// </summary>  
        /// <returns></returns>  
        static int GetBrowserVersion()
        {
            int browserVersion = 0;
            using (var ieKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Internet Explorer",
                RegistryKeyPermissionCheck.ReadSubTree,
                System.Security.AccessControl.RegistryRights.QueryValues))
            {
                var version = ieKey.GetValue("svcVersion");
                if (null == version)
                {
                    version = ieKey.GetValue("Version");
                    if (null == version)
                        throw new ApplicationException("Microsoft Internet Explorer is required!");
                }
                int.TryParse(version.ToString().Split('.')[0], out browserVersion);
            }
            //如果小于7  
            if (browserVersion < 7)
            {
                throw new ApplicationException("不支持的浏览器版本!");
            }
            return browserVersion;
        }
        /// <summary>  
        /// 通过版本得到浏览器模式的值  
        /// </summary>  
        /// <param name="browserVersion"></param>  
        /// <returns></returns>  
        static UInt32 GeoEmulationModee(int browserVersion)
        {
            UInt32 mode = 11000; // Internet Explorer 11. Webpages containing standards-based !DOCTYPE directives are displayed in IE11 Standards mode.   
            switch (browserVersion)
            {
                case 7:
                    mode = 7000; // Webpages containing standards-based !DOCTYPE directives are displayed in IE7 Standards mode.   
                    break;
                case 8:
                    mode = 8000; // Webpages containing standards-based !DOCTYPE directives are displayed in IE8 mode.   
                    break;
                case 9:
                    mode = 9000; // Internet Explorer 9. Webpages containing standards-based !DOCTYPE directives are displayed in IE9 mode.                      
                    break;
                case 10:
                    mode = 10000; // Internet Explorer 10.  
                    break;
                case 11:
                    mode = 11000; // Internet Explorer 11  
                    break;
            }
            return mode;
        }

        #endregion

        #region 事件

        private void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            WebBrowser web = (WebBrowser)sender;
        }

        ///<summary>
        /// WebBrowser新建窗口使不弹出新窗体,在webbroswer中加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void WebBrowser_NewWindow(object sender, CancelEventArgs e)
        {
            //防止弹窗
            e.Cancel = true;
        }

        /// <summary>
        /// 屏蔽网页alert,使用内部弹出框,如果是是申报最后一个报表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void WebBrowser_Navigated(object sender, WebBrowserNavigatedEventArgs e)
        {
            WebBrowser web = (WebBrowser)sender;
            IHTMLWindow2 win = (IHTMLWindow2)webBrowserInfo.Document.Window.DomWindow;
            StringBuilder strAlertBuilder = new StringBuilder();
            strAlertBuilder.Append("var _alert=window.alert;\n");
            strAlertBuilder.Append("window.alert=function(s){\n");
            strAlertBuilder.Append("window.external.alertMessage(s);}");
            win.execScript(strAlertBuilder.ToString(), "javascript");
        }

        #endregion
    }
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值