WebBrowser

  • Thread starter Thread starter Crazypennie
  • Start date Start date
C

Crazypennie

Guest
Here I come again with some Bla-Bla ...

==================

THE PROBLEM

Everybody that have put a webBrowser in an application have noticed that there is a big difference between the way IE render a web page and the way the webBrowser does.

The WebBrowser does ignore many CSS style and many HTML tags. Also some JavaScript function don't work in the WebBrowser.

The reason is:

The WebBrowser, is in fact a wrapper around the win32 WebBrowser Control. And the control is hosted into the application.

What causes the problem is that for compatibility reasons, the webBrowser control emulates IE7. ... and therefore handles only the feature that were available in IE7. So forget about all the features that were created after 2006

.

=====================

THE SOLUTION

It is possible to have the webBrowser control to emulate any version of IE ... As long the IE version it is installed on the

machine !!

Here how to do:

.

The first step is to find the IE version installed.

The version can be find in the Registry. Since IE10 and IE11 are not using the same key than the previous versions, you should use a code similar to this to get the version

Dim Version As String = ""
Dim ieVersion As Object = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("Software\Microsoft\Internet Explorer").GetValue("svcUpdateVersion")
If ieVersion Is Nothing Then
ieVersion = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("Software\Microsoft\Internet Explorer").GetValue("Version")
End If
If ieVersion IsNot Nothing Then
Version = ieVersion.ToString.Substring(0, ieVersion.ToString.IndexOf("."c))
Else
Throw New Exception("Joe, you got something going very wrong into your registry")
End If



.

The second step is to decide which version we wish to emulate.

To do this, we need to get an emulation code.

The codes values are the following

7000 This code specify to emulate IE7 (This is the default value for the WebBrowser)

8000 For IE8, IE9 and IE11, this code specify to emulate IE8 if the directive !DOCTYPE is present into the HTML code. If not present, it emulate IE7. ... For IE10, if the directive is present, it emulate IE10

8888 For all version of IE, regardless of the !DOCTYPE, IE8 will be emulate

9000 For IE9 and IE11, this code specify to emulate IE9 if the directive !DOCTYPE is present into the HTML code. If not present, it emulate IE7. ... For IE10, if the directive is present, it emulate IE10

9999 For all version of IE, regardless of the !DOCTYPE, IE9 will be emulate

10000 For all version of IE, if the directive !DOCTYPE is present into the HTML code IE10 will be emulate. If not present, IE7 will be emulate

10001 For all version of IE, regardless of the !DOCTYPE, IE10 will be emulate

11000 For all version of IE, if the directive !DOCTYPE is present into the HTML code IE11 will be emulate. If not present, IE7 will be emulate

11001 For all version of IE, regardless of the !DOCTYPE, IE11 will be emulate

(NOTE: when I say "all version", of course you cannot emulate a IE version not installed on the machine)

.

This code will look for the IE version installed on the machine and select the emulation code that is the more appropriate. The code throw if the IE version is older than IE7

Dim VersionCode As Integer
Dim Version As String = ""
Dim ieVersion As Object = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("Software\Microsoft\Internet Explorer").GetValue("svcUpdateVersion")
If ieVersion Is Nothing Then
ieVersion = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("Software\Microsoft\Internet Explorer").GetValue("Version")
End If
If ieVersion IsNot Nothing Then
Version = ieVersion.ToString.Substring(0, ieVersion.ToString.IndexOf("."c))
Select Case Version
Case "7"
VersionCode = 7000
Case "8"
VersionCode = 8888
Case "9"
VersionCode = 9999
Case "10"
VersionCode = 10001
Case else
If CInt(Version) >= 11 Then
VersionCode = 11001
Else
Throw New Exception("IE Version not supported")
End If
End Select
Else
Throw New Exception("Joe, you got something going very wrong into your registry")
End If



.

Now that we have our emulation code, the next step will be to create a registry key that specify which version of IE need to be emulate in our application.

Each application need to have its own key, you cannot set this for "All application"

Also, if you wish that the emulation to work when the application is attached to the debugger, you will need a key for the vshost.

.

This code will look for the IE version installed on the machine and select the code that is the more appropriate. Then it will look if the key is set for the emulation code selected. (Basically, the emulation code will not be set when: 1) The application runs for the first time. 2) If a new version of IE was installed). If the key is not set, it will set it and also set the key for the vshost.

Dim AppName As String = My.Application.Info.AssemblyName

Dim VersionCode As Integer
Dim Version As String = ""
Dim ieVersion As Object = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("Software\Microsoft\Internet Explorer").GetValue("svcUpdateVersion")
If ieVersion Is Nothing Then
ieVersion = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("Software\Microsoft\Internet Explorer").GetValue("Version")
End If
If ieVersion IsNot Nothing Then
Version = ieVersion.ToString.Substring(0, ieVersion.ToString.IndexOf("."c))
Select Case Version
Case "7"
VersionCode = 7000
Case "8"
VersionCode = 8888
Case "9"
VersionCode = 9999
Case "10"
VersionCode = 10001
Case else
If CInt(Version) >= 11 Then
VersionCode = 11001
Else
Throw New Exception("IE Version not supported")
End If
End Select
Else
Throw New Exception("Registry error")
End If
'Check if the right emulation is set
'if not, Set Emulation to highest level possible on the user machine
Dim Root As String = "HKEY_CURRENT_USER\"
Dim Key As String = "Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION"
Dim CurrentSetting As String = CStr(Microsoft.Win32.Registry.CurrentUser.OpenSubKey(Key).GetValue(AppName & ".exe"))
If CurrentSetting Is Nothing OrElse CInt(CurrentSetting) <> VersionCode Then
Microsoft.Win32.Registry.SetValue(Root & Key, AppName & ".exe", VersionCode)
Microsoft.Win32.Registry.SetValue(Root & Key, AppName & ".vshost.exe", VersionCode)
End If

.

Of course, on some machine, a security prompt may popup. So if this code is not in the application installer, but in the application itself, it would be good to notify the user before setting the new keys.



EDIT : Case "11" should in fact be Case Else ... obviously!!

Continue reading...
 
Back
Top