C# Windows.Forms.WebBrowser scaling

雨燕双飞 提交于 2019-12-08 06:22:12

问题


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


回答1:


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

  • OLECMDID
  • OLECMDEXECOPT
  • ShDcVw.InternetExplorer
  • ExecWB method
  • IE Architecture, IE Architecture 2
  • IE Hosting and Re-Use
  • Interop with Unmanaged Code
  • COM Interop Architecture
  • Introduction to COM Interop (VB)
  • Importing Type Libraries as Assemblies
  • COM To .Net Datatypes

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.



来源:https://stackoverflow.com/questions/48047110/c-sharp-windows-forms-webbrowser-scaling

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!