LevelOS-Core/RoundComboBox.cs
2026-04-17 21:09:19 -03:00

126 lines
3.8 KiB
C#

using System.Drawing.Drawing2D;
namespace UI
{
public class RoundComboBox : UserControl
{
private ComboBox _comboBox = null!;
public int Radius { get; set; } = 4;
public Color BorderColor { get; set; } = Color.LightGray;
public Color FocusColor { get; set; } = Color.Blue;
private bool _focused;
public ComboBoxStyle DropDownStyle
{
get => _comboBox.DropDownStyle;
set => _comboBox.DropDownStyle = value;
}
public ComboBox.ObjectCollection Items => _comboBox.Items;
public object? SelectedItem
{
get => _comboBox.SelectedItem;
set => _comboBox.SelectedItem = value;
}
public int SelectedIndex
{
get => _comboBox.SelectedIndex;
set => _comboBox.SelectedIndex = value;
}
public override string Text
{
get => _comboBox.Text;
set => _comboBox.Text = value;
}
public new Color BackColor
{
get => base.BackColor;
set { base.BackColor = value; if (_comboBox != null) _comboBox.BackColor = value; }
}
public new event EventHandler? SelectedIndexChanged
{
add => _comboBox.SelectedIndexChanged += value;
remove => _comboBox.SelectedIndexChanged -= value;
}
public RoundComboBox()
{
DoubleBuffered = true;
base.BackColor = Color.White;
_comboBox = new ComboBox
{
//BorderStyle = BorderStyle.None,
Font = new Font("Segoe UI", 9f),
BackColor = Color.White,
DropDownStyle = ComboBoxStyle.DropDownList,
FlatStyle = FlatStyle.Flat
};
_comboBox.GotFocus += (s, e) => { _focused = true; Invalidate(); };
_comboBox.LostFocus += (s, e) => { _focused = false; Invalidate(); };
_comboBox.TextChanged += (s, e) => OnTextChanged(e);
_comboBox.Leave += (s, e) => OnLeave(e);
Controls.Add(_comboBox);
SizeChanged += (s, e) => AjustarComboBox();
}
private void AjustarComboBox()
{
_comboBox.Width = Width - 12;
_comboBox.Location = new Point(6, (Height - _comboBox.PreferredHeight) / 2);
}
public object? DataSource
{
get => _comboBox.DataSource;
set => _comboBox.DataSource = value;
}
public string DisplayMember
{
get => _comboBox.DisplayMember;
set => _comboBox.DisplayMember = value;
}
public string ValueMember
{
get => _comboBox.ValueMember;
set => _comboBox.ValueMember = value;
}
public object? SelectedValue
{
get => _comboBox.SelectedValue;
set => _comboBox.SelectedValue = value;
}
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
using var path = GetPath(new Rectangle(0, 0, Width - 1, Height - 1), Radius);
using var brush = new SolidBrush(BackColor);
e.Graphics.FillPath(brush, path);
using var pen = new Pen(_focused ? FocusColor : BorderColor, _focused ? 1.5f : 1f);
e.Graphics.DrawPath(pen, path);
}
private static GraphicsPath GetPath(Rectangle r, int rad)
{
var path = new GraphicsPath();
int d = rad * 2;
path.AddArc(r.X, r.Y, d, d, 180, 90);
path.AddArc(r.Right - d, r.Y, d, d, 270, 90);
path.AddArc(r.Right - d, r.Bottom - d, d, d, 0, 90);
path.AddArc(r.X, r.Bottom - d, d, d, 90, 90);
path.CloseFigure();
return path;
}
}
}