/// <summary>
        /// This function is used to zoom web browser's content.
        /// </summary>
        /// <param name="webbrowser">The instance of web browser.</param>
        /// <param name="zoom">The zoom scale. It should be 50~400</param>
        /// <remarks>This function must be invoked after the webbrowser has completely loaded the URI.</remarks>
        static void SetZoom(System.Windows.Controls.WebBrowser webbrowser, int zoom)
        {
            try
            {
                if (null == webbrowser)
                {
                    return;
                }

                FieldInfo fiComWebBrowser = webbrowser.GetType().GetField(
                    "_axIWebBrowser2", BindingFlags.Instance | BindingFlags.NonPublic);
                if (null != fiComWebBrowser)
                {
                    Object objComWebBrowser = fiComWebBrowser.GetValue(webbrowser);

                    if (null != objComWebBrowser)
                    {
                        object[] args = new object[]
                        {
                            OLECMDID_OPTICAL_ZOOM,
                            OLECMDEXECOPT_DODEFAULT,
                            zoom,
                            IntPtr.Zero
                        };
                        objComWebBrowser.GetType().InvokeMember(
                            "ExecWB",
                            BindingFlags.InvokeMethod,
                            null, objComWebBrowser,
                            args);
                    }
                }
            }
            catch {}
        }
Example #2
0
        public static void SetZoom(this System.Windows.Controls.WebBrowser wb, int zoom)
        {
            try
            {
                FieldInfo webBrowserInfo = wb.GetType().GetField("_axIWebBrowser2", BindingFlags.Instance | BindingFlags.NonPublic);

                object comWebBrowser = null;
                object zoomPercent   = zoom;

                if (webBrowserInfo != null)
                {
                    comWebBrowser = webBrowserInfo.GetValue(wb);
                }
                if (comWebBrowser != null)
                {
                    InternetExplorer ie = (InternetExplorer)comWebBrowser;
                    ie.ExecWB(OLECMDID.OLECMDID_OPTICAL_ZOOM, OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER, ref zoomPercent, IntPtr.Zero);

                    if (wb.Document is mshtml.HTMLDocument htmlDoc)
                    {
                        htmlDoc.parentWindow.scrollTo(0, 0);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }