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.
If you FEATURE_BROWSER_EMULATION to 11001, FEATURE_NINPUT_LEGACYMODE to 0 and FEATURE_96DPI_PIXEL to 1, this will result in a more modern WebBrowser control, resulting in better website compatibility, faster rendering, scrolling and zooming and content being easier to read in high DPI environments. Note that Microsoft states that FEATURE_96DPI_PIXEL is obsolete - though it still works and they have not provided an alternative that we could find. Also note that FEATURE_96DPI_PIXEL only improves system aware DPI scaling - it won't react to scaling changes after the application was launched, so be aware of this if you use Per Monitor DPI scaling.

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.

This snippet is available in Codly. Click the appropriate link below to download the snippet. If you don't have Codly, it is available here in the Microsoft Store.

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!

Follow us to be notified when we release new guides:

Comments

Popular posts from this blog

How to show placeholder text in a WPF TextBox in C# and VB

How to enable dark title bar in WPF and Windows Forms/WinForms apps in C# and VB

How to use modern icons in XAML in WPF on Windows 10 and 11

How to change the colour of a WPF or Windows Forms/WinForms title bar in Windows 11 in C# and VB

Microsoft WebView2: How to check if the WebView2 runtime is installed in C# and VB