Link to home
Start Free TrialLog in
Avatar of Dalexan
DalexanFlag for Afghanistan

asked on

How can I change the font size of a vb2010 webbrowser control

We have a Webbrowser control embedded in a VB2010 app to display html files, and would like to allow the user to change the appearance of the displayed content by providing menu items to change the font type and size.  We are using the following line to set the options:

WebBrowser1.Document.Body.Style = "font-family" & FontName & ";font-size=" & FontSize & "px;"

This works fine for changing the font family using "Ariel", "Courier", etc. but nothing changes when setting the font size.  I've tried "font-size=8px;", I've tried using 0-5 as in "font-size=1;", but nothing works.

Anyone have any idea what to do to change the current font?
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Trouble with setting size is that each element on the page has its own size (usually). What you can do is zoom in/out the page to change the size of whole page. But this requires inheriting the webbrowser control to add this zooming capability. Example here

http://www.codeproject.com/Questions/474489/ZoomplusinplusWebplusBrowserplusControlpluswithplu


Are you using webbrowser as a WYSIWYG editor?
Avatar of Dalexan

ASKER

Not an editor, just a viewer.  I will look into this and respond
Avatar of Dalexan

ASKER

I followed this to http://support.microsoft.com/kb/304103 and found the reference to ExecWB Zoom command available within WebBrowser controls...except it is not available in mine under vb2010.  Any idea how to make it available (if at all)?
ExecWB is not exposed via WebBrowser as far as I know. You have to inherit from the underlying object to have access to this method.
    Private Const OLECMDID_OPTICAL_ZOOM As Integer = 63
    Private Const OLECMDEXECOPT_DONTPROMPTUSER As Integer = 2


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        SetZoom(4)
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        WebBrowser1.Navigate("www.google.com")
    End Sub

    Private Sub SetZoom(ByVal zoom As Integer)
        Dim wb = WebBrowser1.ActiveXInstance.GetType()

        Dim o As Object = zoom * 100

        wb.InvokeMember("ExecWB",
                        System.Reflection.BindingFlags.InvokeMethod,
                        Nothing,
                        WebBrowser1.ActiveXInstance,
                        New Object() {
                            OLECMDID_OPTICAL_ZOOM,
                            OLECMDEXECOPT_DONTPROMPTUSER,
                            o,
                            o
                            }
                        )
    End Sub

Open in new window

PS zoom can be from 0.1 (10% of original) to 10 (1000% of original)
Hmm... Found some strange behavior:
Dim o As Object = zoom * 100
'Following string through an exception - this is why I used reflection in above sample
WebBrowser1.ActiveXInstance.ExecWB(OLECMDID_OPTICAL_ZOOM, _
OLECMDEXECOPT_DONTPROMPTUSER, o, Nothing)
'Following string works!
WebBrowser1.ActiveXInstance.ExecWB(OLECMDID_OPTICAL_ZOOM, _
OLECMDEXECOPT_DONTPROMPTUSER, o - 0, Nothing)

Open in new window

Avatar of Dalexan

ASKER

This is not working for me...I do not have an ExecWB instance available to call.
I'm running VB2012 and the WebBrowser object in the toolbox is ver 4.0.0.0
All I get after typing WebBrowser1.ActiveXInstance. is a choice of Equals, GetHashCode, GetType, ReferenceEquals and ToString.

I also tried using Dim wb = ... and still get only the above choices, no ExecWB

Where would it be hiding?
WebBrowser1.ActiveXInstance is a COM object and it DOES have ExecWB method though VB.Net intellicense don't know about it.
 Equals, GetHashCode, GetType, ReferenceEquals and ToString is an options for any object
Avatar of Dalexan

ASKER

Still no dice.  I tried
  WebBrowser1.ActiveXInstance.ExecWB(63,2,Z,Nothing)
where z was set to 50, 100, 150, 200 and 250...nothing ever changed in the webbrowser box.

I also tried WebBrowser1.ActiveXInstance.ExecWB(19,2,Z,Nothing)
where z was set to 0 - 4...again no change.

Am I missing something like a property value or something?
Did you try reflection approach?
PS This sample works for me (default browser =Chrome)
    Private Const OLECMDID_OPTICAL_ZOOM As Integer = 63
    Private Const OLECMDEXECOPT_DONTPROMPTUSER As Integer = 2

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        WebBrowser1.Navigate("www.google.com")
    End Sub

    Private Sub ResizeBrowser(ByVal zoom As Integer)
        Dim o As Object = zoom * 100
        WebBrowser1.ActiveXInstance.ExecWB(OLECMDID_OPTICAL_ZOOM, _
        OLECMDEXECOPT_DONTPROMPTUSER, o - 0, Nothing)
    End Sub

    Private Sub WebBrowser1_Navigated(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserNavigatedEventArgs) Handles WebBrowser1.Navigated
        ResizeBrowser(4) '400%
    End Sub

Open in new window

Avatar of Dalexan

ASKER

Since I am writing to the WebBrowser1.Document, the Navigated event doesn't help...so I put the following in WebBrowser1_DocumentCompleted, but none of them work...

        WebBrowser1.Document.Body.Style = "font-family=" + dispfontname + ";"
        WebBrowser1.Document.ForeColor = dispfontcolor
        WebBrowser1.ActiveXInstance.ExecWB(63, 2, ((dispfontsize / 2) - 3) * 50, Nothing)
ASKER CERTIFIED SOLUTION
Avatar of Ark
Ark
Flag of Russian Federation image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Dalexan

ASKER

Not sure why, but it worked! Thanks!  I'm curious as to why this works, but I suspect it has something to do with subtracting zero from the font size.  I don't see the difference between calculating the value as I did with ((dispfontsize/2)-3)*50, or presetting zoom to this value and using zoom-0 in the call.  Near as I can tell, this is the only difference.
Anyway, Thanks for your help :-)
ExecWB require passing parameters as Variant (Object) ByRef. Unfortunatelly VB.Net haven't possibility to specify this when calling (like C# can 'ref zoom'). But when you specify expression as parameter (zoom-0) it does passing ByRef!