From 18bbcebaaf939a66e0521f05159d4656363d3311 Mon Sep 17 00:00:00 2001 From: "Ricardo Bossan (BEYONDSOFT CONSULTING INC) (from Dev Box)" Date: Tue, 14 Jan 2025 17:06:15 -0300 Subject: [PATCH] Backport to .NET 9.0 Fixes #12744 ## Root Cause - Regression introduced by PRs 11529 and 11761, which modified how `ComboBox` controls would be drawn. ## Proposed changes - Revert code from PRs 11529 and 11761. ## Customer Impact - `ComboBox` button can be shown normally when switching RightToLeft property, or recreating the `ComboBox` for any other reason. ## Regression? - Yes ## Risk - Minimal ## Screenshots ### Before ### After ## Test methodology - Manual ## Test environment(s) - 9.0.100 --- .../ComboBox/ComboBox.FlatComboAdapter.cs | 77 +++++++------------ .../Forms/Controls/ComboBox/ComboBox.cs | 11 +-- ...ntrol.ToolStripComboBoxFlatComboAdapter.cs | 2 +- 3 files changed, 32 insertions(+), 58 deletions(-) diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/Controls/ComboBox/ComboBox.FlatComboAdapter.cs b/src/System.Windows.Forms/src/System/Windows/Forms/Controls/ComboBox/ComboBox.FlatComboAdapter.cs index f9d1e2dbffb..020cdeeea1c 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/Controls/ComboBox/ComboBox.FlatComboAdapter.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/Controls/ComboBox/ComboBox.FlatComboAdapter.cs @@ -23,9 +23,7 @@ internal class FlatComboAdapter private const int OFFSET_2PIXELS = 2; protected static int s_offsetPixels = OFFSET_2PIXELS; - private bool ShouldRedrawAsSmallButton { get; } - - public FlatComboAdapter(ComboBox comboBox, bool shouldRedrawAsSmallButton) + public FlatComboAdapter(ComboBox comboBox, bool smallButton) { // adapter is re-created when combobox is resized, see IsValid method, thus we don't need to handle DPI changed explicitly s_offsetPixels = comboBox.LogicalToDeviceUnits(OFFSET_2PIXELS); @@ -37,10 +35,8 @@ public FlatComboAdapter(ComboBox comboBox, bool shouldRedrawAsSmallButton) _innerInnerBorder = new Rectangle(_innerBorder.X + 1, _innerBorder.Y + 1, _innerBorder.Width - 2, _innerBorder.Height - 2); _dropDownRect = new Rectangle(_innerBorder.Right + 1, _innerBorder.Y, dropDownButtonWidth, _innerBorder.Height + 1); - ShouldRedrawAsSmallButton = shouldRedrawAsSmallButton; - // fill in several pixels of the dropdown rect with white so that it looks like the combo button is thinner. - if (shouldRedrawAsSmallButton) + if (smallButton) { _whiteFillRect = _dropDownRect; _whiteFillRect.Width = WhiteFillRectWidth; @@ -64,48 +60,6 @@ public bool IsValid(ComboBox combo) return (combo.ClientRectangle == _clientRect && combo.RightToLeft == _origRightToLeft); } - public virtual void DrawPopUpCombo(ComboBox comboBox, Graphics g) - { - if (comboBox.DropDownStyle == ComboBoxStyle.Simple) - { - return; - } - - if (ShouldRedrawAsSmallButton) - { - DrawFlatCombo(comboBox, g); - } - - bool rightToLeft = comboBox.RightToLeft == RightToLeft.Yes; - - // Draw a dark border around everything if we're in popup mode - if ((!comboBox.Enabled) || (comboBox.FlatStyle == FlatStyle.Popup)) - { - bool focused = comboBox.ContainsFocus || comboBox.MouseIsOver; - Color borderPenColor = GetPopupOuterBorderColor(comboBox, focused); - - using var borderPen = borderPenColor.GetCachedPenScope(); - Pen innerPen = comboBox.Enabled ? borderPen : SystemPens.Control; - - // Draw a border around the dropdown. - if (rightToLeft) - { - g.DrawRectangle( - innerPen, - new Rectangle(_outerBorder.X, _outerBorder.Y, _dropDownRect.Width + 1, _outerBorder.Height)); - } - else - { - g.DrawRectangle( - innerPen, - new Rectangle(_dropDownRect.X, _outerBorder.Y, _outerBorder.Right - _dropDownRect.X, _outerBorder.Height)); - } - - // Draw a border around the whole comboBox. - g.DrawRectangle(borderPen, _outerBorder); - } - } - /// /// Paints over the edges of the combo box to make it appear flat. /// @@ -153,6 +107,33 @@ public virtual void DrawFlatCombo(ComboBox comboBox, Graphics g) using var innerBorderPen = innerBorderColor.GetCachedPenScope(); g.DrawRectangle(innerBorderPen, _innerBorder); g.DrawRectangle(innerBorderPen, _innerInnerBorder); + + // Draw a dark border around everything if we're in popup mode + if ((!comboBox.Enabled) || (comboBox.FlatStyle == FlatStyle.Popup)) + { + bool focused = comboBox.ContainsFocus || comboBox.MouseIsOver; + Color borderPenColor = GetPopupOuterBorderColor(comboBox, focused); + + using var borderPen = borderPenColor.GetCachedPenScope(); + Pen innerPen = comboBox.Enabled ? borderPen : SystemPens.Control; + + // Around the dropdown + if (rightToLeft) + { + g.DrawRectangle( + innerPen, + new Rectangle(_outerBorder.X, _outerBorder.Y, _dropDownRect.Width + 1, _outerBorder.Height)); + } + else + { + g.DrawRectangle( + innerPen, + new Rectangle(_dropDownRect.X, _outerBorder.Y, _outerBorder.Right - _dropDownRect.X, _outerBorder.Height)); + } + + // Around the whole combobox. + g.DrawRectangle(borderPen, _outerBorder); + } } /// diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/Controls/ComboBox/ComboBox.cs b/src/System.Windows.Forms/src/System/Windows/Forms/Controls/ComboBox/ComboBox.cs index bf9b6dc33d3..f01c213daa6 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/Controls/ComboBox/ComboBox.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/Controls/ComboBox/ComboBox.cs @@ -3899,14 +3899,7 @@ protected override unsafe void WndProc(ref Message m) } using Graphics g = Graphics.FromHdcInternal((IntPtr)dc); - if ((!Enabled || FlatStyle == FlatStyle.Popup) && MouseIsOver) - { - FlatComboBoxAdapter.DrawPopUpCombo(this, g); - } - else - { - FlatComboBoxAdapter.DrawFlatCombo(this, g); - } + FlatComboBoxAdapter.DrawFlatCombo(this, g); return; } @@ -3990,5 +3983,5 @@ private FlatComboAdapter FlatComboBoxAdapter } internal virtual FlatComboAdapter CreateFlatComboAdapterInstance() - => new(this, shouldRedrawAsSmallButton: false); + => new(this, smallButton: false); } diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/Controls/ToolStrips/ToolStripComboBox.ToolStripComboBoxControl.ToolStripComboBoxFlatComboAdapter.cs b/src/System.Windows.Forms/src/System/Windows/Forms/Controls/ToolStrips/ToolStripComboBox.ToolStripComboBoxControl.ToolStripComboBoxFlatComboAdapter.cs index 4c032266272..98c512c271c 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/Controls/ToolStrips/ToolStripComboBox.ToolStripComboBoxControl.ToolStripComboBoxFlatComboAdapter.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/Controls/ToolStrips/ToolStripComboBox.ToolStripComboBoxControl.ToolStripComboBoxFlatComboAdapter.cs @@ -12,7 +12,7 @@ internal partial class ToolStripComboBoxControl : ComboBox { internal class ToolStripComboBoxFlatComboAdapter : FlatComboAdapter { - public ToolStripComboBoxFlatComboAdapter(ComboBox comboBox) : base(comboBox, shouldRedrawAsSmallButton: true) + public ToolStripComboBoxFlatComboAdapter(ComboBox comboBox) : base(comboBox, smallButton: true) { }