Skip to content

Fix The Form1 title bar change to white color when set the RightToLeft to Yes in PropertyGrid control#14742

Open
Sathish-087 wants to merge 5 commits into
dotnet:mainfrom
Sathish-087:Fix_Issue_12582
Open

Fix The Form1 title bar change to white color when set the RightToLeft to Yes in PropertyGrid control#14742
Sathish-087 wants to merge 5 commits into
dotnet:mainfrom
Sathish-087:Fix_Issue_12582

Conversation

@Sathish-087

@Sathish-087 Sathish-087 commented Jul 15, 2026

Copy link
Copy Markdown

Fixes #12582, #12992

Proposed changes

  • Re-apply the DWMWA_USE_IMMERSIVE_DARK_MODE window attribute during form handle recreation.
  • Update SetFormTitleProperties() to restore dark mode title bar settings when a top-level form handle is recreated.
  • Fix an issue where changing ShowInTaskbar or RightToLeft causes the form title bar to revert to the light theme while the rest of the form remains in dark mode.

Customer Impact

  • Ensures dark mode is consistently preserved for top-level forms after handle recreation scenarios.
  • Fixes title bar theme mismatches that occur when changing ShowInTaskbar or RightToLeft at runtime while dark mode is enabled.

Regression?

  • No

Risk

  • Low. The change only reapplies an existing DWM dark mode attribute during handle recreation and is executed only for top-level forms with dark mode explicitly requested.

Screenshots

Before

FormTitleIssueDemo.mp4

After

FormTitleAfterFixDemo.mp4

Test methodology

Accessibility testing

  • No accessibility changes introduced.
  • This change only restores an existing DWM window attribute during handle recreation and does not modify UIA behavior, keyboard navigation, or control accessibility.

Test environment(s)

  • Windows 11
  • .NET SDK: 11.0.100-preview.5.26302.115
Microsoft Reviewers: Open in CodeFlow

@Sathish-087
Sathish-087 requested a review from a team as a code owner July 15, 2026 06:37
Comment on lines +4971 to +4978
if (Application.ColorModeSet && DarkModeRequestState is true)
{
BOOL isDark = Application.IsDarkModeEnabled;
PInvoke.DwmSetWindowAttribute(
HWND,
DWMWINDOWATTRIBUTE.DWMWA_USE_IMMERSIVE_DARK_MODE,
&isDark,
(uint)sizeof(BOOL)).AssertSuccess();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

  1. Keeps SetFormTitleProperties() non-unsafe. The only reason this method's signature was changed to unsafe is the &isDark pointer. Moving that into a dedicated helper (as SetFormAttributeColorInternal already does with &colorRef) confines unsafe to the small helper and narrows the unsafe scope of the rest of the method.
  2. Consistency/readability. It parallels the neighboring SetFormAttributeColorInternal calls, with the guard condition kept in the caller (same pattern as the Properties.TryGetValue(...) checks below).
  3. Lets us drop .AssertSuccess(). The adjacent SetFormAttributeColorInternal deliberately ignores the HRESULT. AssertSuccess() is Debug.Assert(Succeeded, ...), and DWMWA_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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. We should take this after those changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[DarkMode]The Form1 title bar change to white color when set the RightToLeft to Yes in PropertyGrid control

3 participants