Proposal: Add DeviceLeft and DeviceTop to Window for DPI-Independent Positioning #11335
imechZhangLY
started this conversation in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
When developing a WPF application with Per-Monitor DPI Awareness, positioning a window across multiple monitors becomes unexpectedly difficult. The core issue is that WPF uses logical pixels for window coordinates, but converting logical pixels to device pixels depends on the window’s DPI. At the same time, the window’s DPI depends on which monitor the window is on, and the monitor the window is on is determined by the window’s position.
This creates a circular dependency:
To address this, WPF provides the WindowStartupTopLeftPointHelper class. However, this helper does not actually solve the positioning problem. Its algorithm works as follows:
The relevant code is:
Problems with This Algorithm
This algorithm has two fundamental issues:
Consider two horizontally arranged monitors A and B:
The logical-to-device mapping is:
If
If
This means a logical coordinate may be valid on both monitors A and B.
In this case, the final monitor selection depends purely on the enumeration order, which is nondeterministic and undesirable.
Illustration of the Problem
The following image visualizes both scenarios:
Logical pixel mapping under different DPI scale
Proposed Solution
To avoid the circular dependency and the ambiguity caused by logical coordinates, I suggest adding DeviceLeft and DeviceTop properties to Window, allowing developers to position windows directly using device pixels.
This would:
Beta Was this translation helpful? Give feedback.
All reactions