Solved

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

Posted on 2014-01-28
15
2,360 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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 28

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 28

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 28

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 28

Expert Comment

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

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 28

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 28

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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

726 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