Fix Form with StartPosition=CenterParent and WindowState=Maximized opening on wrong monitor #14156
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.
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