Click here to Skip to main content
15,896,730 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Updated 11-Mar-16 12:31pm
v2

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.

C#
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 :
C#
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
 
Share this answer
 
v2
I found this solution here: winforms - Zoom in on a web page using WebBrowser .NET control - Stack Overflow[^]

By this guy:
[QHNPROF]

Whom proposes this ellegant solution:

C#
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        webBrowser1.Document.Body.Style = "zoom:50%";
    }
 
Share this answer
 
Comments
Member 9528219 5-Jul-16 3:52am    
Solution 3 works well for me and so simple.
Hi,

You need to create your own control that inherits from webbrowser

C#
public partial class MyBrowser : WebBrowser


and use the IWebBrowser2 interface
(code here: http://www.pinvoke.net/default.aspx/Interfaces/IWebBrowser2.html[^])


C#
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
C#
 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.
 
Share this answer
 
Comments
Mayur Panchal 11-Oct-12 9:27am    
Thanks for the solution..

It works great..
I must appreciate this.


Thanks again.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900