Monday, August 18, 2014

Override WinForms Scaling for High DPI Screens, VB.NET

    Public Shared Sub ScaleForm(WindowsForm As System.Windows.Forms.Form)
        Using g As System.Drawing.Graphics = WindowsForm.CreateGraphics
            Dim sngScaleFactor As Single = 1
            Dim sngFontFactor As Single = 1
            If g.DpiX > 96 Then
                sngScaleFactor = g.DpiX / 96
                'sngFontFactor = 96 / g.DpiY
            End If
            If WindowsForm.AutoScaleDimensions = WindowsForm.CurrentAutoScaleDimensions Then
                'ucWindowsFormHost.ScaleControl(WindowsForm, sngFontFactor)
                WindowsForm.Scale(sngScaleFactor)
            End If
        End Using
    End Sub