We have a WinForms app for the classic .NET Framework 4.7. One of the forms contains an instance of the WebBrowser control to render HTML documents. It turned out that if someone changes the Text Size option in Microsoft Help Viewer in Visual Studio
, this also affects the size of text in our WebBrowser-based viewer.
I suspect it is a global setting for the MSIE rendering engine the WebBrowser control is based on. As such, there can be other apps in which the user may change this setting and this will affect our app.
Is there a way to completely ignore this setting while rendering our HTMLs? We tried to specify explicit text sizes for the HTML tags in our HTML pages, but it seems, this does not help. It looks like the MSIE HTML renderer applies its scale factor after pages were rendered using the specified text sizes.
If it is not possible to ignore that global setting, is there an API we can use to control this Text Size parameter from our app? We could use it to provide settings like Microsoft Help Viewer does, and at least could provide the user with a similar choice list to adjust text size for comfort reading.
If possible, provide a solution in C# or VB.NET.
An example about synchronizing the WebBrowser Control's viewport scale, both Zoom and Text Size, to the settings applied by other applications (e.g., Internet Explorer or, as in this case, Help Viewer).
The Problem:
Expected Behavior:
In the sample code, a specialized class,
WebBrowserHelper
, contains a method that reads from the Registry the current Zoom and FontSize settings, to update the MenuItems to the current value:The
ZoomFactor
Key stores Zoom levels multiplied by1000
: e.g.,150%
Zoom has a value of150000
,75%
Zoom has a value of75000
The
WebBrowserHelper
'sSetZoom()
method uses the WebBrowser ActiveX instance to set the Zoom level, calling its ExecWb method, passing asOLECMDID
argument theOLECMDID_OPTICAL_ZOOM
value, theOLECMDEXECOPT
argument is set toOLECMDEXECOPT_DONTPROMPTUSER
and thepvaIn
argument (the Zoom value) is set to the Integer value of the Zoom level specified.▶ This also updates the
ZoomFactor
Registry Key.The
IEFontSize
andIEFontSizePrivate
Keys store a binary value, in the rage[0-4]
. These values loosely correspond to thex-small
,small
,medium
,large
,x-large
CSS settings.The
FontSize
scale is applied to the Document Body, using a multiplier:[Value + 1] * 4
.The
SetTextScale()
method uses the WebBrowser ActiveX instance to set the FonSize scale, calling itsExecWb()
method, passing asOLECMDID
argument theOLECMDID_ZOOM
value, theOLECMDEXECOPT
argument is set toOLECMDEXECOPT_DONTPROMPTUSER
and thepvaIn
argument (the Font scale value) is set to an Integer value in the range[0, 4]
as specified.▶ This also updates the
IEFontSize
Registry Key.Another option is to reset both Zoom and FontSize to default values before a WebBrowser session is started.
You can call the
WebBrowserHelper.InternetResetZoomAndFont()
method. Setting both arguments totrue
, it will reset the Zoom level to100%
and FontSize toMedium
.▶ Here, I'm adding some ToolStripMenuItems that allow to set the Zoom value and FontSize - which is set to a pre-defined scale - as applied by Internet Explorer 11.
zoomMenuItems
. All use the sameClick
event handler,zoomToolStripMenuItems_Click
textMenuItems
. All use the sameClick
event handler,textToolStripMenuItems_Click
Tag
property of each ToolStripMenuItem (you can of course use any other means to associate a value to a specific ToolStripMenuItem)browserViewSettings
Assume the WebBrowser Control is named
webBrowser1
:WebBrowserHelper
class:This is how it works: