Reputation: 37
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.
Upvotes: 1
Views: 5446
Reputation: 2397
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. E.G. IHTMLDocument2 IHTMLDocument3, IHTMLDocument4, etc. So you need to know which interface you actually want to cast your COM object to.
Upvotes: 5