Solved

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

Posted on 2014-01-28
15
2,152 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
 
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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 …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now