C# Windows フォームアプリケーションの Web ブラウザー コントロールの既定のバージョンは 7 です。ブラウザー エミュレーション の記事で 9 に変更しましたが、インストールされている Internet Explorer の最新バージョンを Web ブラウザー コントロールで使用するにはどうすればよいですか?
14 に答える
私はVeerの答えを見ました。それは正しいと思いますが、私にはうまくいきませんでした。たぶん私は.NET 4を使用していて、64x OSを使用しているので、これを確認してください.
アプリケーションの起動時にセットアップまたはチェックすることができます。
private void Form1_Load(object sender, EventArgs e)
{
var appName = Process.GetCurrentProcess().ProcessName + ".exe";
SetIE8KeyforWebBrowserControl(appName);
}
private void SetIE8KeyforWebBrowserControl(string appName)
{
RegistryKey Regkey = null;
try
{
// For 64 bit machine
if (Environment.Is64BitOperatingSystem)
Regkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\\Wow6432Node\\Microsoft\\Internet Explorer\\Main\\FeatureControl\\FEATURE_BROWSER_EMULATION", true);
else //For 32 bit machine
Regkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\\Microsoft\\Internet Explorer\\Main\\FeatureControl\\FEATURE_BROWSER_EMULATION", true);
// If the path is not correct or
// if the user haven't priviledges to access the registry
if (Regkey == null)
{
MessageBox.Show("Application Settings Failed - Address Not found");
return;
}
string FindAppkey = Convert.ToString(Regkey.GetValue(appName));
// Check if key is already present
if (FindAppkey == "8000")
{
MessageBox.Show("Required Application Settings Present");
Regkey.Close();
return;
}
// If a key is not present add the key, Key value 8000 (decimal)
if (string.IsNullOrEmpty(FindAppkey))
Regkey.SetValue(appName, unchecked((int)0x1F40), RegistryValueKind.DWord);
// Check for the key after adding
FindAppkey = Convert.ToString(Regkey.GetValue(appName));
if (FindAppkey == "8000")
MessageBox.Show("Application Settings Applied Successfully");
else
MessageBox.Show("Application Settings Failed, Ref: " + FindAppkey);
}
catch (Exception ex)
{
MessageBox.Show("Application Settings Failed");
MessageBox.Show(ex.Message);
}
finally
{
// Close the Registry
if (Regkey != null)
Regkey.Close();
}
}
テスト用に、messagebox.show が見つかる場合があります。
キーは次のとおりです。
11001 (0x2AF9) - Internet Explorer 11. ディレクティブに関係なく、Web ページが IE11 エッジ モードで表示されます
!DOCTYPE
。11000 (0x2AF8) - Internet Explorer 11。標準ベースの
!DOCTYPE
ディレクティブを含む Web ページが IE11 エッジ モードで表示されます。IE11 のデフォルト値。10001 (0x2711)
!DOCTYPE
- Internet Explorer 10. Web ページは、ディレクティブに関係なく、IE10 標準モードで表示されます。10000 (0x2710) - Internet Explorer 10。標準ベースの
!DOCTYPE
ディレクティブを含む Web ページは IE10 標準モードで表示されます。Internet Explorer 10 のデフォルト値。9999 (0x270F)
!DOCTYPE
- Internet Explorer 9. Web ページは、ディレクティブに関係なく、IE9 標準モードで表示されます。9000 (0x2328) - Internet Explorer 9。標準ベースの
!DOCTYPE
ディレクティブを含む Web ページが IE9 モードで表示されます。8888 (0x22B8)ディレクティブに関係なく、Web ページが IE8 標準モードで表示される
!DOCTYPE
.8000 (0x1F40) - 標準ベースの
!DOCTYPE
ディレクティブを含む Web ページが IE8 モードで表示されます。7000 (0x1B58) - 標準ベースの
!DOCTYPE
ディレクティブを含む Web ページが IE7 標準モードで表示されます。
参照: MSDN: Internet Feature Controls
Skype などのアプリケーションが 10001 を使用しているのを見ましたが、わかりません。
ノート
セットアップ アプリケーションは、レジストリを変更します。レジストリの変更のアクセス許可によるエラーを回避するために、マニフェスト ファイルに次の行を追加する必要がある場合があります。
<requestedExecutionLevel level="highestAvailable" uiAccess="false" />
更新 1
これは、Windows で最新バージョンの IE を取得し、必要に応じて変更を加えるクラスです。
public class WebBrowserHelper
{
public static int GetEmbVersion()
{
int ieVer = GetBrowserVersion();
if (ieVer > 9)
return ieVer * 1000 + 1;
if (ieVer > 7)
return ieVer * 1111;
return 7000;
} // End Function GetEmbVersion
public static void FixBrowserVersion()
{
string appName = System.IO.Path.GetFileNameWithoutExtension(System.Reflection.Assembly.GetExecutingAssembly().Location);
FixBrowserVersion(appName);
}
public static void FixBrowserVersion(string appName)
{
FixBrowserVersion(appName, GetEmbVersion());
} // End Sub FixBrowserVersion
// FixBrowserVersion("<YourAppName>", 9000);
public static void FixBrowserVersion(string appName, int ieVer)
{
FixBrowserVersion_Internal("HKEY_LOCAL_MACHINE", appName + ".exe", ieVer);
FixBrowserVersion_Internal("HKEY_CURRENT_USER", appName + ".exe", ieVer);
FixBrowserVersion_Internal("HKEY_LOCAL_MACHINE", appName + ".vshost.exe", ieVer);
FixBrowserVersion_Internal("HKEY_CURRENT_USER", appName + ".vshost.exe", ieVer);
} // End Sub FixBrowserVersion
private static void FixBrowserVersion_Internal(string root, string appName, int ieVer)
{
try
{
//For 64 bit Machine
if (Environment.Is64BitOperatingSystem)
Microsoft.Win32.Registry.SetValue(root + @"\Software\Wow6432Node\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION", appName, ieVer);
else //For 32 bit Machine
Microsoft.Win32.Registry.SetValue(root + @"\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION", appName, ieVer);
}
catch (Exception)
{
// some config will hit access rights exceptions
// this is why we try with both LOCAL_MACHINE and CURRENT_USER
}
} // End Sub FixBrowserVersion_Internal
public static int GetBrowserVersion()
{
// string strKeyPath = @"HKLM\SOFTWARE\Microsoft\Internet Explorer";
string strKeyPath = @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer";
string[] ls = new string[] { "svcVersion", "svcUpdateVersion", "Version", "W2kVersion" };
int maxVer = 0;
for (int i = 0; i < ls.Length; ++i)
{
object objVal = Microsoft.Win32.Registry.GetValue(strKeyPath, ls[i], "0");
string strVal = System.Convert.ToString(objVal);
if (strVal != null)
{
int iPos = strVal.IndexOf('.');
if (iPos > 0)
strVal = strVal.Substring(0, iPos);
int res = 0;
if (int.TryParse(strVal, out res))
maxVer = Math.Max(maxVer, res);
} // End if (strVal != null)
} // Next i
return maxVer;
} // End Function GetBrowserVersion
}
次のようにクラスを使用する
WebBrowserHelper.FixBrowserVersion();
WebBrowserHelper.FixBrowserVersion("SomeAppName");
WebBrowserHelper.FixBrowserVersion("SomeAppName",intIeVer);
Windows 10 の比較で問題が発生する可能性があります。ウェブサイト自体が原因で、このメタ タグを追加する必要がある場合があります。
<meta http-equiv="X-UA-Compatible" content="IE=11" >
楽しみ :)
MSDNの値を使用:
int BrowserVer, RegVal;
// get the installed IE version
using (WebBrowser Wb = new WebBrowser())
BrowserVer = Wb.Version.Major;
// set the appropriate IE version
if (BrowserVer >= 11)
RegVal = 11001;
else if (BrowserVer == 10)
RegVal = 10001;
else if (BrowserVer == 9)
RegVal = 9999;
else if (BrowserVer == 8)
RegVal = 8888;
else
RegVal = 7000;
// set the actual key
using (RegistryKey Key = Registry.CurrentUser.CreateSubKey(@"SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION", RegistryKeyPermissionCheck.ReadWriteSubTree))
if (Key.GetValue(System.Diagnostics.Process.GetCurrentProcess().ProcessName + ".exe") == null)
Key.SetValue(System.Diagnostics.Process.GetCurrentProcess().ProcessName + ".exe", RegVal, RegistryValueKind.DWord);
var appName = System.Diagnostics.Process.GetCurrentProcess().ProcessName + ".exe";
using (var Key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION", true))
Key.SetValue(appName, 99999, RegistryValueKind.DWord);
私がここで読んだことによると ( Controlling WebBrowser Control Compatibility :
クライアントで FEATURE_BROWSER_EMULATION ドキュメント モードの値を IE のバージョンより高く設定するとどうなりますか?
明らかに、ブラウザー コントロールは、クライアントにインストールされている IE バージョン以下のドキュメント モードのみをサポートできます。FEATURE_BROWSER_EMULATION キーの使用は、ブラウザーの展開されたサポート バージョンがあるエンタープライズ ライン オブ ビジネス アプリに最適です。クライアントにインストールされているブラウザ バージョンよりも新しいバージョンのブラウザ モードに値を設定した場合、ブラウザ コントロールは利用可能な最高のドキュメント モードを選択します。
最も簡単なことは、非常に高い 10 進数を入力することです...
RegKey を変更する代わりに、HTML のヘッダーに次の行を追加できました。
<html>
<head>
<!-- Use lastest version of Internet Explorer -->
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<!-- Insert other header tags here -->
</head>
...
</html>
Web ブラウザ コントロールと IE バージョンの指定を参照してください。
ここで、私が通常使用し、私のために機能する方法を示します (32 ビットと 64 ビットの両方のアプリケーションで使用できます。ie_emulation は、ここに記載されている誰でもかまいません: Internet Feature Controls (B..C), Browser Emulation ):
[STAThread]
static void Main()
{
if (!mutex.WaitOne(TimeSpan.FromSeconds(2), false))
{
// Another application instance is running
return;
}
try
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var targetApplication = Process.GetCurrentProcess().ProcessName + ".exe";
int ie_emulation = 10000;
try
{
string tmp = Properties.Settings.Default.ie_emulation;
ie_emulation = int.Parse(tmp);
}
catch { }
SetIEVersioneKeyforWebBrowserControl(targetApplication, ie_emulation);
m_webLoader = new FormMain();
Application.Run(m_webLoader);
}
finally
{
mutex.ReleaseMutex();
}
}
private static void SetIEVersioneKeyforWebBrowserControl(string appName, int ieval)
{
RegistryKey Regkey = null;
try
{
Regkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION", true);
// If the path is not correct or
// if user haven't privileges to access the registry
if (Regkey == null)
{
YukLoggerObj.logWarnMsg("Application FEATURE_BROWSER_EMULATION Failed - Registry key Not found");
return;
}
string FindAppkey = Convert.ToString(Regkey.GetValue(appName));
// Check if key is already present
if (FindAppkey == "" + ieval)
{
YukLoggerObj.logInfoMsg("Application FEATURE_BROWSER_EMULATION already set to " + ieval);
Regkey.Close();
return;
}
// If a key is not present or different from desired, add/modify the key, key value
Regkey.SetValue(appName, unchecked((int)ieval), RegistryValueKind.DWord);
// Check for the key after adding
FindAppkey = Convert.ToString(Regkey.GetValue(appName));
if (FindAppkey == "" + ieval)
YukLoggerObj.logInfoMsg("Application FEATURE_BROWSER_EMULATION changed to " + ieval + "; changes will be visible at application restart");
else
YukLoggerObj.logWarnMsg("Application FEATURE_BROWSER_EMULATION setting failed; current value is " + ieval);
}
catch (Exception ex)
{
YukLoggerObj.logWarnMsg("Application FEATURE_BROWSER_EMULATION setting failed; " + ex.Message);
}
finally
{
// Close the Registry
if (Regkey != null)
Regkey.Close();
}
}
ルカのソリューションを実装することはできましたが、機能させるにはいくつかの変更を加える必要がありました。私の目標は、Windows フォーム アプリケーション (.NET 2.0 を対象とする) の Web ブラウザー コントロールで D3.js を使用することでした。それは今私のために働いています。これが他の誰かを助けることができることを願っています。
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Threading;
using Microsoft.Win32;
using System.Diagnostics;
namespace ClientUI
{
static class Program
{
static Mutex mutex = new System.Threading.Mutex(false, "jMutex");
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
if (!mutex.WaitOne(TimeSpan.FromSeconds(2), false))
{
// Another application instance is running
return;
}
try
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var targetApplication = Process.GetCurrentProcess().ProcessName + ".exe";
int ie_emulation = 11999;
try
{
string tmp = Properties.Settings.Default.ie_emulation;
ie_emulation = int.Parse(tmp);
}
catch { }
SetIEVersioneKeyforWebBrowserControl(targetApplication, ie_emulation);
Application.Run(new MainForm());
}
finally
{
mutex.ReleaseMutex();
}
}
private static void SetIEVersioneKeyforWebBrowserControl(string appName, int ieval)
{
RegistryKey Regkey = null;
try
{
Regkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION", true);
// If the path is not correct or
// if user doesn't have privileges to access the registry
if (Regkey == null)
{
MessageBox.Show("Application FEATURE_BROWSER_EMULATION Failed - Registry key Not found");
return;
}
string FindAppkey = Convert.ToString(Regkey.GetValue(appName));
// Check if key is already present
if (FindAppkey == ieval.ToString())
{
MessageBox.Show("Application FEATURE_BROWSER_EMULATION already set to " + ieval);
Regkey.Close();
return;
}
// If key is not present or different from desired, add/modify the key , key value
Regkey.SetValue(appName, unchecked((int)ieval), RegistryValueKind.DWord);
// Check for the key after adding
FindAppkey = Convert.ToString(Regkey.GetValue(appName));
if (FindAppkey == ieval.ToString())
{
MessageBox.Show("Application FEATURE_BROWSER_EMULATION changed to " + ieval + "; changes will be visible at application restart");
}
else
{
MessageBox.Show("Application FEATURE_BROWSER_EMULATION setting failed; current value is " + ieval);
}
}
catch (Exception ex)
{
MessageBox.Show("Application FEATURE_BROWSER_EMULATION setting failed; " + ex.Message);
}
finally
{
//Close the Registry
if (Regkey != null) Regkey.Close();
}
}
}
}
また、プロジェクトの設定に 11999 という値の文字列 (ie_emulation) を追加しました。この値は IE11(11.0.15) で機能しているようです。
次に、レジストリへのアクセスを許可するように、アプリケーションのアクセス許可を変更する必要がありました。これは、プロジェクトに新しいアイテムを追加することで実行できます (VS2012 を使用)。[一般項目] で、[アプリケーション マニフェスト ファイル] を選択します。レベルを asInvoker から requireAdministrator に変更します (以下を参照)。
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
これを読んでいる人が Web ブラウザー コントロールで D3.js を使用しようとしている場合、D3.json は XmlHttpRequest を使用するため (Web サーバーで使用する方が簡単)、HTML ページ内の変数内に格納されるように JSON データを変更する必要がある場合があります。これらの変更と上記の後、私の Windows フォームは、D3 を呼び出すローカル HTML ファイルを読み込むことができます。
RooiWillie と MohD の回答を組み合わせて
、アプリを管理者権限で実行することを忘れないでください。
var appName = System.Diagnostics.Process.GetCurrentProcess().ProcessName + ".exe";
RegistryKey Regkey = null;
try
{
int BrowserVer, RegVal;
// get the installed IE version
using (WebBrowser Wb = new WebBrowser())
BrowserVer = Wb.Version.Major;
// set the appropriate IE version
if (BrowserVer >= 11)
RegVal = 11001;
else if (BrowserVer == 10)
RegVal = 10001;
else if (BrowserVer == 9)
RegVal = 9999;
else if (BrowserVer == 8)
RegVal = 8888;
else
RegVal = 7000;
//For 64 bit Machine
if (Environment.Is64BitOperatingSystem)
Regkey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"SOFTWARE\\Wow6432Node\\Microsoft\\Internet Explorer\\MAIN\\FeatureControl\\FEATURE_BROWSER_EMULATION", true);
else //For 32 bit Machine
Regkey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"SOFTWARE\\Microsoft\\Internet Explorer\\Main\\FeatureControl\\FEATURE_BROWSER_EMULATION", true);
//If the path is not correct or
//If user't have priviledges to access registry
if (Regkey == null)
{
MessageBox.Show("Registry Key for setting IE WebBrowser Rendering Address Not found. Try run the program with administrator's right.");
return;
}
string FindAppkey = Convert.ToString(Regkey.GetValue(appName));
//Check if key is already present
if (FindAppkey == RegVal.ToString())
{
Regkey.Close();
return;
}
Regkey.SetValue(appName, RegVal, RegistryValueKind.DWord);
}
catch (Exception ex)
{
MessageBox.Show("Registry Key for setting IE WebBrowser Rendering failed to setup");
MessageBox.Show(ex.Message);
}
finally
{
//Close the Registry
if (Regkey != null)
Regkey.Close();
}
WebView2の形でより優れたソリューションが利用可能になりました。コントロールは、WebView2
レンダリング エンジンとして Chromium に基づく Microsoft Edge を使用して、ネイティブ アプリで Web コンテンツを表示します。このコントロールは、アプリケーションWinforms
だけでなく、でも使用できWPF
ます。
これを で使用するには、 NugetPackageWinforms
をインストールするだけです。その後、アプリケーションで が利用できるようになります。Microsoft.Web.WebView2
WebView2
詳細はこちら。
可能な限り最高のモードを強制するのが最善です。これは、次を追加することで実現できます。
<meta http-equiv="X-UA-Compatible" content="IE=edge">
IE をサポートするために polyfill ライブラリを含めることは常に良いことです:
<script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>
スクリプトの前。