Trying to make WinForms look good in 4K but forms too large after using AutoScaleMode.Dpi?

后端 未结 1 925
忘掉有多难
忘掉有多难 2021-01-28 01:22

Here is the situation: I have a form frmCompanyMasterEdit which inherits frmBaseEdit, which inherits frmBase, which inherits System.Windows.Form

1条回答
  •  无人共我
    2021-01-28 01:49

    My advise for designing forms to scale to different DPI's is to avoid the defaults that the designer sets.

    In the designer, set the AutoScaleMode to Inherit and not the default Font. Then add a default Constructor (Public Sub New()) to the form's code and define the auto-scaling info there.

    Public Sub New()
        InitializeComponent()
        Me.AutoScaleDimensions = New System.Drawing.SizeF(96.0!, 96.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi
    End Sub
    

    The AutoScaleDimensions property is set to what historically has been the base DPI of display devices (screens), 96 DPI.

    By declaring these properties in the constructor versus the designer generated InitializeComponent` method, you do lose the scaling in the designer view for this specific form; the scaling will be applied at run-time. Any form that inherits from it will be displayed with scaling applied in the designer and should appear to have the same size as when run.

    Next, manifest (add a app.manifest) the application to declare it DPI aware. There are various options for DPI awareness, but the basic from is:

      
        
          true
        
      
    

    Weird, I did as you said (with the AutoScaleDimensions) and now the form is not "huge" but it is a bit small and the labels and textboxes are close together.

    Fixing this after the damage has been done by VS overwriting factors based on the current design environment, is a game of trial and error. If the form appears to small, try a different base scale factor. Since (96.0!, 96.0!) is too small, a reasonable next attempt would be (120.0!, 120.0!); i.e. the form was originally designed at a 125% (96 * 1.25 = 120) zoom factor.

    Finally, make sure the form looks proper at all display resolutions/DPI configurations. You may need to implement a specific scaling mechanism for some configurations.

    0 讨论(0)
提交回复
热议问题