How to Adapt webbrowser to DPI in WPF

Source: Internet
Author: User
In WPF, webbrowser itself has some defects and cannot adapt to DPI. The specific reason will be added later. First paste the code and how to use it. (Very irresponsible first post code... Make it clear later)
The method is: first load an empty html. After loading the HTML, perform the zoom of webbrowser and then load the URL you want to load.
namespace WebbrowserTest{    public class WebBrowserZoomInvoker    {        [DllImport("user32.dll")]        static extern IntPtr GetDC(IntPtr ptr);        [DllImport("user32.dll", EntryPoint = "ReleaseDC")]        public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDc);        [DllImport("gdi32.dll")]        public static extern IntPtr CreateDC(            string lpszDriver,  // driver name            string lpszDevice,  // device name            string lpszOutput,  // not used; should be NULL            Int64 lpInitData    // optional printer data        );        [DllImport("gdi32.dll")]        public static extern int GetDeviceCaps(            IntPtr hdc,         // handle to DC            int nIndex          // index of capability        );        [DllImport("user32.dll")]        public static extern bool SetProcessDPIAware();        const int LOGPIXELSX = 88;        const int LOGPIXELSY = 90;        static System.Drawing.PointF GetCurrentDIPScale()        {            System.Drawing.PointF scaleUI = new System.Drawing.PointF(1.0f, 1.0f);            try            {                SetProcessDPIAware();                IntPtr screenDC = GetDC(IntPtr.Zero);                int dpi_x = GetDeviceCaps(screenDC, LOGPIXELSX);                int dpi_y = GetDeviceCaps(screenDC, LOGPIXELSY);                scaleUI.X = (float)dpi_x / 96.0f;                scaleUI.Y = (float)dpi_y / 96.0f;                ReleaseDC(IntPtr.Zero, screenDC);                return scaleUI;            }            catch (System.Exception ex)            {            }            return scaleUI;        }        /// <summary>        /// The flags are used to zoom web browser's content.        /// </summary>        static readonly int OLECMDEXECOPT_DODEFAULT = 0;        static readonly int OLECMDID_OPTICAL_ZOOM = 63;        /// <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(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 (System.Exception ex)            {            }        }        public static readonly String AppDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);        public static readonly String EmptyHTMLFilePath = Path.Combine(AppDataPath, "Empty.html");        public static readonly String EmptyHTML = @"

How to use:
For example, if you have a WPF webbrowser and want to load "http://www.baidu.com", you should use it like this:

        public void LoadURL(String strURL)        {            if (strURL.Length > 0)            {                WebBrowserZoomInvoker.AddZoomInvoker(webbrowser);                this.webbrowser.Navigate(new Uri(strURL.ToString()));            }        }

Not complete... to be supplemented

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.