Click here to Skip to main content
Sign Up to vote bad
good
See more: C#2.0C#3.0C#4.0
Hi..
 
My application has a form with two web browser control on it,
I want zoom in/out facility on both of them.
 
So, How can i do that in c#..?
 
I found a link related this, but it's in VB.net
Using ExecWB with the native .NET 2.0 WebBrowser control[^]
 
Please help...
Posted 10 Oct '12 - 21:38
Edited 10 Oct '12 - 22:25


2 solutions

Hi,
 
You need to create your own control that inherits from webbrowser
 
 public partial class MyBrowser : WebBrowser
 
and use the IWebBrowser2 interface
(code here: http://www.pinvoke.net/default.aspx/Interfaces/IWebBrowser2.html[^])
 

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;
}
 
Then just add a Zoom method
 public void Zoom(int factor)
{
    object pvaIn = factor;
    try
    {
        this.axIWebBrowser2.ExecWB(OLECMDID.OLECMDID_OPTICAL_ZOOM,
           OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER,
           ref pvaIn, IntPtr.Zero);
    }
    catch (Exception)
    {
        throw;
    }
}
 
you can find the enumns values in pinvoke

[^]
 
http://www.pinvoke.net/search.aspx?search=OLECMDEXECOPT&namespace=[All][^]
 
Hope it helps.
 
Valery.
  Permalink  
Comments
Mayur_Panchal - 11 Oct '12 - 9:27
Thanks for the solution.. It works great.. I must appreciate this. Thanks again.
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;
        }
        
        /// <summary>
        /// Changes browser text size .
        /// </summary>
        /// <param name="textSize"></param>
        private void ChangeTextSize(int textSize)
        {
            try
            {
                this.axIWebBrowser2.ExecWB(OLECMDID.OLECMDID_ZOOM,
                     OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER,
                     textSize, IntPtr.Zero);
            }
            catch (Exception)
            {
                throw;
            }            
        }
 
        // source : http://support.microsoft.com/kb/304103
        // the valid range for the zoom level is 0 through 4, 
        // in which 0 is smallest and 4 is largest
        private const int MIN_TEXT_SIZE = 0;
        private const int MAX_TEXT_SIZE = 4;
        private const int DEFAULT_TEXT_SIZE = 2;
        
        /*
        /// <summary>
        /// Returns the current size of text .
        /// NOTE : DOES NOT WORK ! Always returns zero !
        /// </summary>
        /// <returns></returns>
        private int GetTextSize()
        {           
            object pvaIn = 0;
            this.axIWebBrowser2.ExecWB (OLECMDID.OLECMDID_ZOOM, 
            OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER, IntPtr.Zero, pvaIn );
            return (int)pvaIn;
        }
        */
      
        /// <summary>
        /// Returns True if text size can be increased .
        /// </summary>
        public bool CanZoomIn
        {
            get { return textSize < MAX_TEXT_SIZE; }
        }
 
        /// <summary>
        /// Returns True if text size can be decreased .
        /// </summary>
        public bool CanZoomOut
        {
            get { return textSize > MIN_TEXT_SIZE; }
        }
        /// <summary>
        /// Current text size ( default is 2 ) .
        /// </summary>
        private int textSize = DEFAULT_TEXT_SIZE;
 
        /// <summary>
        /// Increases text size . 
        /// </summary>
        public void ZoomIn()
        {            
            if (textSize < MAX_TEXT_SIZE)
            {
                textSize++;
                this.ChangeTextSize(textSize);
            }
        }
 
        /// <summary>
        /// Decreases text size .
        /// </summary>
        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
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 OriginalGriff 325
1 Sergey Alexandrovich Kryukov 140
2 Mohammed Hameed 123
3 Santhosh G_ 113
4 Ron Beyer 99
0 Sergey Alexandrovich Kryukov 8,286
1 OriginalGriff 6,561
2 CPallini 3,533
3 Rohan Leuva 2,703
4 Maciej Los 2,234


Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 11 Nov 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid