Using the WebBrowser control, how do I increase/decrease the text size of a web page (like how the text size of a web page can be altered in IE by navigating to the View menu & then selecting Text Size)?
Thanks,
Arpan
Printable View
Using the WebBrowser control, how do I increase/decrease the text size of a web page (like how the text size of a web page can be altered in IE by navigating to the View menu & then selecting Text Size)?
Thanks,
Arpan
Set the Body's style to include "Font-Size:xx" where xx is the new size.
As you'll see (even with IE), explicitly sized fonts do not change when you use IE's text size. Same will happen to your's in this case.
But how do I set the body's style to include the font size? I mean to say that such styles are coded in HTML, ASP documents something likeQuote:
Set the Body's style to include "Font-Size:xx" where xx is the new size.
So how do I do the same in VB? What's should the code be & where do I put the code?Code:<html>
<body>
<font size=3 face="Arial">
....................
....................
....................
</body>
</html>
Actually I have never done anything like this.
Thanks,
Arpan
If you are using the webbrowser control in vb you can get a refference to the html document object. this object exposes a "Body" object. Under the body object, you'll find the styles property. To these styles, add "font-size:15px;", or whatever size and unit you would like to set it to.
Here's a little sample I quickly made. You'll see that the explicitly sized text remain the same.
I have a full web browser that I spend a few years on, in VB6 using the WebBrowser control.
This is the method that IE uses (and I used) to change the text size.
VB Code:
Private Sub mnuTextSize_Click(Index As Integer) ' snip... brows.ExecWB OLECMDID_ZOOM, OLECMDEXECOPT_DONTPROMPTUSER, CLng(4 - Index), Null ' snip End Sub
Valid indices for the "Index" parameter being:
HTH you :)Code:0 - Largest
1 - Larger
2 - Medium
3 - Smaller
4 - Smallest
cool.
I just did it the simple way. For a quick fix...
Penagate, I tried out your code & it works fine but for a petty problem. The Text Size menu I am using is exactly the same that IE uses (under the View menu). This is how I did it:Quote:
I have a full web browser that I spend a few years on, in VB6 using the WebBrowser control.
This is the method that IE uses (and I used) to change the text size.
But what happens is as soon as the mouse is over the Text Size menu item, the text size of the web page becomes Largest (note that I haven't clicked the Largest sub-menu). How do I overcome this? I want that by default, the Text Size should be Medium.VB Code:
Private Sub [b]Form_Load()[/b] Call mnuTextSize_Click(2) End Sub Private Sub [b]mnuTextSize_Click(Index As Integer)[/b] wWeb.ExecWB OLECMDID_ZOOM, OLECMDEXECOPT_DONTPROMPTUSER, CLng(4 - Index), Null End Sub Private Sub [b]mnuLargest_Click()[/b] Call mnuTextSize_Click(0) End Sub Private Sub [b]mnuLarger_Click()[/b] Call mnuTextSize_Click(1) End Sub Private Sub [b]mnuMedium_Click()[/b] Call mnuTextSize_Click(2) End Sub Private Sub [b]mnuSmaller_Click()[/b] Call mnuTextSize_Click(3) End Sub Private Sub [b]mnuSmallest_Click()[/b] Call mnuTextSize_Click(4) End Sub
Thanks,
Arpan
I don't know where your "mnuTextSize" controls are if you already have "mnuSmallest" etc., but I just made the whole thing a control array. It was mnuTextSize(0 To 4) where the captions of each were as I listed above. There is no need to call anything on form_Load() because the default size is Medium anyway.
So my menu structure was:
where Increase and Decrease incremented and decremented a variable containing the current font size, respectively, and then called the mnuTextSize_Click() handler passing that variable as the Index parameter.Code:View
...
Text Size
Largest mnuTextSize(0)
Larger mnuTextSize(1)
Medium mnuTextSize(2)
Smaller mnuTextSize(3)
Smallest mnuTextSize(4)
-
Increase
Decrease
Penagate, as pointed out earlier, my menu is exactly identical to the Text Size menu item (under the View menu) in IE. Nevertheless, this is how my menu looks:Quote:
I don't know where your "mnuTextSize" controls are if you already have "mnuSmallest" etc., but I just made the whole thing a control array. It was mnuTextSize(0 To 4) where the captions of each were as I listed above. There is no need to call anything on form_Load() because the default size is Medium anyway.
Now what is happening is as soon as the mouse hovers over the Text Size menu, as expected, the sub-menu (Largest, Larger, Medium, Smaller & Smallest) opens up & then the text size becomes largest.Code:View
....Text Size mnuTextSize(0)
........Largest mnuLargest
........Larger mnuLarger
........Medium mnuMedium
........Smaller mnuSmaller
........Smallest mnuSmallest
So where am I going wrong?
Arpan
Oh - because when a submenu opens it fires the _Click event - because your "Text Size" menu has the index 0, it has the effect that you'd expect if you clicked "Largest". If you look at my menu structure you'll see that I named them differently, with each individual size having an index for its size. It doesn't work at all if the parent menu is part of the array. Just name the "Text Size" one mnuViewTextSize or something, and then each size is "mnuTextSize(x)" with x from 0 to 4.
Yeah Penagate, it's working now.Quote:
Oh - because when a submenu opens it fires the _Click event - because your "Text Size" menu has the index 0, it has the effect that you'd expect if you clicked "Largest". If you look at my menu structure you'll see that I named them differently, with each individual size having an index for its size. It doesn't work at all if the parent menu is part of the array. Just name the "Text Size" one mnuViewTextSize or something, and then each size is "mnuTextSize(x)" with x from 0 to 4.
One last question on this topic - I want that which is the current text size, only that size should be checked in the menu. For e.g. if the text size is smaller, then Smaller should be checked & the rest should be unchecked or if the text size is largest, then Largest should be checked & the rest should be unchecked. One way of doing this isBut this means that there will be many If...Else statements. Any other way to do this?VB Code:
Private Sub mnuTextSize_Click(Index As Integer) If (Index = 0) Then mnuTextSize(0).Checked = True mnuTextSize(1).Checked = False mnuTextSize(2).Checked = False mnuTextSize(3).Checked = False mnuTextSize(4).Checked = False ............... ............... ............... End If wWeb.ExecWB OLECMDID_ZOOM, OLECMDEXECOPT_DONTPROMPTUSER, CLng(4 - Index), Null End Sub
Thanks,
Arpan
OK, here's the complete unsnipped source of my handler routine that I should have provided in the first place :D
VB Code:
Private Sub mnuTextSize_Click(Index As Integer) For i = mnuTextSize.LBound To mnuTextSize.UBound If Not i = Index Then mnuTextSize(i).Checked = False Else mnuTextSize(i).Checked = True End If Next i brows.ExecWB OLECMDID_ZOOM, OLECMDEXECOPT_DONTPROMPTUSER, CLng(4 - Index), Null bSearch.ExecWB OLECMDID_ZOOM, OLECMDEXECOPT_DONTPROMPTUSER, CLng(4 - Index), Null SetAppSetting "TextZoom", 4 - Index End Sub
Of course, I wrote that a long time ago, and it could be simplified a bit.
VB Code:
Private Sub mnuTextSize_Click(Index As Integer) For i = mnuTextSize.LBound To mnuTextSize.UBound mnuTextSize(i).Checked = (i <> Index) Next i brows.ExecWB OLECMDID_ZOOM, OLECMDEXECOPT_DONTPROMPTUSER, CLng(4 - Index), Null SetAppSetting "TextZoom", 4 - Index End Sub
Penagate, where from is this SetAppSetting coming? It throws an error saying Sub or Function not defined. I tried searching this forum but my search yielded only 1 result. Guess which was that? This very post! Couldn't find anything else!
Arpan
Sorry about that :lol:
Basically it just stores the value in the registry. You can accomplish the same with a module-level variable.
VB Code:
Private fontSize As Long Private Sub Form_Load() fontSize = 2 ' medium ' ... End Sub Private Sub mnuTextSize_Click(Index As Integer) For i = mnuTextSize.LBound To mnuTextSize.UBound mnuTextSize(i).Checked = (i <> Index) Next i fontSize = (4 - Index) brows.ExecWB OLECMDID_ZOOM, OLECMDEXECOPT_DONTPROMPTUSER, fontSize, Null End Sub
Penagate, sorry to say that your code throws an error saying Member already exists in a object model from which this object model derives pointing to the Private fontSize As Long line. If I delete that line, then except for medium, the rest of the text sizes get checked! What's wrong now?
Arpan
Well, I have never seen that error before, but just try naming it something else - like textZoom or zoomIndex or something.
And change the line in the For loop to this
VB Code:
mnuTextSize(i).Checked = (i = Index)
Not my day is it :D
......but finally I have made your day (or is it the other way round??) :DQuote:
Not my day is it
It's working now but what was the problem with fontSize? I am more inquisitive because you haven't ever encountered that error!
Thanks,
Arpan
I have honestly no idea. Was that the exact wording? Because Google turned up nothing for that phrase.
I reproduced the exact error message....exactly as what VB threw.Quote:
I have honestly no idea. Was that the exact wording?
BTW, Penagate, just now I realized that though the different sizes are getting checked/unchecked correctly but the text size always remains medium. Checking the other 4 sizes doesn't make any difference to the text size! What could be the problem now?
Arpan
Thanks shirazamod this works perfectly!!!
penagate, your example is exactly the same line of code I've been trying. There is something else that I probably need for it to work.
Thanks
Brian
...&SizeQuote:
Originally Posted by arpan_de
......&Largest
......Lar&ger
......&Medium
......&Smaller
......Small&est
mnuLargest_Click()
WebBrowser1.Exec OLECMDID_ZOOM, OLECMDEXECOPT_DONTPROMPTUSER, CLng(4 - Index), Null