Fix The Form1 title bar change to white color when set the RightToLeft to Yes in PropertyGrid control#14742
Fix The Form1 title bar change to white color when set the RightToLeft to Yes in PropertyGrid control#14742Sathish-087 wants to merge 5 commits into
Conversation
Add dark mode synchronization for top-level windows after handle recreation.
| if (Application.ColorModeSet && DarkModeRequestState is true) | ||
| { | ||
| BOOL isDark = Application.IsDarkModeEnabled; | ||
| PInvoke.DwmSetWindowAttribute( | ||
| HWND, | ||
| DWMWINDOWATTRIBUTE.DWMWA_USE_IMMERSIVE_DARK_MODE, | ||
| &isDark, | ||
| (uint)sizeof(BOOL)).AssertSuccess(); |
There was a problem hiding this comment.
Consider extracting this new DWM call into a small private helper (e.g. SetFormImmersiveDarkModeInternal(bool isDarkMode)), mirroring the existing SetFormAttributeColorInternal:
private unsafe void SetFormImmersiveDarkModeInternal(bool isDarkMode)
{
BOOL isDark = isDarkMode;
PInvoke.DwmSetWindowAttribute(
HWND,
DWMWINDOWATTRIBUTE.DWMWA_USE_IMMERSIVE_DARK_MODE,
&isDark,
(uint)sizeof(BOOL));
}and call it here:
if (Application.ColorModeSet && DarkModeRequestState is true)
{
SetFormImmersiveDarkModeInternal(Application.IsDarkModeEnabled);
}Benefits:
- Keeps
SetFormTitleProperties()non-unsafe. The only reason this method's signature was changed tounsafeis the&isDarkpointer. Moving that into a dedicated helper (asSetFormAttributeColorInternalalready does with&colorRef) confinesunsafeto the small helper and narrows the unsafe scope of the rest of the method. - Consistency/readability. It parallels the neighboring
SetFormAttributeColorInternalcalls, with the guard condition kept in the caller (same pattern as theProperties.TryGetValue(...)checks below). - Lets us drop
.AssertSuccess(). The adjacentSetFormAttributeColorInternaldeliberately ignores the HRESULT.AssertSuccess()isDebug.Assert(Succeeded, ...), andDWMWA_USE_IMMERSIVE_DARK_MODE(attribute 20) is unsupported on Windows 10 builds < 19041, where the call can return a failure HRESULT and trip a debug assertion. Matching the surrounding "ignore HRESULT" pattern avoids that.
There was a problem hiding this comment.
Thanks for the suggestion—agreed. I’ll extract the DWM call into a private SetFormImmersiveDarkModeInternal helper (mirroring SetFormAttributeColorInternal), keep SetFormTitleProperties() non-unsafe, and drop .AssertSuccess() so we ignore the HRESULT consistently.
There was a problem hiding this comment.
Agreed. We should take this after those changes.
|
@Sathish-087, do you have the permission to merge this? |
|
Hi @KlausLoeffelmann , |
Fixes #12582, #12992
Proposed changes
DWMWA_USE_IMMERSIVE_DARK_MODEwindow attribute during form handle recreation.SetFormTitleProperties()to restore dark mode title bar settings when a top-level form handle is recreated.ShowInTaskbarorRightToLeftcauses the form title bar to revert to the light theme while the rest of the form remains in dark mode.Customer Impact
ShowInTaskbarorRightToLeftat runtime while dark mode is enabled.Regression?
Risk
Screenshots
Before
FormTitleIssueDemo.mp4
After
FormTitleAfterFixDemo.mp4
Test methodology
RightToLeftproperty at runtime.ShowInTaskbarproperty at runtime.Accessibility testing
Test environment(s)
Microsoft Reviewers: Open in CodeFlow