Easily modernise your WPF and Windows Forms/WinForms WebBrowser controls with 3 lines of C# or VB code
If you've ever used the WPF or Windows Forms WebBrowser control in recent years, it's quite likely that you've noticed that many websites don't work well with it and that it can be slow when rendering content. This control uses Internet Explorer to render web contents and although Internet Explorer may be old, it's actually better than how it behaves by default when using the WebBrowser controls. This is because of compatibility features that result in the WebBrowser control not being as up-to-date as it should. Luckily, it's very easy to disable these compatibility features for your app by simply adding a value to the registry (and note that this works even if your app is packaged and using registry virtualisation). There are 3 compatibility features that can greatly affect the behaviour of the WebBrowser control and these are FEATURE_BROWSER_EMULATION, FEATURE_NINPUT_LEGACYMODE and FEATURE_96DPI_PIXEL. Here's what they all do:
- FEATURE_BROWSER_EMULATION - This feature causes the WebBrowser control to behave as though it is using an older version of Internet Explorer.
- FEATURE_NINPUT_LEGACYMODE - This disables modern features such as hardware accelerated panning and zooming is disabled, tap to select text and pointer and gesture events.
- FEATURE_96DPI_PIXEL - This feature enables better DPI scaling behaviour.
To set these values, you just have to add a DWORD to the key 'HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\FeatureControl' with the name being the file name of the executable file of your app. Then, set the value according to the information below:
- FEATURE_BROWSER_EMULATION - This can be set to a variety of different values depending on the behaviour you want. The highest value you can use here is '11001'. This will result in any WebBrowsers running inside that executable using Internet Explorer 11. By default, WebBrowsers will emulate Internet Explorer 8 if this value is not set. View more information about this feature here.
- FEATURE_NINPUT_LEGACYMODE - This can be set to 1 or 0 and is set to 1 by default. Setting this to 0 will disable this compatibility feature, resulting in more modern behaviour. View more information about this feature here.
- FEATURE_96DPI_PIXEL - Setting this to 1 will result in better system aware DPI scaling behaviour. If you find content in WebBrowsers appears too small, this can help. View more information about this feature here.
You can set these values in code in C# like so:
Microsoft.Win32.Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Internet Explorer\\Main\\FeatureControl\\FEATURE_BROWSER_EMULATION", Microsoft.Win32.RegistryKeyPermissionCheck.Default).SetValue(System.IO.Path.GetFileName(System.Windows.Forms.Application.ExecutablePath), 11001, Microsoft.Win32.RegistryValueKind.DWord);
Microsoft.Win32.Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Internet Explorer\\Main\\FeatureControl\\FEATURE_NINPUT_LEGACYMODE", Microsoft.Win32.RegistryKeyPermissionCheck.Default).SetValue(System.IO.Path.GetFileName(System.Windows.Forms.Application.ExecutablePath), 0, Microsoft.Win32.RegistryValueKind.DWord);
Microsoft.Win32.Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Internet Explorer\\Main\\FeatureControl\\FEATURE_96DPI_PIXEL", Microsoft.Win32.RegistryKeyPermissionCheck.Default).SetValue(System.IO.Path.GetFileName(System.Windows.Forms.Application.ExecutablePath), 1, Microsoft.Win32.RegistryValueKind.DWord);
And in VB:
Microsoft.Win32.Registry.CurrentUser.CreateSubKey("Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION", Microsoft.Win32.RegistryKeyPermissionCheck.Default).SetValue(System.IO.Path.GetFileName(Forms.Application.ExecutablePath), 11001, Microsoft.Win32.RegistryValueKind.DWord)
Microsoft.Win32.Registry.CurrentUser.CreateSubKey("Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_NINPUT_LEGACYMODE", Microsoft.Win32.RegistryKeyPermissionCheck.Default).SetValue(System.IO.Path.GetFileName(Forms.Application.ExecutablePath), 0, Microsoft.Win32.RegistryValueKind.DWord)
Microsoft.Win32.Registry.CurrentUser.CreateSubKey("Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_96DPI_PIXEL", Microsoft.Win32.RegistryKeyPermissionCheck.Default).SetValue(System.IO.Path.GetFileName(Forms.Application.ExecutablePath), 1, Microsoft.Win32.RegistryValueKind.DWord)
You can run the above code when your app starts up before you initialise any WebBrowsers to ensure the values apply.
Note: For a more modern web control, you could use the WebView, WebViewCompatible or WebView2 controls. If you need help with these controls, we have guides on how to allow moving focus to and from the WPF WebView control using the Tab key and how to check if the WebView2 runtime is installed.
Did this guide help you? Do you have anything to add? Let us know in the comments! Also, if this guide did help you and you use an AdBlocker and you want to support us, consider disabling it for this site - if you do, it will be greatly appreciated!
Comments
Post a Comment