最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Dialog Units, Dynamic control location - Stack Overflow

programmeradmin3浏览0评论

Windows Forms support auto-scaling so if the user increases "font size" in Windows preferences, the form will automatically rescale. Now, when you create a window, add a button and this button has some predefined location, eg. x=100 y=200 this location will be represented in pixels on the system where the window was initially created. We can call it "Dialog Units". On the initial project 1 dialog unit = 1 pixel. No longer holds when you change "font size".

You can create controls dynamically and specify location to be eg. x=100 y=300 to put something else below that button. This will work only as long as the font size is the same as on the system of the user who created the form.

Now when you change your font size settings, the layout will break as controls added via Forms.Controls.Add(...) are not rescaled. How to force rescaling for dynamically added controls, so they're not landing on "random" position which depends on windows font scaling preference?

Is there some method to convert Dialog Units to pixels?

Windows Forms support auto-scaling so if the user increases "font size" in Windows preferences, the form will automatically rescale. Now, when you create a window, add a button and this button has some predefined location, eg. x=100 y=200 this location will be represented in pixels on the system where the window was initially created. We can call it "Dialog Units". On the initial project 1 dialog unit = 1 pixel. No longer holds when you change "font size".

You can create controls dynamically and specify location to be eg. x=100 y=300 to put something else below that button. This will work only as long as the font size is the same as on the system of the user who created the form.

Now when you change your font size settings, the layout will break as controls added via Forms.Controls.Add(...) are not rescaled. How to force rescaling for dynamically added controls, so they're not landing on "random" position which depends on windows font scaling preference?

Is there some method to convert Dialog Units to pixels?

Share Improve this question edited Feb 17 at 6:41 Uwe Keim 40.7k61 gold badges188 silver badges302 bronze badges asked Feb 14 at 15:20 SlawekSlawek 7824 silver badges7 bronze badges 1
  • See: layout, dpi-aware, likely high-dpi too is useful. – Sinatr Commented Feb 14 at 15:50
Add a comment  | 

1 Answer 1

Reset to default 0

I can reproduce your problem: If you change the Scale setting in Windows Settings, you will find that the size and location of the form and controls are affected by the change in the scale setting after opening the project, and after running the project, the dynamically added buttons do not display the correct size and location.

It is normal that the size and location of the initially created form and controls are affected by the change in the scale setting. You can refer to the instructions in the following link and handle it:

https://github/dotnet/winforms/blob/main/docs/designer/designer-high-dpi-mode.md

https://learn.microsoft/en-us/visualstudio/designers/disable-dpi-awareness?view=vs-2022

Based on the above link, we could set ForceDesignerDPIUnaware property to solve the problem that controls displayed abnormally. However, it is only suitable for dragged controls instead of dynmaically created control. If you still want to solve it for dynmaically created control, I recommend that you could submit an issue in Winforms-GitHub-Issues. Upon you submit the issue in GitHub, please post back your issue link, which may help others who faces the simliar issue to know the progress.

发布评论

评论列表(0)

  1. 暂无评论