Webbrowser Component Not Showing CSS 3

WebBrowser component not showing CSS 3

Just for further reference to other people needing this:

First of all:
Thanks to Boo & Lex Li
For helping me find the answer to my question.

You have to set a certain registry to the right value:

There are two different sets of keys for 32 bit and 64 bit applications.

32 bit:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION

Value Key: yourapplication.exe

64 bit:

HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION

Value Key: yourapplication.exe

The value to set this key to is (taken from MSDN here) as decimal values:

9999 (0x270F)
Internet Explorer 9. Webpages are displayed in IE9 Standards mode, regardless of the !DOCTYPE directive.

9000 (0x2328)
Internet Explorer 9. Webpages containing standards-based !DOCTYPE directives are displayed in IE9 mode.

8888 (0x22B8)
Webpages are displayed in IE8 Standards mode, regardless of the !DOCTYPE directive.

8000 (0x1F40)
Webpages containing standards-based !DOCTYPE directives are displayed in IE8 mode.

7000 (0x1B58)
Webpages containing standards-based !DOCTYPE directives are displayed in IE7 Standards mode.

Even tough MSDn claims that 9000 is the automatically assigned value. Apperently this is simply not true.

Below you can find the code how to add these keys to your registry. Please not that your application has a different processname when you debug.

RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION", true);
if (key != null)
{
key.SetValue("YourApplicationName.exe", 9000, RegistryValueKind.DWord);
}

key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Wow6432Node\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION", true);
if (key != null)
{
key.SetValue("YourApplicationName.exe", 9000, RegistryValueKind.DWord);
}

So thanks all and Good Luck

Edit: User Account Control should be off to make this code work.

Why does some CSS Function not work in my WPF WebBrowser Element?

The WebBrowser control in WPF internally uses a native ActiveX control that is kind of dated.

If you want a modern browser experience that supports the latest HTML and CSS in your app, you should switch to using the WebView2 control.

Winform WebBrowser Not Displaying CSS

Internet Explorer 7 rendering will be used if you do not override the Feature Browser Emultate setting in the registry.

For your user (Current User) only use this key:

  • HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION

For all users on the computer/server use this key:

  • On 64 bit app on 64 bit- or 32 bit app on 32 bit machine:

    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION

  • On 32 bit app on 64 bit machine:

    HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION

For example if your executable is named flying_toaster.exe. You will have to add an DWORD entry with the name flying_toaster.exe and with a value 11001 (to use Internet Exlporer 11-rendering).

Sample Image

A complete list of values is listed below (quoted from MSDN):

11001 (0x2AF9) Internet Explorer 11. Webpages are displayed in IE11 edge mode, regardless of the !DOCTYPE directive.

11000 (0x2AF8) IE11. Webpages containing standards-based !DOCTYPE directives are displayed in IE11 edge mode. Default value for IE11.

10001 (0x2711) Internet Explorer 10. Webpages are displayed in IE10 Standards mode, regardless of the !DOCTYPE directive.

10000 (0x02710) Internet Explorer 10. Webpages containing standards-based !DOCTYPE directives are displayed in IE10 Standards mode. Default value for Internet Explorer 10.

9999 (0x270F) Windows Internet Explorer 9. Webpages are displayed in IE9 Standards mode, regardless of the !DOCTYPE directive.

9000 (0x2328) Internet Explorer 9. Webpages containing standards-based !DOCTYPE directives are displayed in IE9 mode. Default value for Internet Explorer 9.
Important In Internet Explorer 10, Webpages containing standards-based !DOCTYPE directives are displayed in IE10 Standards mode.

8888 (0x22B8) Webpages are displayed in IE8 Standards mode, regardless of the !DOCTYPE directive.

8000 (0x1F40) Webpages containing standards-based !DOCTYPE directives are displayed in IE8 mode. Default value for Internet Explorer 8
Important In Internet Explorer 10, Webpages containing standards-based !DOCTYPE directives are displayed in IE10 Standards mode.

7000 (0x1B58) Webpages containing standards-based !DOCTYPE directives are displayed in IE7 Standards mode. Default value for applications hosting the WebBrowser Control.

Read more about the in this blog post "Web Browser Control – Specifying the IE Version"

Also look into the MSDN Documentation about Feature Controls.

Injecting CSS in WebBrowser Control

What I did to get it working

        CurrentDocument = (mshtml.HTMLDocument)webBrowser1.Document.DomDocument;
styleSheet = CurrentDocument.createStyleSheet("", 0);

StreamReader streamReader = new StreamReader(@"test.css"); //test.css is Stylesheet file
string text = streamReader.ReadToEnd();
streamReader.Close();
styleSheet.cssText = text;

How to inject CSS in WebBrowser control?

I didn't try this myself but since CSS style rules can be included in a document using the <style> tag as in:

<html>
<head>
<style type="text/css">
h1 {color:red}
p {color:blue}
</style>
</head>

you could try giving:

HtmlElement head = webBrowser1.Document.GetElementsByTagName("head")[0];
HtmlElement styleEl = webBrowser1.Document.CreateElement("style");
IHTMLStyleElement element = (IHTMLStyleElement)styleEl.DomElement;
IHTMLStyleSheetElement styleSheet = element.styleSheet;
styleSheet.cssText = @"h1 { color: red }";
head.AppendChild(styleEl);

a go. You can find more info on the IHTMLStyleElement here.

Edit

It seems the answer is much much simpler than I originally thought:

  using mshtml;

IHTMLDocument2 doc = (webBrowser1.Document.DomDocument) as IHTMLDocument2;
// The first parameter is the url, the second is the index of the added style sheet.
IHTMLStyleSheet ss = doc.createStyleSheet("", 0);

// Now that you have the style sheet you have a few options:
// 1. You can just set the content as text.
ss.cssText = @"h1 { color: blue; }";
// 2. You can add/remove style rules.
int index = ss.addRule("h1", "color: red;");
ss.removeRule(index);
// You can even walk over the rules using "ss.rules" and modify them.

I wrote a small test project to verify that this works. I arrived at this final result by doing a search on MSDN for IHTMLStyleSheet, upon which I happened across this page, this page and this one.

C# WebBrowser CSS hover style not working (despite configuration of browser emulation)

I've made what you depicted. Everything worked out after I substituted 69632 with 11000 and got the correct name of the program process this way:

var fileName = System.IO.Path.GetFileName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);

So that setting the parameter is:

keyIeFeatureBrowserEmulation.SetValue(fileName, 11000);


Related Topics



Leave a reply



Submit