I had the same issue and while googleing I found this page.
I tried the code posted by Valery : it was very useful (definetely a major hint, thank you Valery!), though it did not exactly was I was after .
After more googleing I found this link :
http://support.microsoft.com/kb/304103 .
It helped indeed, even though the code is in VB.NET .
Basically, the trick was to use
OLECMDID.OLECMDID_ZOOM instead of
OLECMDID.OLECMDID_OPTICAL_ZOOM .
So I played a little and ended up with this.
Works like a charm.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SHDocVw;
namespace ColorCodeTest
{
public partial class MyBrowser : System.Windows.Forms.WebBrowser
{
private IWebBrowser2 axIWebBrowser2;
protected override void AttachInterfaces(
object nativeActiveXObject)
{
base.AttachInterfaces(nativeActiveXObject);
this.axIWebBrowser2 = (IWebBrowser2)nativeActiveXObject;
}
protected override void DetachInterfaces()
{
base.DetachInterfaces();
this.axIWebBrowser2 = null;
}
private void ChangeTextSize(int textSize)
{
try
{
this.axIWebBrowser2.ExecWB(OLECMDID.OLECMDID_ZOOM,
OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER,
textSize, IntPtr.Zero);
}
catch (Exception)
{
throw;
}
}
private const int MIN_TEXT_SIZE = 0;
private const int MAX_TEXT_SIZE = 4;
private const int DEFAULT_TEXT_SIZE = 2;
public bool CanZoomIn
{
get { return textSize < MAX_TEXT_SIZE; }
}
public bool CanZoomOut
{
get { return textSize > MIN_TEXT_SIZE; }
}
private int textSize = DEFAULT_TEXT_SIZE;
public void ZoomIn()
{
if (textSize < MAX_TEXT_SIZE)
{
textSize++;
this.ChangeTextSize(textSize);
}
}
public void ZoomOut()
{
if (textSize > MIN_TEXT_SIZE)
{
textSize--;
this.ChangeTextSize(textSize);
}
}
}
}
I was not able to retrieve the current text size as explained in the link above ( see the commented
private int GetTextSize() ) but this is not a big issue :
the browser default text size is 2, so I just use a private variable with this value as default .
I placed two buttons in a form :
btnZoomIn and
btnZoomOut ,
which hardly need any explanation .
They are disabled when the maximum or minimum text size is reached .
(
wbrCode is an instance of
MyBrowser class ).The code is :
private void btnZoomIn_Click(object sender, EventArgs e)
{
wbrCode.ZoomIn();
RefreshZoomButtons();
}
private void btnZoomOut_Click(object sender, EventArgs e)
{
wbrCode.ZoomOut();
RefreshZoomButtons();
}
private void RefreshZoomButtons()
{
btnZoomIn.Enabled = wbrCode.CanZoomIn;
btnZoomOut.Enabled = wbrCode.CanZoomOut;
}
HTH
Jack Griffin