Skip to content

Commit f68fedc

Browse files
committed
Changes for dotnet#12042 to prep for pull request
1 parent 8d96e56 commit f68fedc

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

src/System.Windows.Forms/src/System/Windows/Forms/Controls/ListView/ListView.cs

+34
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ public partial class ListView : Control
5656
private View _viewStyle = View.LargeIcon;
5757
private string? _toolTipCaption = string.Empty;
5858

59+
private HBRUSH _hBrush; // To hold created dark mode brush for deletion
60+
5961
private const int LISTVIEWSTATE_ownerDraw = 0x00000001;
6062
private const int LISTVIEWSTATE_allowColumnReorder = 0x00000002;
6163
private const int LISTVIEWSTATE_autoArrange = 0x00000004;
@@ -4331,6 +4333,12 @@ internal void ListViewItemToolTipChanged(ListViewItem item)
43314333
protected virtual void OnAfterLabelEdit(LabelEditEventArgs e)
43324334
{
43334335
_onAfterLabelEdit?.Invoke(this, e);
4336+
4337+
// Delete created _hBrush if it exists
4338+
[DllImport("Gdi32.dll", PreserveSig = true)]
4339+
static extern void DeleteObject(HGDIOBJ ho);
4340+
DeleteObject(_hBrush);
4341+
43344342
}
43354343

43364344
protected override void OnBackgroundImageChanged(EventArgs e)
@@ -6908,6 +6916,32 @@ protected override void WndProc(ref Message m)
69086916
{
69096917
switch (m.MsgInternal)
69106918
{
6919+
case PInvokeCore.WM_CTLCOLOREDIT:
6920+
// Default handling of edit label colors
6921+
m.ResultInternal = (LRESULT)0;
6922+
#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.
6923+
if (Application.IsDarkModeEnabled)
6924+
{
6925+
// Make background of dark mode edit labels the correct color
6926+
[DllImport("Gdi32.dll", PreserveSig = true)]
6927+
static extern HBRUSH CreateSolidBrush(COLORREF color);
6928+
[DllImport("Gdi32.dll", PreserveSig = true)]
6929+
static extern COLORREF SetBkColor(HDC hdc, COLORREF color);
6930+
[DllImport("Gdi32.dll", PreserveSig = true)]
6931+
static extern COLORREF SetTextColor(HDC hdc, COLORREF color);
6932+
6933+
Color tvColor = BackColor;
6934+
Color tvTextColor = ForeColor;
6935+
_hBrush = CreateSolidBrush(tvColor);
6936+
HDC editHDC = (HDC)m.WParamInternal;
6937+
SetBkColor(editHDC, tvColor);
6938+
SetTextColor(editHDC, tvTextColor);
6939+
LRESULT lrBrush = (LRESULT)(IntPtr)_hBrush;
6940+
m.ResultInternal = lrBrush;
6941+
}
6942+
#pragma warning restore WFO5001
6943+
break;
6944+
69116945
case MessageId.WM_REFLECT_NOTIFY:
69126946
WmReflectNotify(ref m);
69136947
break;

src/System.Windows.Forms/src/System/Windows/Forms/Controls/TreeView/TreeView.cs

+33
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ public partial class TreeView : Control
5353
private bool _hoveredAlready;
5454
private bool _rightToLeftLayout;
5555

56+
private HBRUSH _hBrush; // To hold created dark mode brush for deletion
57+
5658
private nint _mouseDownNode = 0; // ensures we fire nodeClick on the correct node
5759

5860
private const int TREEVIEWSTATE_hideSelection = 0x00000001;
@@ -2102,6 +2104,11 @@ protected virtual void OnAfterLabelEdit(NodeLabelEditEventArgs e)
21022104
{
21032105
e.Node.AccessibilityObject?.RaiseAutomationEvent(UIA_EVENT_ID.UIA_AutomationFocusChangedEventId);
21042106
}
2107+
2108+
// Delete created _hBrush if it exists
2109+
[DllImport("Gdi32.dll", PreserveSig = true)]
2110+
static extern void DeleteObject(HGDIOBJ ho);
2111+
DeleteObject(_hBrush);
21052112
}
21062113

21072114
/// <summary>
@@ -3160,6 +3167,32 @@ protected override unsafe void WndProc(ref Message m)
31603167
{
31613168
switch (m.MsgInternal)
31623169
{
3170+
case PInvokeCore.WM_CTLCOLOREDIT:
3171+
// Default handling of edit label colors
3172+
m.ResultInternal = (LRESULT)0;
3173+
#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.
3174+
if (Application.IsDarkModeEnabled)
3175+
{
3176+
// Make background of dark mode edit labels the correct color
3177+
[DllImport("Gdi32.dll", PreserveSig = true)]
3178+
static extern HBRUSH CreateSolidBrush(COLORREF color);
3179+
[DllImport("Gdi32.dll", PreserveSig = true)]
3180+
static extern COLORREF SetBkColor(HDC hdc, COLORREF color);
3181+
[DllImport("Gdi32.dll", PreserveSig = true)]
3182+
static extern COLORREF SetTextColor(HDC hdc, COLORREF color);
3183+
3184+
Color tvColor = BackColor;
3185+
Color tvTextColor = ForeColor;
3186+
_hBrush = CreateSolidBrush(tvColor);
3187+
HDC editHDC = (HDC)m.WParamInternal;
3188+
SetBkColor(editHDC, tvColor);
3189+
SetTextColor(editHDC, tvTextColor);
3190+
LRESULT lrBrush = (LRESULT)(IntPtr)_hBrush;
3191+
m.ResultInternal = lrBrush;
3192+
}
3193+
#pragma warning restore WFO5001
3194+
break;
3195+
31633196
case PInvokeCore.WM_WINDOWPOSCHANGING:
31643197
case PInvokeCore.WM_NCCALCSIZE:
31653198
case PInvokeCore.WM_WINDOWPOSCHANGED:

0 commit comments

Comments
 (0)