How can I get the WebBrowser control to show modern contents? - c#

I've created a Winforms app that uses a WebBrowser control; I dynamically assign its Uri. It worked fine for awhile, but now I'm getting this msg:
You seem to be using an unsupported browser. Older browsers can put your security at risk, are slow and don't work with newer Google Maps features. To access Google Maps, you'll need to update to a modern browser.
The last two words are a link, and following that link, I see:
You are currently using...
IE 11
So, okay, the WebBrowser component uses IE 11; how can I change that?
My machine is set to use Chrome as its browser; perhaps the control should use whatever your current browser is? I don't know if that's possible/feasible.
UPDATE
Okay, I'm willing to give Reza's suggestion a try. But when I navigate to the specified spot in regedit, and right-click in the right pane to add a New entry, it has three options:
Key, String Value, Binary Value
I reckon the string values are the ".exe" strings, and the Binary values are the "dword" vals, but what should the "Key" values be?

Note: The post is about WebBrowser control, however, for all the new
.NET projects the main solution is using
WebView2.
To learn more, take a look at this post:
Getting started with WebView2.
WebBrowser Control
The WebBrowser control uses the same Internet Explorer version which is installed on your OS but it doesn't use the latest document mode by default and shows content in compatibility mode.
Symptom - As a symptom, the site works properly in Internet Explorer or other browsers, but WebBrowser control doesn't show the site well and for some sites it shows script errors.
Solution - You can tell the WebBrowser control to use the latest document mode without compatibility mode in WebBrowser control. You can follow instructions here to disable the setting using registry.
[Reference: Browser Emulation]
Apply Browser Emulation setting using code
If you want to apply the settings using code, run the following code once:
using (var key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(
#"Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION",
true))
{
var app = System.IO.Path.GetFileName(Application.ExecutablePath);
key.SetValue(app, 11001, Microsoft.Win32.RegistryValueKind.DWord);
key.Close();
}
In above code, I've used 11001 which means IE11 Edge mode.
Internet Explorer 11. Webpages are displayed in IE11 edge mode,
regardless of the declared !DOCTYPE directive. Failing to declare a
!DOCTYPE directive causes the page to load in Quirks.
Apply the Browser Emulation setting manually
Open Registry editor and browse HKEY_CURRENT_USER, go to the following key:
Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION
Add the following values:
"YourApplicationFileName.exe"=dword:00002af9
"YourApplicationFileName.vshost.exe"=dword:00002af9
(In older versions of Visual Studio you needed to add vshost.exe value as well, when you run your program in Visual Studio.)
To create entries right click on an empty area of the right pane, then in the window which appears after selecting dword value, choose hexadecimal and enter 2af9:
In above steps, I've used 11001 which means IE11 Edge mode.
Use WebViewCompatible Control for Windows Forms
You can also use the new WebViewCompatible control for Windows Forms. You can see simple steps to use here: Replace WebBrowser control by new WebView Compatible control for Windows Forms.
WebViewCompatible uses one of two rendering engines to support a broader set of Windows clients:
On Windows 10 devices, the newer Microsoft Edge rendering engine is used to embed a view that renders richly formatted HTML content from a remote web server, dynamically generated code, or content files.
On devices running older versions of Windows, the System.Windows.Controls.WebBrowser is used, which provides Internet Explorer engine-based rendering.
Note: WebView2 is a replacement for WebView and WebViewCompatible.
Set X-UA-Compatibile meta tag
In case that you have access to the html content of the page and you can change the content (for example it's a local html file, or the site belong to yourself) then you can set X-UA-Compatibile meta tag in the head like: <meta http-equiv="X-UA-Compatible" content="IE=Edge" />.
Use other Browser Controls
You can rely on other browser controls like CefSharp.

In my case for embedded custom protocol on an application, I will allow only to browse pages served by the application, and no content from the outside, so I wanted to skip saving to the Windows Registry. When I tested after following Reza Aghaei answer and found that you can change the compatibility mode from within the content page. This will skip the need to configure a registry key, but you will have to add it to every page.
For changing the compatibility mode of a page, you must add a meta tag for it to be applied by the rendering engine:
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
</head>
<body>
...
</body>
</html>

The below procedures will add the correct key and remove it again.
Call the CreateBrowserKey upon loading the form that your web browser is in.
Then when closing the form, call the RemoveBrowserKey
Private Sub CreateBrowserKey(Optional ByVal IgnoreIDocDirective As Boolean = False)
' Dim basekey As String = Microsoft.Win32.Registry.CurrentUser.ToString
Dim value As Int32
' Dim thisAppsName As String = My.Application.Info.AssemblyName & ".exe"
' Value reference: http://msdn.microsoft.com/en-us/library/ee330730%28v=VS.85%29.aspx
' IDOC Reference: http://msdn.microsoft.com/en-us/library/ms535242%28v=vs.85%29.aspx
Select Case (New WebBrowser).Version.Major
Case 8
If IgnoreIDocDirective Then
value = 8888
Else
value = 8000
End If
Case 9
If IgnoreIDocDirective Then
value = 9999
Else
value = 9000
End If
Case 10
If IgnoreIDocDirective Then
value = 10001
Else
value = 10000
End If
Case 11
If IgnoreIDocDirective Then
value = 11001
Else
value = 11000
End If
Case Else
Exit Sub
End Select
Microsoft.Win32.Registry.SetValue(Microsoft.Win32.Registry.CurrentUser.ToString & BrowserKeyPath, _
Process.GetCurrentProcess.ProcessName & ".exe", _
value, _
Microsoft.Win32.RegistryValueKind.DWord)
End Sub
Private Sub RemoveBrowserKey()
Dim key As Microsoft.Win32.RegistryKey
key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(BrowserKeyPath.Substring(1), True)
key.DeleteValue(Process.GetCurrentProcess.ProcessName & ".exe", False)
End Sub

The C# WebBrowser class is a basically a IE wrapper and because of this it cannot be changed.
See this link:
The WebBrowser control is a managed wrapper around a component installed with Internet Explorer.
For alternatives you can check out
WebKit.NET
GeckoFX

Related

div innerHtml string after null character ignored in WebBrowser control but not in IE8

Background: Our application before runs in Windows 7 and uses IE 8 as the browser to display HTML. But when we upgraded to Windows 10, we cannot use IE 8 anymore, so we created a WinForms application hosting a WebBrowser control to display the HTML.
Problem: We noticed that some string information is not displayed anymore when using the WebBrowser control and found out that the string which has a null character in the middle will discard the succeeding characters. In IE 8, the null was just ignored and it was able to display the whole string. I created a simple HTML to replicate the issue:
<HTML>
<TITLE>TEST</TITLE>
<HEAD>
<script language="VBScript">
Dim str
Sub Window_OnLoad()
str= "<TABLE >" & _
"<TR ><TD >0</TD></TR>" & _
"<TR ><TD >1" & chr(&H0) & "</TD></TR>" &_
"<TR ><TD >2</TD></TR><TR ><TD >3</TD></TR></TABLE>"
MsgBox str
document.all.mydiv.innerHtml = str
End Sub
</script>
</HEAD>
<BODY >
<div id="mydiv">
</div>
</BODY>
</HTML>
Output (IE 8):
0
1
2
3
Output (WebBrowser control):
0
1
In our application, the 2nd data retrieved (in the example above "1") that is set to the table element contains null after it, and what happens in the WebBrowser control is that the data after the null are discarded, thus not being able to print the whole data on the table. However, in IE8, it just seems to ignore the null and display the whole data on the table. Is there some way to make the WebBrowser control behave like in IE 8?
Solutions tried\Investigation results:
We already set the Browser Emulation correctly so that the web browser will behave like IE 8 but it still not working. (tried both 8000 and 8888)
We tried to use meta X-UA-Compatible instead of the Browser Emulation, but still not working. (tried both "IE=8" and "IE=EmulateIE8")
We tried setting all kinds of DOCTYPE but it also doesn't work.
In the sample code above I put a MsgBox to see the data to be set in the inner HTML and IE8 did not alert the data after the null. It was only able to ignore the null when setting it to the inner HTML. Could this be a special behavior of the inner HTML in IE?
I don't know if necessary but I tried to translate the code in javascript and still got the same results for both IE8 and WebBrowser control.
While it was clear from the question that document mode was considered, what was overlooked is that CASE matters when moving to a newer document mode. Also, it was stated that the output was complete (0,1,2,3) with IE 8, but was incomplete (0,1) using the WebBrowser control.
We can simplify testing by running the page as an HTA. When doing that, we can see that in IE=8 mode (and lower) the output is (0,1) and in IE=9 (or IE=10) mode the output is (0,1,2,3).
The case problem in the original code is this line:
Sub Window_OnLoad()
It should be:
Sub window_onload
Specifically, Window must be window (all lowercase) in IE=9 or higher modes. The case of "onload" and the unnecessary parenthesis make no functional difference, but have been changed to their standard formats. Please note that calling a sub from an element in IE=9 or higher mode requires parenthesis but the Sub itself does not need them when there are no parameters to be passed.
Here's a minimal version of the code that outputs (0,1,2,3) and ignores the null character:
Test.hta
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=9">
<script language="VBScript">
Sub window_onload
d1.innerHTML = "<table><tr><td>0</td></tr><tr><td>1" & chr(&H0) & "</td></tr><tr><td>2</td></tr><tr><td>3</td></tr></table>"
End Sub
</script>
</head>
<body>
<div id=d1></div>
</body>
</html>
In the code above, if IE=9 is changed to IE=8 the output will be (0,1).

WPF WebBrowser not render external html content properly

In WPF application the WebBrowser control doesn't render external html url properly.
While source assigned with external url, page get loaded.
But html hover effects & svg animations are not working.
Stuck at this point (VisualStudio 2015 environment).
Help me to step up.
Thanks in advance.
<StackPanel>
<WebBrowser Source="https://www.amcharts.com/demos/simple-pie-chart/"
OverridesDefaultStyle="False" MaxHeight="500"/>
</StackPanel>
If I understand correctly, you can not modify the HTML content of the web page.
You can use the following trick instead:
In registry:
Go to HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION for 64-bit or 32-bit only machines.
Create a new DWORD key, and name it the name of your application e.g. "myapp.exe" and then edit the value of the key. There are many different values you can add depending on the IE version you want to emulate. I entered 11001 (as a decimal value - 0x2AF9 in HEX) which emulates IE 11 (many more values located at: http://msdn.microsoft.com/en-us/library/ee330730%28v=vs.85%29.aspx#browser_emulation).

WPF WebBrowser Control will not load video in a locally hosted HTML page

I have a WPF application written in C#. This application has two WPF WebBrowser controls next to each other. Each one navigates to a different locally-hosted HTML file. Each HTML file has a video element which points to an .mp4 video file stored in the same directory.
My problem is that the video content does not render in the WebBrowser controls. If I use the exact same URL and put it into Internet Explorer 11 on the same PC, the video content runs perfectly fine. I know the actual navigations of the WebBrowser controls are working because the page half-renders. I can see the background color show correctly but that is it. If I right-click inside one of the controls and view the source, I can see the correct HTML there.
Attempting to apply the recommendation in an answer to this question to Internet Explorer 11, I tried having the WebBrowser controls in my executable run like IE11 using the information in this link with no success.
I'm not sure if this is relevant, but I already have "Allow active content to run in files on My Computer" checked in Internet Options on Internet Explorer.
Here is the HTML markup for the video:
<div id="video_holder">
<video id="video" width="1200" height="900" autoplay>
<source src=".\FANCY.mp4" type="video/mp4">
</video>
</div>
And here is the code that calls the Navigate() method of the WebBrowser controls (the constructor for this Form receives the urls from another class - I'm pretty sure the problem isn't here, though, as I can see that the actual navigation seems to be working and the HTML starts to render):
public partial class TwoPaneWindow : Window
{
private string leftUrl;
private string rightUrl;
public TwoPaneWindow(string left, string right)
{
leftUrl = left;
rightUrl = right;
}
public void StartWindow()
{
InitializeComponent();
this.Show();
LeftBrowser.Navigate(leftUrl);
RightBrowser.Navigate(rightUrl);
}
}
Can anyone tell what is going on? I am hosting this on IIS installed on Windows 8.1. Thank you in advance for any help you can offer.
Finally figured this out. Even though I said I tried the answer to this question, I was actually using the FEATURE_BROWSER_EMULATION key incorrectly. I attempted to add FEATURE_BROWSER_EMULATION to HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl, when in fact it needs to be modified at HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION. Once I created a new DWORD entry for my application (sharing its name) with a value of 0x2AF8, the WebBrowser controls immediately started working correctly. A newbie mistake I'm sure, but hopefully this helps someone else in the future.

Windows phone webbrowser Control is Blank?

Im trying to open a URL(Some link) in Webbrowser Control.
The link return a html page which contain Google Graph , but my Webbrowser Control is Blank and dont display any thing on it. It works fine on WebBrowserTask and on my pc so their is no problem in this link but it is blank on webBrowser Control Any Idea How i can Do this ??
public GraphPage()
{
InitializeComponent();
webBrowser1.Navigated += new EventHandler<System.Windows.Navigation.NavigationEventArgs>(Browser_Navigated);
webBrowser1.Navigating += new EventHandler<NavigatingEventArgs>(Browser_Navigating);
loadPage(getBaseUrl(graphType));
}
private void loadPage(String url )
{
webBrowser1.IsScriptEnabled = true;
webBrowser1.Source = new Uri("Link");
}
As mentioned by user112553, set IsScriptEnabled true. Can be done under the XAML-code or in the code-behind with
XAML
<phone:WebBrowser x:Name="Browser" IsScriptEnabled="True" />
Code-Behind
Browser.IsScriptEnabled = true;
I encountered a similar situation, with Windows Phone 8 and a HTML page using JQuery.
IsScriptEnabled=true wasn't enough (the page didn't render properly).
I solved adding to the html page a
doctype declaration:
<!DOCTYPE html>
<html>
...
Seems like the WebBrowser component refuses to render HTML5 pages without explicit defining the document-type.
Since it's a common problem with rendering pages in IE<11 when not defining this tag, the cause of why my scripts didn't run could be many and most likely a reference to a HTML5 tag which was not handled correctly.
ref: http://msdn.microsoft.com/en-us/hh779632.aspx
Since windows phone 8.0 is based on Internet Explorer 10, it makes sense, the confusing part with debugging this behavior is that Internet Explorer on your phone renders the page perfectly. Still the WebBrowser component will not.
If this is documented in the API specifications, it should be easier to find, because I was not able to find any information that would point me to this solution at all, this would be mostly because my pages was rendered in WebViews for Android and IOS without any problems.
Thanks to Antonio Pelleriti for providing this solution.

How Internet Explorer Prepare Print Preview window

I am wondering how Internet Explorer, Mozilla Firefox or any other browser generate print preview window of an web page loaded into the browser.
The preview image will have various changes such as banners and adv are removed, will have white background and black text and etc.
We would like implement similar print preview window using C# WebBrowser control and i don't want to use default browser Print preview feature such as ExecWB command or any other.
Please give us some light on this.
Thanks,
Ramanand Bhat.
You could try to alter the styles by accessing and modifying the HTMLDocument LINK elements.
HtmlDocument document = WebBrowser1.Document;
foreach (HtmlElement element in document.GetElementsByTagName("LINK"))
{
string cssMedia = element.GetAttribute("Media");
if (cssMedia == "print")
element.SetAttribute("Media", "screen"); //sets print styles to display normally
else
element.SetAttribute("Media", "hidden"); //hides normal styles
}
This will change your print-styles to display in screen view (i.e. as a normal stylesheet without having to use the print-preview window) and your screen-styles to not be shown (as they don't have a Media type of screen anymore)
This is sample code so doesn't have any error checking. It might also have some syntax errors but it should be a start to achieve your goal.
To print a screen you need to set up a call to window.print() in javascript.
Print screen
It will then use whatever css you have assigned as 'print' in the page to render the page as a preview
As far as I know, the banners, advertisements, et cetera are not removed by the browser during a print preview. CSS governs the appearance when the media is print.

Categories

Resources