Proper approach to dealing with Auto-scaling

2020-03-01 08:35发布

问题:

I'm working with another developer on a WinForms application, and we are having a big problem with the appearance related to auto scaling.

My system (a Sony VAIO) defaults to 125% on this menu:

From what I can tell, 125% sets the DPI to 120, and default font size (menus, etc.) to 11pt. On the other developer's system, his defaults to 100%, which is 96 dpi, and 9pt font. I'm not sure, but I think only the DPI is what is causing us grief.

The problem manifests itself with text being the wrong size, and either covering up other controls, or being way too spaced out (depending on who had it open in designer last). As far as I can tell, all ContainerControls are set to AutoScaleMode.Inherit, and the top-level form is AutoScaleMode.Dpi. Is this what most other people use?

回答1:

I had the same problem with one of my applications. The answer was to move it to WPF. The solution that I came with when I still used WinForms was to have 2 executable files. One developed under 96DPI and the other under 120DPI. It was double effort, but I have tried any layout configurations without success. If my controls were looking fine on 96DPI, when switching to 120DPI, they were out of the window's application. And viceversa.

WPF is the only solution to this annoying problem.



回答2:

Could this be a problem with the anchoring of the components in your form. For example, the formatting may be affected if the components are not anchored to all four corners and the dpi changes.

When I changed the dpi on my computer and opened the designer, I didn't see any formatting problems... Maybe some more information may help. Which components cover each other?



回答3:

In the end, our solution was to disable Auto-scaling by setting AutoScaleMode = AutoScaleMode.None. It required some careful placement of things (and leaving larger gaps than desired), but at least Auto-Scale didn't come along and mess things up.

This seems to be a big problem, because even parts of VisualStudio have been seen to exhibit artifacts due to my higher screen settings.