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

Mods for #12042 #12672

Draft
wants to merge 13 commits into
base: main
Choose a base branch
from
Draft
18 changes: 18 additions & 0 deletions Winforms.sln
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Analyzers", "Analyzers", "{
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "System.Private.Windows.GdiPlus", "src\System.Private.Windows.GdiPlus\System.Private.Windows.GdiPlus.csproj", "{442C867C-51C0-8CE5-F067-DF065008E3DA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ScratchProject2", "src\System.Windows.Forms\tests\IntegrationTests\ScratchProject2\ScratchProject2.csproj", "{9A195C79-D335-485B-B5AB-9A1EF1F9E58A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -1102,6 +1104,22 @@ Global
{442C867C-51C0-8CE5-F067-DF065008E3DA}.Release|x64.Build.0 = Release|Any CPU
{442C867C-51C0-8CE5-F067-DF065008E3DA}.Release|x86.ActiveCfg = Release|Any CPU
{442C867C-51C0-8CE5-F067-DF065008E3DA}.Release|x86.Build.0 = Release|Any CPU
{9A195C79-D335-485B-B5AB-9A1EF1F9E58A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9A195C79-D335-485B-B5AB-9A1EF1F9E58A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9A195C79-D335-485B-B5AB-9A1EF1F9E58A}.Debug|arm64.ActiveCfg = Debug|Any CPU
{9A195C79-D335-485B-B5AB-9A1EF1F9E58A}.Debug|arm64.Build.0 = Debug|Any CPU
{9A195C79-D335-485B-B5AB-9A1EF1F9E58A}.Debug|x64.ActiveCfg = Debug|Any CPU
{9A195C79-D335-485B-B5AB-9A1EF1F9E58A}.Debug|x64.Build.0 = Debug|Any CPU
{9A195C79-D335-485B-B5AB-9A1EF1F9E58A}.Debug|x86.ActiveCfg = Debug|Any CPU
{9A195C79-D335-485B-B5AB-9A1EF1F9E58A}.Debug|x86.Build.0 = Debug|Any CPU
{9A195C79-D335-485B-B5AB-9A1EF1F9E58A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9A195C79-D335-485B-B5AB-9A1EF1F9E58A}.Release|Any CPU.Build.0 = Release|Any CPU
{9A195C79-D335-485B-B5AB-9A1EF1F9E58A}.Release|arm64.ActiveCfg = Release|Any CPU
{9A195C79-D335-485B-B5AB-9A1EF1F9E58A}.Release|arm64.Build.0 = Release|Any CPU
{9A195C79-D335-485B-B5AB-9A1EF1F9E58A}.Release|x64.ActiveCfg = Release|Any CPU
{9A195C79-D335-485B-B5AB-9A1EF1F9E58A}.Release|x64.Build.0 = Release|Any CPU
{9A195C79-D335-485B-B5AB-9A1EF1F9E58A}.Release|x86.ActiveCfg = Release|Any CPU
{9A195C79-D335-485B-B5AB-9A1EF1F9E58A}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
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,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);

Copy link
Member

Choose a reason for hiding this comment

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

Remove the extra blank line in here, also apply this requirement for below code sections.

}

protected override void OnBackgroundImageChanged(EventArgs e)
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
using Windows.Win32.UI.Accessibility;
using static System.Windows.Forms.TreeNode;





namespace System.Windows.Forms;

/// <summary>
Expand All @@ -30,6 +34,9 @@ public partial class TreeView : Control
private const string BackSlash = "\\";
private const int DefaultTreeViewIndent = 19;

// To hold created dark mode brush for deletion
private HBRUSH _hBrush;

private DrawTreeNodeEventHandler? _onDrawNode;
private NodeLabelEditEventHandler? _onBeforeLabelEdit;
private NodeLabelEditEventHandler? _onAfterLabelEdit;
Expand All @@ -53,6 +60,8 @@ public partial class TreeView : Control
private bool _hoveredAlready;
private bool _rightToLeftLayout;

private HBRUSH _hBrush; // To hold created dark mode brush for deletion
Copy link
Member

Choose a reason for hiding this comment

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

The _hBrush have been declared in line 38.

Copy link
Author

Choose a reason for hiding this comment

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

Not sure how I got both definitions into my file. I added both. Probably from being such a noob. Not sure what to do next? Delete this and create a new pull request draft? Modify this one? Not sure how to do either.

Copy link
Member

@Zheng-Li01 Zheng-Li01 Dec 23, 2024

Choose a reason for hiding this comment

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

Thanks, your great job, and you don't need to delete this Pull Request, just modify it on your local repo, and then commit again.

In addition, please reference the https://github.com/dotnet/winforms/blob/main/docs/coding-style.md to modify your code format due to there have extra blank lines, files appeared in your pull request.


private nint _mouseDownNode = 0; // ensures we fire nodeClick on the correct node

private const int TREEVIEWSTATE_hideSelection = 0x00000001;
Expand Down Expand Up @@ -2102,6 +2111,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 @@ -2551,6 +2565,38 @@ private unsafe void TvnSelected(NMTREEVIEWW* nmtv)
}
}

private enum DWMWINDOWATTRIBUTE
{
DWMWA_NCRENDERING_ENABLED = 1,
DWMWA_NCRENDERING_POLICY,
DWMWA_TRANSITIONS_FORCEDISABLED,
DWMWA_ALLOW_NCPAINT,
DWMWA_CAPTION_BUTTON_BOUNDS,
DWMWA_NONCLIENT_RTL_LAYOUT,
DWMWA_FORCE_ICONIC_REPRESENTATION,
DWMWA_FLIP3D_POLICY,
DWMWA_EXTENDED_FRAME_BOUNDS,
DWMWA_HAS_ICONIC_BITMAP,
DWMWA_DISALLOW_PEEK,
DWMWA_EXCLUDED_FROM_PEEK,
DWMWA_CLOAK,
DWMWA_CLOAKED,
DWMWA_FREEZE_REPRESENTATION,
DWMWA_PASSIVE_UPDATE_MODE,
DWMWA_USE_HOSTBACKDROPBRUSH,
DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20H1 = 19,
DWMWA_USE_IMMERSIVE_DARK_MODE = 20,
DWMWA_WINDOW_CORNER_PREFERENCE = 33,
DWMWA_BORDER_COLOR,
DWMWA_CAPTION_COLOR,
DWMWA_TEXT_COLOR,
DWMWA_VISIBLE_FRAME_BORDER_THICKNESS,
DWMWA_SYSTEMBACKDROP_TYPE,
DWMWA_LAST
}



private LRESULT TvnBeginLabelEdit(NMTVDISPINFOW nmtvdi)
{
// Check for invalid node handle
Expand Down Expand Up @@ -2579,8 +2625,81 @@ private LRESULT TvnBeginLabelEdit(NMTVDISPINFOW nmtvdi)
if (!e.CancelEdit)
{
_labelEdit = new TreeViewLabelEditNativeWindow(this);

_labelEdit.AssignHandle(PInvokeCore.SendMessage(this, PInvoke.TVM_GETEDITCONTROL));
}



/*
// Force dark mode on editing label if necessary
[DllImport("dwmapi.dll", PreserveSig = false)]
static extern void DwmSetWindowAttribute(IntPtr hwnd, int attr,
ref int attrValue, int attrSize);

#pragma warning disable WFO5001
SystemColorMode thisColorMode;
if (Application.ColorMode == SystemColorMode.System)
{
thisColorMode = Application.SystemColorMode;
}
else
{
thisColorMode = Application.ColorMode;
}

if (thisColorMode == SystemColorMode.Dark)
{


// Try different dwm attributes and values
// May be a problem with WIN 10 compatibility

//int trueVal = 1;

//var attr = (int)DWMWINDOWATTRIBUTE.DWMWA_CAPTION_COLOR;
//var attrVal = 0x00ff0000;

IntPtr handle = _labelEdit.Handle;
Debug.WriteLine(handle.ToString());

for (int attrVal = 0; attrVal <= 10; attrVal++)

{
Debug.WriteLine("");

for (int attr = 1; attr <= 50; attr++)
{
Debug.WriteLine("");
try
{

DwmSetWindowAttribute(handle, attr, ref attrVal, Marshal.SizeOf(attrVal));


}
catch (Exception dwmEx)
{
string msg = dwmEx.Message;
if (dwmEx.InnerException is not null)
{
msg = msg + Environment.NewLine + dwmEx.InnerException.Message;
}

Debug.WriteLine("Attribute:" + attr.ToString());
Debug.WriteLine("Attr Valu:" + attrVal.ToString());
Debug.WriteLine(msg);
}
}
}
}

// do I need to invalidate to get an update?


#pragma warning restore WFO5001
*/


return (LRESULT)(e.CancelEdit ? 1 : 0);
}
Expand Down Expand Up @@ -2782,6 +2901,7 @@ private unsafe void CustomDraw(ref Message m)
// when one item is selected, click and hold on another). This needs to be fixed.
Color riFore = renderinfo.ForeColor;
Color riBack = renderinfo.BackColor;

if (renderinfo is not null && !riFore.IsEmpty)
{
nmtvcd->clrText = ColorTranslator.ToWin32(riFore);
Expand Down Expand Up @@ -3160,6 +3280,33 @@ 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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading