使用 WebBrowser .NET 控件放大网页

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/738232/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 12:30:04  来源:igfitidea点击:

Zoom in on a web page using WebBrowser .NET control

.netwinformswebbrowser-control

提问by Jon B

In IE7 there's a "zoom" feature built-in (show in the status bar), allowing you to zoom in up to 400%. I'm using the WebBrowser .NET control in a demo/simulation app, and need to zoom in on a web page. Is this at all possible?

在 IE7 中有一个内置的“缩放”功能(显示在状态栏中),允许您放大 400%。我在演示/模拟应用程序中使用 WebBrowser .NET 控件,需要放大网页。这是可能吗?

(I don't want to simply take a picture of the page and enlarge it, as I need to use the links and buttons on the page).

(我不想简单地拍下页面的照片并放大它,因为我需要使用页面上的链接和按钮)。

I'm using .NET 2.0 if it matters.

如果重要的话,我正在使用 .NET 2.0。

回答by Daniel LeCheminant

There appears to be a solution at IE Zoomthat involves overriding AttachInterfacesand DetachInterfacesin the WebBrowserto get a IWebBrowser2interface, and then calling ExecWBwith OLECMDID_OPTICAL_ZOOM.

似乎有一个解决方案IE变焦,其涉及覆盖AttachInterfaces,并DetachInterfacesWebBrowser获得一个IWebBrowser2接口,然后调用ExecWBOLECMDID_OPTICAL_ZOOM

I've tried his sample codeand it appears to work; the (abridged) relevant class looks like this:

我试过他的示例代码,它似乎有效;(删节)相关类看起来像这样:

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace ZoomBrowser
{
    public partial class MyBrowser : WebBrowser
    {
        #region enums
        public enum OLECMDID
        {
            // ...
            OLECMDID_OPTICAL_ZOOM = 63,
            OLECMDID_OPTICAL_GETZOOMRANGE = 64,
            // ...
        }

        public enum OLECMDEXECOPT
        {
            // ...
            OLECMDEXECOPT_DONTPROMPTUSER,
            // ...
        }

        public enum OLECMDF
        {
            // ...
            OLECMDF_SUPPORTED = 1
        }
        #endregion

        #region IWebBrowser2
        [ComImport, /*SuppressUnmanagedCodeSecurity,*/
         TypeLibType(TypeLibTypeFlags.FOleAutomation | 
                     TypeLibTypeFlags.FDual |       
                     TypeLibTypeFlags.FHidden), 
         Guid("D30C1661-CDAF-11d0-8A3E-00C04FC9E26E")]
        public interface IWebBrowser2
        {
            [DispId(100)]  void GoBack();
            [DispId(0x65)] void GoForward();
            [DispId(0x66)] void GoHome();
            [DispId(0x67)] void GoSearch();
            [DispId(0x68)] void Navigate([In] string Url, 
                                         [In] ref object flags, 
                                         [In] ref object targetFrameName, 
                                         [In] ref object postData, 
                                         [In] ref object headers);
            [DispId(-550)] void Refresh();
            [DispId(0x69)] void Refresh2([In] ref object level);
            [DispId(0x6a)] void Stop();
            [DispId(200)]  object Application 
                           { [return: 
                              MarshalAs(UnmanagedType.IDispatch)] get; }
            [DispId(0xc9)] object Parent 
                           { [return: 
                              MarshalAs(UnmanagedType.IDispatch)] get; }
            [DispId(0xca)] object Container 
                           { [return: 
                              MarshalAs(UnmanagedType.IDispatch)] get; }
            [DispId(0xcb)] object Document 
                           { [return: 
                              MarshalAs(UnmanagedType.IDispatch)] get; }
            [DispId(0xcc)] bool TopLevelContainer { get; }
            [DispId(0xcd)] string Type { get; }
            [DispId(0xce)] int Left { get; set; }
            [DispId(0xcf)] int Top { get; set; }
            [DispId(0xd0)] int Width { get; set; }
            [DispId(0xd1)] int Height { get; set; }
            [DispId(210)]  string LocationName { get; }
            [DispId(0xd3)] string LocationURL { get; }
            [DispId(0xd4)] bool Busy { get; }
            [DispId(300)]  void Quit();
            [DispId(0x12d)] void ClientToWindow(out int pcx, out int pcy);
            [DispId(0x12e)] void PutProperty([In] string property, 
                                             [In] object vtValue);
            [DispId(0x12f)] object GetProperty([In] string property);
            [DispId(0)] string Name { get; }
            [DispId(-515)] int HWND { get; }
            [DispId(400)] string FullName { get; }
            [DispId(0x191)] string Path { get; }
            [DispId(0x192)] bool Visible { get; set; }
            [DispId(0x193)] bool StatusBar { get; set; }
            [DispId(0x194)] string StatusText { get; set; }
            [DispId(0x195)] int ToolBar { get; set; }
            [DispId(0x196)] bool MenuBar { get; set; }
            [DispId(0x197)] bool FullScreen { get; set; }
            [DispId(500)] void Navigate2([In] ref object URL, 
                                         [In] ref object flags, 
                                         [In] ref object targetFrameName, 
                                         [In] ref object postData, 
                                         [In] ref object headers);
            [DispId(0x1f5)] OLECMDF QueryStatusWB([In] OLECMDID cmdID);
            [DispId(0x1f6)] void ExecWB([In] OLECMDID cmdID, 
                                        [In] OLECMDEXECOPT cmdexecopt, 
                                        ref object pvaIn, IntPtr pvaOut);
            [DispId(0x1f7)] void ShowBrowserBar([In] ref object pvaClsid, 
                                                [In] ref object pvarShow, 
                                                [In] ref object pvarSize);
            [DispId(-525)] WebBrowserReadyState ReadyState { get; }
            [DispId(550)] bool Offline { get; set; }
            [DispId(0x227)] bool Silent { get; set; }
            [DispId(0x228)] bool RegisterAsBrowser { get; set; }
            [DispId(0x229)] bool RegisterAsDropTarget { get; set; }
            [DispId(0x22a)] bool TheaterMode { get; set; }
            [DispId(0x22b)] bool AddressBar { get; set; }
            [DispId(0x22c)] bool Resizable { get; set; }
        }
        #endregion

        private IWebBrowser2 axIWebBrowser2;

        public MyBrowser()
        {           
        }

        protected override void AttachInterfaces(
            object nativeActiveXObject)
        {
            base.AttachInterfaces(nativeActiveXObject);
            this.axIWebBrowser2 = (IWebBrowser2)nativeActiveXObject;
        }

        protected override void DetachInterfaces()
        {
            base.DetachInterfaces();
            this.axIWebBrowser2 = null;
        }

        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;
            }
        }
    }
}

回答by Gloupi

Easy tip:

简单提示:

//Zoom IN
webBrowser1.Focus();
SendKeys.Send("^{+}"); // [CTRL]+[+]

//Zoom OUT
webBrowser1.Focus();
SendKeys.Send("^-"); // [CTRL]+[-]

//Zoom 100%
webBrowser1.Focus();
SendKeys.Send("^0"); // [CTRL]+[0]

回答by bdrajer

This works for me:

这对我有用:

int zoomFactor = 300;
((SHDocVw.WebBrowser)webBrowser1.ActiveXInstance).ExecWB(SHDocVw.OLECMDID.OLECMDID_OPTICAL_ZOOM,
    SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER, zoomFactor, IntPtr.Zero);

It seems this can be done only after the document has been loaded.

这似乎只能在加载文档后才能完成。

回答by QHNPROF

You can use CSS:

您可以使用 CSS:

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        webBrowser1.Document.Body.Style = "zoom:50%";
    }

回答by RogerInHawaii

Note that the factor value that you pass in to the Zoom function assumes that a value of 100 corresponds to the "normal" size of the web page. Higher values of the factor means you're "zooming in", so the page looks larger. Smaller values of the factor means you're "zooming out", so the page looks smaller. I found that a reasonable increment between factors is 10.

请注意,您传递给 Zoom 函数的因子值假定值 100 对应于网页的“正常”大小。较高的因子值意味着您正在“放大”,因此页面看起来更大。较小的因子值意味着您正在“缩小”,因此页面看起来更小。我发现因子之间的合理增量是 10。

回答by Polynomial

For anybody that runs into the same issue as I did, here is a modification to Gloupi's answer that should work on more international systems where the plus and minus keys are in different locations by using the numpad sign keys. This also fixes the zoom in and out on dvorak layouts.

对于遇到与我相同问题的任何人,这里是对 Gloupi 答案的修改,它应该适用于更多国际系统,其中加号和减号键使用小键盘符号键位于不同的位置。这也修复了 dvorak 布局的放大和缩小。

// zoom in
webBrowser1.Focus();
SendKeys.Send("^{ADD}");

// zoom out
webBrowser1.Focus();
SendKeys.Send("^{SUBTRACT}");

// zoom reset
webBrowser1.Focus();
SendKeys.Send("^0");