Solved

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

Posted on 2014-01-28
15
2,297 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Sql server insert 13 43
Optimize the query 5 49
System.net.Sockets Error 5 38
vb.net, web page, SQL holding the selected data in an accessible variable 7 32
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

679 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