简体   繁体   English

C#Windows.Forms.WebBrowser缩放

[英]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). 我的Windows窗体应用程序中有一个WebBrowser控件,并且想要更改正在加载的HTML页面的“缩放级别”(在我的情况下为Bing地图)。

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). 我希望找到在“文档”属性级别执行此操作的方法,但没有可使用的zoom或height / width / size属性(在浏览器级别,但我不想调整控件本身的大小) 。

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. 您要添加对Microsoft Internet控件的COM引用,以便可以访问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. 200代表缩放等级。EG200%缩放。 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. 如果您进行了50%的缩放,那将是缩小。换句话说,小于100的值表示缩小,而大于100的值表示放大。可能的值在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. 不幸的是,许多COM组件是为C ++开发人员而不是C#开发的,因为COM是围绕二进制兼容性的C ++范例。 And thus in C# we can interop with these COM objects that were originally written in C++. 因此,在C#中,我们可以与最初用C ++编写的这些COM对象进行互操作。

The other trick you have to remember about COM is that each time new functionality is added, it gets added to a new interface. 关于COM,您还必须记住的另一招是,每次添加新功能时,都会将其添加到新接口中。 EG IHTMLDocument2 IHTMLDocument3 , IHTMLDocument4 , etc. So you need to know which interface you actually want to cast your COM object to. EG IHTMLDocument2 IHTMLDocument3IHTMLDocument4等。因此,您需要知道将COM对象实际投射到哪个接口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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