0

Running a .NET 4.7.2 desktop application on Windows 10 2004 with a high DPI display, opted in to PerMonitorV2 DPI awareness, the TextBox control is not scaled.

See the minimal example here. That is the default desktop project template, configured for high DPI according to MSDN and with a Label and TextBox added to the form.

Run the project; and change the display scaling. Note that the Label is scaled, but the TextBox is not.

Why?

Form at 100% scale:

enter image description here

Form at 150% scale:

enter image description here

TheNextman
  • 12,428
  • 2
  • 36
  • 75
  • With `AutoScaleMode = AutoScaleMode.Font; Font = new Font("Tahoma", 9.25F);` in form constructor - textbox apply scaling too, check this https://stackoverflow.com/questions/2973165/autoscalemode-problems-with-changed-default-font – Genusatplay Mar 16 '21 at 05:20
  • As [previously noted](https://stackoverflow.com/q/66642871/7444103), if you want a DpiAware WinForms app to scale correctly when the System Dpi changes, you have to handle it. When the App is not DpiAware, the System will scale it for you. When it's not you have Control over it. If you try to scale to Font alone and hope that all will scale automatically and without any *glitches*, you're out of luck. That's not how it works. That's why events and method as `Form.OnGetDpiScaledSize()`, `Form.OnDpiChanged()`, `Control.DpiChangedBeforeParent` etc. have been added to the Framework. – Jimi Mar 16 '21 at 11:58
  • If you scale to just Font, some Controls may not scale as you hope them to. You have to handle it. Also, use the same Font for all Controls, inheriting the Font from the Parent. Some Controls are very old and not designer for this at all. Some new behaviors that override the default has been added to the Framework recently, in .Net Framework 4.8 and more to the new .Net 5. The implementation is still partial and doesn't handle all cases. It's up to you to do the job most of the time. – Jimi Mar 16 '21 at 12:06
  • As mentioned before, take care of what the `AutoScaleDimensions` is set to, or just remove it. I suggest to scale your Forms to Dpi (`AutoScaleMode = Dpi`) and handle the Dpi Change Notification directly. You can get notifications on a per-Monitor basis now, so that's something else you can handle more properly in WinForms. If you don't like the *manual work* involved, there's always WPF, which handles this feature in a more *automatic* way. – Jimi Mar 16 '21 at 12:06
  • I'm not sure I understand the downvote or .NET apologism. I maintain a Win32 application that fully supports `PerMonitorV2`, and of course in Win32 *everything* is done manually - I understand the issues. Microsoft claims that WinForms in .NET 4.7.2 and Windows 10 automatically scale. I'm trying to understand what I'm seeing. In my example project, the label scales but the textbox doesn't. If I switch to "Dpi" scaling mode, everything scales, but if I switch to a custom font, the textbox scales and now the label doesn't. – TheNextman Mar 16 '21 at 17:03
  • Is there documentation about which controls I have to "handle" and which I don't? If the implementation is partial, is there a way to know in which cases I need to "do the job"? Or is this just innate knowledge that .NET developers posses? – TheNextman Mar 16 '21 at 17:06
  • Does this answer your question? [How to write WinForms code that auto-scales to system font and dpi settings?](https://stackoverflow.com/questions/22735174/how-to-write-winforms-code-that-auto-scales-to-system-font-and-dpi-settings) – magicandre1981 Mar 20 '21 at 16:06
  • @magicandre1981 No. That answer says "All ContainerControls must be set to the same AutoScaleMode = Font". My example shows a specific - and straightforward - case where that doesn't work. – TheNextman Mar 21 '21 at 18:34

0 Answers0