简体   繁体   中英

C# Windows.Forms.WebBrowser scaling

I have a WebBrowser control in my Windows Forms app and want to change the "zoom level" of the HTML page I am loading (in my case Bing map).

I expected to find ways to do this at the 'Document' property level, but there is no zoom or height/width/size property to play with (there is at the browser level but I don't want to resize the control itself).

Attached are pics of what I want to do. Any thoughts? Thanks.

Browser zooming issue

Jimi is basically right. But I will go ahead and give you the full code/explanation.

You want to add a COM reference to Microsoft Internet Controls so you have access to ShDocVw.

using System;
using System.Windows.Forms;

namespace winforms
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            webBrowser1.Navigate(new Uri("http://www.google.com"));
            webBrowser1.DocumentCompleted += WebBrowser1_DocumentCompleted;
        }

        private void WebBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            var browser = webBrowser1.ActiveXInstance as SHDocVw.InternetExplorer;
            browser.ExecWB(SHDocVw.OLECMDID.OLECMDID_OPTICAL_ZOOM, SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT,200 ,IntPtr.Zero );
        }
    }
}

The 200 represents the zoom level.EG 200% zoom. If you did 50% zoom, that would be zooming out.In other words values less than 100 mean zooming out, and values greater than 100 are zooming in. Possible Values range from 10-1000.

Documnetation Links

Unfortunately, many of the COM components are documented for C++ developers not C# as COM is a C++ paradigm around binary compatibility. And thus in C# we can interop with these COM objects that were originally written in C++.

The other trick you have to remember about COM is that each time new functionality is added, it gets added to a new interface. EG IHTMLDocument2 IHTMLDocument3 , IHTMLDocument4 , etc. So you need to know which interface you actually want to cast your COM object to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM