Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Address non dark-mode coloring of listitem and treenode controls while in edit mode #12674

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -4331,6 +4333,11 @@ 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)
Expand Down Expand Up @@ -6908,6 +6915,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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

/// <summary>
Expand Down Expand Up @@ -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:
Expand Down