From f68fedc5efe1e7d07cdd969ecbe4d157dfd86c99 Mon Sep 17 00:00:00 2001 From: Mark Daniel Osborne Date: Sat, 21 Dec 2024 15:41:27 +1000 Subject: [PATCH] Changes for #12042 to prep for pull request --- .../Forms/Controls/ListView/ListView.cs | 34 +++++++++++++++++++ .../Forms/Controls/TreeView/TreeView.cs | 33 ++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/Controls/ListView/ListView.cs b/src/System.Windows.Forms/src/System/Windows/Forms/Controls/ListView/ListView.cs index 5949a52df76..3a5c3124b7f 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/Controls/ListView/ListView.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/Controls/ListView/ListView.cs @@ -56,6 +56,8 @@ public partial class ListView : Control private View _viewStyle = View.LargeIcon; private string? _toolTipCaption = string.Empty; + private HBRUSH _hBrush; // To hold created dark mode brush for deletion + private const int LISTVIEWSTATE_ownerDraw = 0x00000001; private const int LISTVIEWSTATE_allowColumnReorder = 0x00000002; private const int LISTVIEWSTATE_autoArrange = 0x00000004; @@ -4331,6 +4333,12 @@ internal void ListViewItemToolTipChanged(ListViewItem item) protected virtual void OnAfterLabelEdit(LabelEditEventArgs e) { _onAfterLabelEdit?.Invoke(this, e); + + // Delete created _hBrush if it exists + [DllImport("Gdi32.dll", PreserveSig = true)] + static extern void DeleteObject(HGDIOBJ ho); + DeleteObject(_hBrush); + } protected override void OnBackgroundImageChanged(EventArgs e) @@ -6908,6 +6916,32 @@ protected override void WndProc(ref Message m) { switch (m.MsgInternal) { + case PInvokeCore.WM_CTLCOLOREDIT: + // Default handling of edit label colors + m.ResultInternal = (LRESULT)0; +#pragma warning disable WFO5001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed. + if (Application.IsDarkModeEnabled) + { + // Make background of dark mode edit labels the correct color + [DllImport("Gdi32.dll", PreserveSig = true)] + static extern HBRUSH CreateSolidBrush(COLORREF color); + [DllImport("Gdi32.dll", PreserveSig = true)] + static extern COLORREF SetBkColor(HDC hdc, COLORREF color); + [DllImport("Gdi32.dll", PreserveSig = true)] + static extern COLORREF SetTextColor(HDC hdc, COLORREF color); + + Color tvColor = BackColor; + Color tvTextColor = ForeColor; + _hBrush = CreateSolidBrush(tvColor); + HDC editHDC = (HDC)m.WParamInternal; + SetBkColor(editHDC, tvColor); + SetTextColor(editHDC, tvTextColor); + LRESULT lrBrush = (LRESULT)(IntPtr)_hBrush; + m.ResultInternal = lrBrush; + } +#pragma warning restore WFO5001 + break; + case MessageId.WM_REFLECT_NOTIFY: WmReflectNotify(ref m); break; diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/Controls/TreeView/TreeView.cs b/src/System.Windows.Forms/src/System/Windows/Forms/Controls/TreeView/TreeView.cs index a7e66b35eb7..6b667a2a425 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/Controls/TreeView/TreeView.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/Controls/TreeView/TreeView.cs @@ -53,6 +53,8 @@ public partial class TreeView : Control private bool _hoveredAlready; private bool _rightToLeftLayout; + private HBRUSH _hBrush; // To hold created dark mode brush for deletion + private nint _mouseDownNode = 0; // ensures we fire nodeClick on the correct node private const int TREEVIEWSTATE_hideSelection = 0x00000001; @@ -2102,6 +2104,11 @@ protected virtual void OnAfterLabelEdit(NodeLabelEditEventArgs e) { e.Node.AccessibilityObject?.RaiseAutomationEvent(UIA_EVENT_ID.UIA_AutomationFocusChangedEventId); } + + // Delete created _hBrush if it exists + [DllImport("Gdi32.dll", PreserveSig = true)] + static extern void DeleteObject(HGDIOBJ ho); + DeleteObject(_hBrush); } /// @@ -3160,6 +3167,32 @@ protected override unsafe void WndProc(ref Message m) { switch (m.MsgInternal) { + case PInvokeCore.WM_CTLCOLOREDIT: + // Default handling of edit label colors + m.ResultInternal = (LRESULT)0; +#pragma warning disable WFO5001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed. + if (Application.IsDarkModeEnabled) + { + // Make background of dark mode edit labels the correct color + [DllImport("Gdi32.dll", PreserveSig = true)] + static extern HBRUSH CreateSolidBrush(COLORREF color); + [DllImport("Gdi32.dll", PreserveSig = true)] + static extern COLORREF SetBkColor(HDC hdc, COLORREF color); + [DllImport("Gdi32.dll", PreserveSig = true)] + static extern COLORREF SetTextColor(HDC hdc, COLORREF color); + + Color tvColor = BackColor; + Color tvTextColor = ForeColor; + _hBrush = CreateSolidBrush(tvColor); + HDC editHDC = (HDC)m.WParamInternal; + SetBkColor(editHDC, tvColor); + SetTextColor(editHDC, tvTextColor); + LRESULT lrBrush = (LRESULT)(IntPtr)_hBrush; + m.ResultInternal = lrBrush; + } +#pragma warning restore WFO5001 + break; + case PInvokeCore.WM_WINDOWPOSCHANGING: case PInvokeCore.WM_NCCALCSIZE: case PInvokeCore.WM_WINDOWPOSCHANGED: