Skip to content

Commit

Permalink
Merge pull request #12785 from ricardobossan/Issue_12744_ComboBox_Bor…
Browse files Browse the repository at this point in the history
…der_Invisible

[release/9.0] WinForms ComboBox border and drop down button are not visible sometimes
  • Loading branch information
Tanya-Solyanik authored Feb 5, 2025
2 parents 98c0db1 + 18bbceb commit 8e825fa
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand All @@ -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);
}
}

/// <summary>
/// Paints over the edges of the combo box to make it appear flat.
/// </summary>
Expand Down Expand Up @@ -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);
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -3990,5 +3983,5 @@ private FlatComboAdapter FlatComboBoxAdapter
}

internal virtual FlatComboAdapter CreateFlatComboAdapterInstance()
=> new(this, shouldRedrawAsSmallButton: false);
=> new(this, smallButton: false);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
}

Expand Down

0 comments on commit 8e825fa

Please sign in to comment.