Update Anchor Layout to support high DPI machines. - #7956
Merged
Conversation
dreddy-work
force-pushed
the
dev/dreddy/UpdateAnchorLayout
branch
from
October 20, 2022 19:19
5761470 to
5d85d41
Compare
dreddy-work
marked this pull request as ready for review
October 20, 2022 19:20
JeremyKuhne
requested changes
Oct 20, 2022
JeremyKuhne
left a comment
Member
There was a problem hiding this comment.
Mostly nits. If you can try to update to interpolated strings wherever you're touching code it helps avoid extra work / allocations. They are significantly better everywhere and Debug statements will completely skip forming the string if conditions aren't met.
dreddy-work
force-pushed
the
dev/dreddy/UpdateAnchorLayout
branch
from
October 21, 2022 00:37
4c746ad to
d7bfd19
Compare
JeremyKuhne
previously approved these changes
Oct 21, 2022
RussKie
reviewed
Oct 23, 2022
RussKie
reviewed
Oct 23, 2022
RussKie
reviewed
Oct 23, 2022
RussKie
reviewed
Oct 23, 2022
RussKie
reviewed
Oct 23, 2022
RussKie
reviewed
Oct 23, 2022
RussKie
reviewed
Oct 23, 2022
RussKie
reviewed
Oct 23, 2022
RussKie
reviewed
Oct 23, 2022
dreddy-work
force-pushed
the
dev/dreddy/UpdateAnchorLayout
branch
from
October 26, 2022 23:30
86d8367 to
b1c10af
Compare
RussKie
reviewed
Oct 27, 2022
Tanya-Solyanik
previously approved these changes
Oct 27, 2022
Tanya-Solyanik
approved these changes
Oct 27, 2022
dreddy-work
commented
Oct 28, 2022
| internal static partial class LocalAppContextSwitches | ||
| { | ||
| // Switch names declared internal below are used in unit/integration tests. Refer to | ||
| // https://github.com/microsoft/winforms/blob/tree/main/docs/design/anchor_layout_changes_in_net80.md |
Member
Author
There was a problem hiding this comment.
Changing name to anchor-layout-changes-in-net80.md when renaming it on doc PR. Intentionally underscored here.
Contributor
There was a problem hiding this comment.
❗ This link is still incorrect.
RussKie
reviewed
Nov 2, 2022
| /// <devdoc> | ||
| /// Returns AnchorLayoutV2 switch value from runtimeconfig.json. Defaults to true if application is targeting .NET 8.0 and beyond. | ||
| /// Refer to | ||
| /// https://github.com/microsoft/winforms/blob/tree/main/docs/design/anchor_layout_changes_in_net80.md for more details. |
RussKie
reviewed
Nov 2, 2022
| /// </summary> | ||
| /// <devdoc> | ||
| /// This is the new behavior introduced in .NET 8.0. Refer to | ||
| /// https://github.com/microsoft/winforms/blob/tree/main/docs/design/anchor_layout_changes_in_net80.md for more details. |
Contributor
|
A friendly reminder to raise a breaking doc in https://github.com/dotnet/docs/ |
Member
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
We have multiple issues reported in WinForms around the anchor layout being problematic on higher DPI scale monitors, irrespective of application DPI mode. This document outlines the changes being made in .NET 8.0 to address these issues along with setting the goal to support all supported application DPI modes in WinForms.
Problem in Scope:
Anchored control’s position with respect to its parent should be able to determine at the design time and would only need to be changed if there were explicit changes in the control’s Bounds or when the control is scaled in response to a DPI changed event. Bounds changes as result of Parent’s bounds change shouldn’t alter control’s relative position in the parent’s rectangle. However, layout in WinForms computes the anchored control’s position every time there are changes to control’s bounds or control’s property change that may impact its position. This is leading to the issues we have been seeing. The following is a source snippet that is being serialized in WinForms designer and added comments show the various events that trigger anchor computations and why they could be wrong or unnecessary.
The above snippet does not represent the complete set of instances where anchor computations are unnecessary and may hold invalid anchor values. It gets even more complicated when nested UserControls are involved.
Known issues:
We have multiple issues reported here from customers and some of them are direct result of anchor miscalculations. The following are snippets of the category of issues we currently see.
Expected:

Current Behavior:

Proposed solution:
Unnecessary control’s anchors computation is the root cause for many issues reported so far. In this proposal, we are delaying the anchors computation to the specific time in the layout flow - which is when the control’s and its parent’s handles are created. By that time, in the majority of scenarios, the control’s and its parent’s bounds are finalized and the display monitor’s DPI has been applied to them. We also have issues with how we are calculating anchors but, in this proposal, we are simplifying anchor calculations as mentioned in the above pic to help diagnose any future issues.
We may still have cases where developers are forced to create handle explicitly, out of order, but those cases can be handled separately by the application developer for any anchor miscalculations. The following are the events we would be using to compute anchors and replacing the current set of events mentioned in Figure 1 above.
Source snippet:
Simplifying Anchor calculations:
The current anchor calculation implementation is complicated and appears to have been a result of unnecessary attempts to calculate anchors with invalid bounds. Replacing the current implementation with the one described in Figure 1 above. Following is the source snippet that computes the anchors.
Risk mitigation:
Layout in general is complex and could impact every component in the WinForms. In order to reduce the potential risk and provide backward compatibility, This changes are
quirkedunder switchSystem.Windows.Forms.EnableAnchorLayoutV2. These changes are by default on for new/migrating applications targeting .NET 8.0 but the developers can control this by setting the above mentioned flag tofalsein the runtimeconfig.template.json for the application.Snippet for runtimeconfig.template.json:
{ "configProperties": { "System.Windows.Forms.EnableAnchorLayoutV2": true } }Fixes #8090
Fixes #8088
Microsoft Reviewers: Open in CodeFlow