Solved

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

Posted on 2014-01-28
15
2,227 Views
Last Modified: 2014-02-12
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?
0
Comment
Question by:Dalexan
  • 7
  • 6
  • 2
15 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39815451
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?
0
 

Author Comment

by:Dalexan
ID: 39815463
Not an editor, just a viewer.  I will look into this and respond
0
 

Author Comment

by:Dalexan
ID: 39815825
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)?
0
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39816321
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.
0
 
LVL 27

Expert Comment

by:Ark
ID: 39820186
    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)
0
 
LVL 27

Expert Comment

by:Ark
ID: 39820222
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

0
 

Author Comment

by:Dalexan
ID: 39836985
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?
0
 
LVL 27

Expert Comment

by:Ark
ID: 39838196
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
0
 

Author Comment

by:Dalexan
ID: 39840273
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?
0
 
LVL 27

Expert Comment

by:Ark
ID: 39841300
Did you try reflection approach?
0
 
LVL 27

Expert Comment

by:Ark
ID: 39841308
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

0
 

Author Comment

by:Dalexan
ID: 39842391
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)
0
 
LVL 27

Accepted Solution

by:
Ark earned 500 total points
ID: 39843785
    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 dispfontname As String,
                              ByVal dispfontsize As Integer,
                              ByVal dispfontcolor As Color)
        WebBrowser1.Document.Body.Style = "font-family=" + dispfontname + ";"
        WebBrowser1.Document.ForeColor = dispfontcolor
        Dim zoom As Integer = (dispfontsize / 2 - 3) * 50
        WebBrowser1.ActiveXInstance.ExecWB(OLECMDID_OPTICAL_ZOOM, _
        OLECMDEXECOPT_DONTPROMPTUSER, zoom - 0, Nothing)
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ResizeBrowser("Verdana", 14, Color.Red)
    End Sub

Open in new window

0
 

Author Closing Comment

by:Dalexan
ID: 39848456
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 :-)
0
 
LVL 27

Expert Comment

by:Ark
ID: 39852618
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!
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question