dotnet / winforms

Windows Forms is a .NET UI framework for building Windows desktop applications.
MIT License
4.37k stars 968 forks source link

Right aligned elements font scaling problem #5076

Open kirsan31 opened 3 years ago

kirsan31 commented 3 years ago

Problem description:

Right aligned elements on the form can change their relative position after font scaling. For example, when you design a form in designer you often right align labels before input fields: image

But after you ran this app in .Net, you will see this: image Right align is broken due to Font scaling (default font change in net core).

Most interesting things, same app above: image image And Right align is ok. I really don't know how this happens :) It seems that's it bugs only with 100% dpi and 100% text size. But this statement must be double checked... Seems it's all depends on the final font size.

Because we are using old designer here it's seems that this is only migration issue. But this is not true, due to new designer is not ready for production use, and we are still force to use old one with .Net :(

Expected behavior:

Right align is not broken.

Minimal repro:

RightAlign.zip

dreddy-work commented 3 years ago

@kirsan31 , you don't see this when AutoscaleMode is DPI?

kirsan31 commented 3 years ago

@dreddy-work

you don't see this when AutoscaleMode is DPI?

Yea, with AutoscaleMode = DPI align is ok, and it's logical with 100% dpi: image

dreddy-work commented 3 years ago

In this case, i do not think winforms has any control over it. We rely on Font metrics when scaling and they come from windows. Users have alternative to set DPI scale. When it comes to high DPI, our choice should default DPI scale but that would be a breaking change from current default Font scale.

In Core, Designer is rendered as out of proc and use the targeted runtime. So, it may be worth checking the behavior there.

kirsan31 commented 3 years ago

@dreddy-work Hmm, and if we look to this from other side? Open this project in new designer (see wrong align), correct right align, build, change dpi from 100 to 125, run app - align is wrong:

https://user-images.githubusercontent.com/17767561/121750611-5eca4c80-cb15-11eb-8877-30e55ec1b9eb.mp4

No direct font increase here, only dpi.

Also:

  1. Take form with corrected right align in new designer (from video above).
  2. Set AutoscaleMode to DPI and compile.
  3. Set custom dpi to 330% and relogin.
  4. Run app: Snipaste_2021-06-12_09-45-09

P.s. We relay on Font scaling instead of DPI scaling because our apps are all accessed only by rdp. And changing dpi on the server from rdp session is still challenging now days (especially with all park of different rdp clients) :( But Font scaling is work easy in this case.

dreddy-work commented 3 years ago

Thanks @kirsan31 for the GIF demonstrating the issue. A logical fix of right aligning the label will still create same alignment issue with controls on the left. Label size is adjusted to the text in it no matter if it is DPI ( DPI change cause Font change) or Font scaled.

kirsan31 commented 3 years ago

@dreddy-work did you got that with AutoscaleMode = DPI issue is still present (picture after video)?

Overall I understand that to scale well right (bottom?) aligned controls we need a new technique here. Control need to know from what corner it's must be scaled.

Can I close this or we leave this for distant future? :-)

dreddy-work commented 3 years ago

Yes. for RTL, we need special handling on the DPI changed. I have not dig into it yet but keep this bug open. We will update it with info and decide then if this need to be closed.

Olina-Zhang commented 2 years ago

@dreddy-work Verified this issue in the latest .Net 7.0 SDK build, this issue doesn't reproduce now. When Right aligned controls on 100% main screen, the runtime has the same behavior as design time when changing DPI to 300% main screen. Please see following screenshot on 300% main screen: image

RussKie commented 2 years ago

Presume fixed. @kirsan31 please let us know if the issue resurfaces.

kirsan31 commented 2 years ago

@RussKie with 7.0.100-alpha.1.21568.2 all problems described in first and fifth posts are persists.