0

I have some issues with Winform scaling, and found this code here

Public 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

The code is brilliant! It does what I want it to do, (scales the form) even though I have NO idea WHAT it is doing. However I get the following message on this line WindowsForm.Scale(sngScaleFactor) 'Public Sub Scale(ratio As Single)' is obsolete: 'This method has been deprecated. Use the Scale(SizeF ratio) method instead.

Please would someone be able to help me to rewrite the line of code using the suggested change?

As I said , I have no idea what the code is doing, and in order to test such code in high DPI scale environment one can't use the debugger because VS resizes everything to the new DPI settings. I also searched and found some very sparse explanations with no examples and as my skill level is nonexistent in this field I am floundering with it.

Community
  • 1
  • 1
user1500403
  • 551
  • 8
  • 32
  • 2
    It dates from .NET 1.0 and was deprecated when 2.0 added the AutoScaleMode property. Just do what it says and write WindowsForm.Scale(new PointF(sngScaleFactor, sngScaleFactor)); And do consider that you probably should delete this code because, well, AutoScaleMode already does this. They don't come up with these deprecation warnings to make your life miserable, they are supposed to be *helpful*. – Hans Passant Jul 14 '15 at 22:33
  • I get the following error when I input your suggested code. Error 2 Overload resolution failed because no accessible 'Scale' can be called with these arguments: 'Public Sub Scale(factor As System.Drawing.SizeF)': Value of type 'System.Drawing.PointF' cannot be converted to 'System.Drawing.SizeF'. 'Public Sub Scale(ratio As Single)': Value of type 'System.Drawing.PointF' cannot be converted to 'Single'. – user1500403 Jul 19 '15 at 02:28

1 Answers1

0

I found the answer through experimentation and the way shown by Hans Passant. The code is designed to scale the Form using DPI. WHile it is true that this can be done by Form AutoScaleMode settings, the issue is that this setting cannot be altered dynamicaly. I have need for a user to manually select such scaling, where otherwise it is set to none. This code does the job very well!

Public 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)
            WindowsForm.Scale(New SizeF(sngScaleFactor, sngScaleFactor))
        End If
    End Using
End Sub
user1500403
  • 551
  • 8
  • 32