1. 程式人生 > >WPF中使用瀏覽器控制元件WebBrowser

WPF中使用瀏覽器控制元件WebBrowser

設定使用IE的版本

    public static class Extensions
    {
        #region  設定WebBroswer 使用IE版本
        public 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\";
            //設定瀏覽器對應用程式(appName)以什麼模式(ieMode)執行  
            Registry.SetValue(featureControlRegKey + "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);
        }

        /// <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)
                    {
                        System.Windows.MessageBox.Show("只支援IE瀏覽器");
                    }
                }
                int.TryParse(version.ToString().Split('.')[0], out browserVersion);
            }
            //如果小於7  
            if (browserVersion < 7)
            {
                System.Windows.MessageBox.Show("不支援的瀏覽器版本!");
            }
            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

    }

遮蔽指令碼錯誤彈窗

使用的是System.Windows.Controls.WebBrowser時使用如下程式碼

        private void Browser_Navigated(object sender, NavigationEventArgs e)
        {
            var fiComWebBrowser = typeof(WebBrowser).GetField("_axIWebBrowser2", BindingFlags.Instance | BindingFlags.NonPublic);
            if (fiComWebBrowser == null)
                return;

            object objComWebBrowser = fiComWebBrowser.GetValue(browser);
            if (objComWebBrowser == null)
                return;

            objComWebBrowser.GetType().InvokeMember("Silent", System.Reflection.BindingFlags.SetProperty, null, objComWebBrowser, new object[] { true });
        }

使用的是 System.Windows.Forms.WebBrowser時只需將WebBrowser的ScriptErrorsSuppressed設定為true

            browser.ScriptErrorsSuppressed = true;

處理彈出新視窗

WPF中的WebBrowser 詳情見監聽WPF的WebBrowser控制元件彈出新視窗的事件

WinFrom中的WebBrowser

通過NewWindow事件加以處理,程式碼如下:

 private void InnerBrowser_NewWindow(object sender, System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = true;
            try
            {
                InnerBrowser.Navigate(InnerBrowser.StatusText);
            }
            catch (Exception ex)
            {

            }
        }

[程式碼下載地址](https://download.csdn.net/download/sanyuni/10729884)