Fix Form with StartPosition=CenterParent and WindowState=Maximized opening on wrong monitor#14156
Conversation
…ening on wrong monitor
There was a problem hiding this comment.
Pull request overview
This PR fixes an issue where modal forms with StartPosition=CenterParent and WindowState=Maximized would incorrectly open on the primary monitor instead of the same monitor as their parent form.
Key Changes:
- Moved the
s_propDialogOwnerproperty assignment to occur beforeCreateControl()is called in theShowDialogmethod - Removed the duplicate property assignment that previously occurred after
CreateControl()
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #14156 +/- ##
===================================================
+ Coverage 77.15180% 77.15252% +0.00072%
===================================================
Files 3279 3279
Lines 645333 645336 +3
Branches 47720 47721 +1
===================================================
+ Hits 497886 497893 +7
- Misses 143754 143759 +5
+ Partials 3693 3684 -9
Flags with carried forward coverage won't be shown. Click here to find out more. 🚀 New features to boost your workflow:
|
|
Hi, a couple of observations on this change: 1. Stale comment contradicts new behavior (Form.cs, ~line 5750): The existing comment explicitly states: "it is necessary to not set the owner before creating the handle. Otherwise, the window may never receive Dpi changed event even if its parent has different Dpi." — Your change now sets 2. DPI regression risk: The original ordering was deliberately chosen to ensure DPI-changed events fire correctly in multi-DPI setups (per the comment above). While your PR description correctly notes this only affects the property dictionary and not the Win32 parent-child relationship, it would be prudent to verify on a multi-monitor setup with different DPI settings that DPI-changed events still fire as expected for maximized dialogs shown via (Copilot co-authored on Klaus' behalf.) |
Updated comments to clarify DPI handling in multi-DPI environments.
@KlausLoeffelmann For issue1: I updated the comment |
Fixes #8932
Root Cause:
In the
ShowDialogmethod, thes_propDialogOwnerproperty was set afterCreateControl()was called. This meant that during window creation, whenFillInCreateParamsStartPositionis invoked to determine window coordinates, the dialog owner information was not yet available. As a result, the window coordinates defaulted to the primary monitor, and Windows would maximize the form on that monitor regardless of where the parent form was located.Proposed changes
Properties.AddOrRemoveValue(s_propDialogOwner, owner)call to beforeCreateControl()in theShowDialogmethodProperties.AddOrRemoveValue(s_propDialogOwner, owner)call that occurred afterCreateControl()FillInCreateParamsStartPositionto access the dialog owner information during theCenterScreenpositioning logic, which already has code to detect the owner's screen and position the window accordinglyCustomer Impact
StartPosition=CenterParentandWindowState=Maximizedwill now correctly maximize on the same monitor as their owner/parent formRegression?
Risk
s_propDialogOwneris set in Properties dictionary, does not affect the Windows API parent-child relationship)Screenshots
Before
In the form with value
FormWindowState.Maximizedit is always displayed on the main monitor.After
A maximized window is displayed on the monitor where its parent window is located (i.e., the secondary monitor).
AfterChanges.mp4
Test methodology
Test environment(s)
Microsoft Reviewers: Open in CodeFlow