Tallan's Technology Blog

Tallan's Top Technologists Share Their Thoughts on Today's Technology Challenges

Setting Compatibility Mode Of A WPF Web Browser

Jordan Piscitelli

Recently I needed to use the WebBrowser control in a C Sharp WPF application. But after adding the web browser and navigating to the web page I was met with a horribly mangled version of it. Yet in Internet Explorer the same web page displayed correctly and as you may or may not know, the WebBrowser control uses Internet Explorer. I then discovered that checking Compatibility Mode in Internet Explorer changed the page to match the WebBrowser controls! It turns out the WebBrowser control was defaulting to a rendering engine (IE7) that I did not want!

As an example, here is the popular game 2048 in Internet Explorer with and without compatibility mode and in the WebBrowser control before the fix in this post and after.

As you can see there is a big difference! To fix this issue we are going to change the Internet Feature Control for Browser Emulation in the registry, with some help from Stack Overflow. First, we are going to need a method that can set the value for us in the registry. So add the method SetBrowserFeatureControlKey to your class (or a helper class). This helper method will set the Internet Explorer Feature registry entry for a given key and application.

private void SetBrowserFeatureControlKey(string feature, string appName, uint value) {
            using (var key = Registry.CurrentUser.CreateSubKey(
                String.Concat(@"Software\Microsoft\Internet Explorer\Main\FeatureControl\", feature),
                RegistryKeyPermissionCheck.ReadWriteSubTree))
            {
                key.SetValue(appName, (UInt32)value, RegistryValueKind.DWord);
            }
        }

Next we will add a method to change the FEATURE_BROWSER_EMULATION registry entry to the mode we want, in my case, Internet Explorer 10.

public void SetBrowserEmulationMode() {
            var fileName = System.IO.Path.GetFileName(Process.GetCurrentProcess().MainModule.FileName);

            if (String.Compare(fileName, "devenv.exe", true) == 0 || String.Compare(fileName, "XDesProc.exe", true) == 0)
                return;
            UInt32 mode = 10000;
            SetBrowserFeatureControlKey("FEATURE_BROWSER_EMULATION", fileName, mode);
        }

And all you need to do is call the SetBrowserEmulationMode in your window’s constructor! If you are interested in what this code set in the registry, take a look at the screenshot below.

2 Comments. Leave new

You are my new hero. You’ll go straight to heaven bro. XOXO.

Well, unfortunately this method can’t be used in WP8, since they removed all parts of the API that allowed us to touch to registry keys.
Still, thanks for sharing.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

\\\