-
Notifications
You must be signed in to change notification settings - Fork 998
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
base: main
Are you sure you want to change the base?
Mods for #12042 #12672
Changes from all commits
6632fc2
b384b60
5ac73f9
513ab3e
3610023
1bc5d82
d8a5fa9
1e9ba19
dc7c5ae
06d0e26
084bbeb
f68fedc
db63d99
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,10 @@ | |
using Windows.Win32.UI.Accessibility; | ||
using static System.Windows.Forms.TreeNode; | ||
|
||
|
||
|
||
|
||
|
||
namespace System.Windows.Forms; | ||
|
||
/// <summary> | ||
|
@@ -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; | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The _hBrush have been declared in line 38. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
@@ -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> | ||
|
@@ -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 | ||
|
@@ -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); | ||
} | ||
|
@@ -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); | ||
|
@@ -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: | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
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.