WPF Web Browser Control and DPI Scaling

后端 未结 4 1813
星月不相逢
星月不相逢 2021-02-07 06:50

I\'m working with a WPF application that uses the Web Browser control and I\'m having issues with High DPI scaling.

It looks like the Web Browser control is not properl

4条回答
  •  花落未央
    2021-02-07 07:21

    You need to determine the browser zoom percentage from the OS dpi, then set the browser zoom via its ActiveX wrapper. Unfortunately, the wrapper is not exposed in WPF so you'll need to add a reference to "Microsoft Internet Controls" and use reflection to get it.

    Private Sub Browser_LoadCompleted(sender As Object, e As NavigationEventArgs)
        Dim source = PresentationSource.FromDependencyObject(sender)
        Dim matrix As Matrix = source.CompositionTarget.TransformToDevice
        If matrix.Determinant > 1 Then
            Dim zoomLevel As Integer = matrix.Determinant * 100
            Dim ie As SHDocVw.InternetExplorer = GetType(WebBrowser).GetField("_axIWebBrowser2", BindingFlags.Instance Or BindingFlags.NonPublic).GetValue(sender)
            ie.ExecWB(SHDocVw.OLECMDID.OLECMDID_OPTICAL_ZOOM, SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER, zoomLevel, IntPtr.Zero)
        End If
    End Sub
    

    Additional reading:

    https://dzimchuk.net/best-way-to-get-dpi-value-in-wpf/

    https://weblog.west-wind.com/posts/2016/Aug/22/Detecting-and-Setting-Zoom-Level-in-the-WPF-WebBrowser-Control

提交回复
热议问题