From 370141786be2891bc056a182ac66dcf3c6050672 Mon Sep 17 00:00:00 2001 From: Klaus Loeffelmann Date: Wed, 6 Mar 2024 13:11:05 -0800 Subject: [PATCH] * Enable GroupBox... * Enable PropertyGrid * Fix some code formatting issues * Try easy fixes for Calendar. --- .../src/System/Windows/Forms/Control.cs | 79 +++++++++---------- .../Controls/DateTimePicker/DateTimePicker.cs | 4 +- .../Forms/Controls/GroupBox/GroupBox.cs | 10 +-- .../Forms/Controls/ListView/ListView.cs | 10 +-- .../Controls/MonthCalendar/MonthCalendar.cs | 6 +- .../Controls/PropertyGrid/PropertyGrid.cs | 11 ++- 6 files changed, 63 insertions(+), 57 deletions(-) diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/Control.cs b/src/System.Windows.Forms/src/System/Windows/Forms/Control.cs index 2392cf990ef..1eeee49e966 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/Control.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/Control.cs @@ -270,7 +270,6 @@ private protected void TraceCanProcessMnemonic() private static bool s_needToLoadComCtl = true; private static readonly int s_darkModeProperty = PropertyStore.CreateKey(); - private static readonly int s_isDarkModeEnabledProperty = PropertyStore.CreateKey(); // This switch determines the default text rendering engine to use by some controls that support switching rendering engine. // CheckedListBox, PropertyGrid, GroupBox, Label and LinkLabel, and ButtonBase controls. @@ -7840,50 +7839,50 @@ protected virtual void OnHandleCreated(EventArgs e) { if (this is - // Controls with four levels of inheritance, sorted alphabetically by type name - DomainUpDown // Inherits from UpDownBase, ContainerControl, ScrollableControl, Control - or NumericUpDown // Inherits from UpDownBase, ContainerControl, ScrollableControl, Control + // Controls with 4 levels of inheritance, sorted alphabetically by type name + DomainUpDown // Inherits from UpDownBase, ContainerControl, ScrollableControl, Control + or NumericUpDown // Inherits from UpDownBase, ContainerControl, ScrollableControl, Control - // Controls with three levels of inheritance, sorted alphabetically by type name - or CheckedListBox // Inherits from ListBox, ListControl, Control - or Form // Excluded - too invasive. - or FlowLayoutPanel // Inherits from Panel, ScrollableControl, Control - or SplitContainer // Inherits from ContainerControl, ScrollableControl, Control - or TabPage // Inherits from Panel, ScrollableControl, Control - or TableLayoutPanel // Inherits from Panel, ScrollableControl, Control + // Controls with 3 levels of inheritance, sorted alphabetically by type name + or CheckedListBox // Inherits from ListBox, ListControl, Control + or Form // Excluded - too invasive. + or FlowLayoutPanel // Inherits from Panel, ScrollableControl, Control + or SplitContainer // Inherits from ContainerControl, ScrollableControl, Control + or TabPage // Inherits from Panel, ScrollableControl, Control + or TableLayoutPanel // Inherits from Panel, ScrollableControl, Control // Controls with 2 levels of inheritance, sorted alphabetically by type name - // or ComboBox // Excluded - directly handled. - or ListBox // Inherits from ListControl, Control - - or Button // Inherits from ButtonBase, Control - or CheckBox // Inherits from ButtonBase, Control - or MaskedTextBox // Inherits from TextBoxBase, Control - or Panel // Inherits from ScrollableControl, Control - or RadioButton // Inherits from ButtonBase, Control - or RichTextBox // Inherits from TextBoxBase, Control - or TextBox // Inherits from TextBoxBase, Control + // or ComboBox // Excluded - directly handled. + or ListBox // Inherits from ListControl, Control + + or Button // Inherits from ButtonBase, Control + or CheckBox // Inherits from ButtonBase, Control + or MaskedTextBox // Inherits from TextBoxBase, Control + or Panel // Inherits from ScrollableControl, Control + or RadioButton // Inherits from ButtonBase, Control + or RichTextBox // Inherits from TextBoxBase, Control + or TextBox // Inherits from TextBoxBase, Control + or HScrollBar // Inherits from ScrollBar, Control + or VScrollBar // Inherits from ScrollBar, Control // Base classes and controls with direct inheritance from Control, sorted alphabetically by type name - // or ButtonBase // Excluded - probably too invasive. - or DateTimePicker // Inherits from Control - or GroupBox // Inherits from Control directly, but behaves like a container - or HScrollBar // Inherits from ScrollBar, Control - or Label // Inherits from Control - or LinkLabel // Inherits from Label, Control - // or ListView // Excluded - directly handled. - or MonthCalendar // Inherits from Control - or PictureBox // Inherits from Control - or ProgressBar // Inherits from Control - // or ScrollableControl // Excluded - probably too invasive. - // or TextBoxBase // Excluded - probably too invasive. - or TrackBar // Inherits from Control - or TreeView // Inherits from Control - // or UpDownBase // Excluded - probably too invasive. - or VScrollBar // Inherits from ScrollBar, Control - - // Base class for all UI controls in WinForms - or Control) + or ButtonBase // Inherits from Control + or DateTimePicker // Inherits from Control + // or GroupBox // Inherits from Control directly, but behaves like a container + or Label // Inherits from Control + or LinkLabel // Inherits from Label, Control + // or ListView // Excluded - directly handled. + or MonthCalendar // Inherits from Control + or PictureBox // Inherits from Control + or ProgressBar // Inherits from Control + or ScrollableControl // Inherits from Control + or TextBoxBase // Excluded - probably too invasive. + or TrackBar // Inherits from Control + or TreeView // Inherits from Control + or UpDownBase) // Inherits from Control + + // Base class for all UI controls in WinForms + // or Control // Excluded. { _ = PInvoke.SetWindowTheme(HWND, "DarkMode_Explorer", null); } diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/Controls/DateTimePicker/DateTimePicker.cs b/src/System.Windows.Forms/src/System/Windows/Forms/Controls/DateTimePicker/DateTimePicker.cs index e783e361604..497e89c56e9 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/Controls/DateTimePicker/DateTimePicker.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/Controls/DateTimePicker/DateTimePicker.cs @@ -127,7 +127,7 @@ public DateTimePicker() : base() [EditorBrowsable(EditorBrowsableState.Never)] public override Color BackColor { - get => ShouldSerializeBackColor() ? base.BackColor : Application.SystemColors.Window; + get => ShouldSerializeBackColor() || IsDarkModeEnabled ? base.BackColor : Application.SystemColors.Window; set => base.BackColor = value; } @@ -488,7 +488,7 @@ public LeftRightAlignment DropDownAlign [EditorBrowsable(EditorBrowsableState.Never)] public override Color ForeColor { - get => ShouldSerializeForeColor() ? base.ForeColor : Application.SystemColors.WindowText; + get => ShouldSerializeForeColor() || IsDarkModeEnabled ? base.ForeColor : Application.SystemColors.WindowText; set => base.ForeColor = value; } diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/Controls/GroupBox/GroupBox.cs b/src/System.Windows.Forms/src/System/Windows/Forms/Controls/GroupBox/GroupBox.cs index f98f4e6f789..d26331e5968 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/Controls/GroupBox/GroupBox.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/Controls/GroupBox/GroupBox.cs @@ -397,8 +397,8 @@ protected override void OnPaint(PaintEventArgs e) { // BACKCOMPAT requirement: // - // Why the Height/Width < 10 check? This is because uxtheme doesn't seem to handle those cases similar to - // what we do for the non-themed case, so if someone is using the groupbox as a separator, their app will + // Why the Height/Width < 10 check? This is because Ux-theme doesn't seem to handle those cases similar to + // what we do for the non-themed case, so if someone is using the GroupBox as a separator, their app will // look weird in .NET Framework 2.0. We render the old way in these cases. if (!Application.RenderWithVisualStyles || Width < 10 || Height < 10) @@ -424,15 +424,15 @@ protected override void OnPaint(PaintEventArgs e) // We only pass in the text color if it is explicitly set, else we let the renderer use the color // specified by the theme. This is a temporary workaround till we find a good solution for the // "default theme color" issue. - if (ShouldSerializeForeColor() || Enabled == false) + if (ShouldSerializeForeColor() || IsDarkModeEnabled || Enabled == false) { - Color textcolor = Enabled ? ForeColor : TextRenderer.DisabledTextColor(BackColor); + Color textColor = Enabled ? ForeColor : TextRenderer.DisabledTextColor(BackColor); GroupBoxRenderer.DrawGroupBox( e, new Rectangle(0, 0, Width, Height), Text, Font, - textcolor, + textColor, textFlags, gbState); } 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 6df5b073468..de13770cde3 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 @@ -4613,9 +4613,6 @@ protected override void OnHandleCreated(EventArgs e) PInvoke.SendMessage(this, PInvoke.CCM_SETVERSION, (WPARAM)5); } - UpdateExtendedStyles(); - RealizeProperties(); - if (IsDarkModeEnabled) { _ = PInvoke.SetWindowTheme(HWND, "DarkMode_Explorer", null); @@ -4625,6 +4622,9 @@ protected override void OnHandleCreated(EventArgs e) PInvoke.SetWindowTheme(columnHeaderHandle, "DarkMode_ItemsView", null); } + UpdateExtendedStyles(); + RealizeProperties(); + PInvoke.SendMessage(this, PInvoke.LVM_SETBKCOLOR, (WPARAM)0, (LPARAM)BackColor); PInvoke.SendMessage(this, PInvoke.LVM_SETTEXTCOLOR, (WPARAM)0, (LPARAM)ForeColor); @@ -6032,9 +6032,7 @@ private unsafe bool WmNotify(ref Message m) else if (nmlvcd->nmcd.dwDrawStage == NMCUSTOMDRAW_DRAW_STAGE.CDDS_ITEMPREPAINT) { - // We're just setting the text color, but we do not adapt the text (fore) color, - // instead we stick to the theming settings... - PInvoke.SetTextColor(nmlvcd->nmcd.hdc, Application.SystemColors.WindowText); + PInvoke.SetTextColor(nmlvcd->nmcd.hdc, ForeColor); // ...and then just let the default handling play out. m.ResultInternal = (LRESULT)(nint)PInvoke.CDRF_DODEFAULT; diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/Controls/MonthCalendar/MonthCalendar.cs b/src/System.Windows.Forms/src/System/Windows/Forms/Controls/MonthCalendar/MonthCalendar.cs index 8f125f18ee6..742c20360d0 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/Controls/MonthCalendar/MonthCalendar.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/Controls/MonthCalendar/MonthCalendar.cs @@ -73,7 +73,7 @@ public partial class MonthCalendar : Control /// /// This is the arbitrary number of pixels that the Win32 control /// inserts between calendars vertically, regardless of font. - /// From comctl32 MonthCalendar sources CALBORDER. + /// From ComCtl32 MonthCalendar sources CALBORDER. /// private const int InsertHeightSize = 6; @@ -193,7 +193,7 @@ public override Color BackColor { get { - if (ShouldSerializeBackColor()) + if (ShouldSerializeBackColor() || IsDarkModeEnabled) { return base.BackColor; } @@ -407,7 +407,7 @@ public override Color ForeColor { get { - if (ShouldSerializeForeColor()) + if (ShouldSerializeForeColor() || IsDarkModeEnabled) { return base.ForeColor; } diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/Controls/PropertyGrid/PropertyGrid.cs b/src/System.Windows.Forms/src/System/Windows/Forms/Controls/PropertyGrid/PropertyGrid.cs index 5b88eedae58..ab96c94ffd1 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/Controls/PropertyGrid/PropertyGrid.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/Controls/PropertyGrid/PropertyGrid.cs @@ -70,7 +70,11 @@ public partial class PropertyGrid : ContainerControl, IComPropertyBrowser, IProp private object[]? _selectedObjects; private int _paintFrozen; - private Color _lineColor = SystemInformation.HighContrast ? Application.SystemColors.ControlDarkDark : Application.SystemColors.InactiveBorder; + + private Color _lineColor = SystemInformation.HighContrast + ? Application.SystemColors.ControlDarkDark + : Application.SystemColors.InactiveBorder; + private Color _categoryForegroundColor = Application.SystemColors.ControlText; private Color _categorySplitterColor = Application.SystemColors.Control; private Color _viewBorderColor = Application.SystemColors.ControlDark; @@ -2537,6 +2541,11 @@ protected override void OnFontChanged(EventArgs e) protected override void OnHandleCreated(EventArgs e) { base.OnHandleCreated(e); + + // Making sure, the _toolBar BackColor gets updated when the + // default BackColor is not the typical light-theme one. + BackColor = BackColor; + OnLayoutInternal(dividerOnly: false); TypeDescriptor.Refreshed += OnTypeDescriptorRefreshed; if (_selectedObjects is not null && _selectedObjects.Length > 0)