Saturday, January 25, 2014

Fix AutoScale In Windows Forms

System.Windows.Forms.AutoScaleMode.None
System.Windows.Forms.AutoScaleMode.Dpi
System.Windows.Forms.AutoScaleMode.Font
System.Windows.Forms.AutoScaleMode.Inherit

Some pieces of your Windows.Forms application may scale differently than other pieces.  Here are some simple tricks to force Windows.Forms to scale the way you want them to.

1.  Put your form elements inside a TableLogoutPanel.  Controls inside this panel will inherit better scaling than controls on their own.

2.  Use the same AutoScaleMode on all parts of your application.

3.  When AutoScale does not work the way you want it to, create your own scaler.
  • Set all your Windows.Forms to System.Windows.Forms.AutoScaleMode.None
  • Use the following to scale your Forms:
    Private Sub LoadedInitally(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.LoadedInitally
        If mobj_WindowsForm IsNot Nothing Then

            Using g As System.Drawing.Graphics = mobj_WindowsForm.CreateGraphics
                Dim sngScaleFactor As Single = 1
                If g.DpiX > 96 Then
                    sngScaleFactor = g.DpiX / 96
                End If
                If mobj_WindowsForm.AutoScaleDimensions = mobj_WindowsForm.CurrentAutoScaleDimensions Then
                    mobj_WindowsForm.Scale(sngScaleFactor)
                End If
            End Using

        End If
    End Sub

No comments: