Estou fazendo uma interface no Windows Forms do C# (Usando o Visual Studio 2017) que tinha projetado no Figma com um ComboBox, tinha projetado as bordas dele arredondadas, consegui fazer a alteração de várias características dele com um tutorial que vi no youtube, mas não achei em lugar algum um meio de fazer as bordas do ComboBox poderem ser customizáveis, ou seja, poder colocar um arredondamento, seja de um ângulo fixo ou customizável, só consegui fazer elas poderem trocar de cor e grossura mas não arredondamento. Isso é possível? (o que quero fazer com a borda é basicamente o que mostra na imagem que eu coloquei)
coloquei o código onde estou tentando implemetar essa borda arredondada abaixo da imagem.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.ComponentModel;
using System.Drawing.Design;
namespace PIM_4_Desktop.Controles_Personalizados
{
[DefaultEvent("OnSelectedIndexChanged")]
class ComboBoxPers: UserControl
{
//Fields
private Color backColor = Color.WhiteSmoke;
private Color iconColor = Color.MediumSlateBlue;
private Color listBackColor = Color.FromArgb(230, 228, 245);
private Color listTextColor = Color.DimGray;
private Color borderColor = Color.MediumSlateBlue;
private int borderSize = 2;
private int borderRadius = 10;
//Items
private ComboBox cmbList;
private Label lblText;
private Button btnIcon;
//Events
public event EventHandler OnSelectedIndexChanged;//Default event
//Constructor
public ComboBoxPers()
{
cmbList = new ComboBox();
lblText = new Label();
btnIcon = new Button();
this.SuspendLayout();
//ComboBox: Dropdown list
cmbList.BackColor = listBackColor;
cmbList.Font = new Font(this.Font.Name, 10F);
cmbList.ForeColor = listTextColor;
cmbList.SelectedIndexChanged += new EventHandler(ComboBox_SelectedIndexChanged);//Default event
cmbList.TextChanged += new EventHandler(ComboBox_TextChanged);//Refresh text
//Button: Icon
btnIcon.Dock = DockStyle.Right;
btnIcon.FlatStyle = FlatStyle.Flat;
btnIcon.FlatAppearance.BorderSize = 0;
btnIcon.BackColor = backColor;
btnIcon.Size = new Size(30, 30);
btnIcon.Cursor = Cursors.Hand;
btnIcon.Click += new EventHandler(Icon_Click);//Open dropdown list
btnIcon.Paint += new PaintEventHandler(Icon_Paint);//Draw icon
//Label: Text
lblText.Dock = DockStyle.Fill;
lblText.AutoSize = false;
lblText.BackColor = backColor;
lblText.TextAlign = ContentAlignment.MiddleLeft;
lblText.Padding = new Padding(8, 0, 0, 0);
lblText.Font = new Font(this.Font.Name, 10F);
//->Attach label events to user control event
lblText.Click += new EventHandler(Surface_Click);//Select combo box
lblText.MouseEnter += new EventHandler(Surface_MouseEnter);
lblText.MouseLeave += new EventHandler(Surface_MouseLeave);
//User Control
this.Controls.Add(lblText);//2
this.Controls.Add(btnIcon);//1
this.Controls.Add(cmbList);//0
this.MinimumSize = new Size(200, 30);
this.Size = new Size(200, 30);
this.ForeColor = Color.DimGray;
this.Padding = new Padding(borderSize);//Border Size
this.Font = new Font(this.Font.Name, 10F);
base.BackColor = borderColor; //Border Color
this.ResumeLayout();
AdjustComboBoxDimensions();
}
//Properties
//-> Appearance
[Category("Trabalho PIM 4 - Aparência")]
public int BorderRadius
{
get { return borderRadius; }
set
{
borderRadius = value;
this.Invalidate();
}
}
[Category("Trabalho PIM 4 - Aparência")]
public new Color BackColor
{
get { return backColor; }
set
{
backColor = value;
lblText.BackColor = backColor;
btnIcon.BackColor = backColor;
}
}
[Category("Trabalho PIM 4 - Aparência")]
public Color IconColor
{
get { return iconColor; }
set
{
iconColor = value;
btnIcon.Invalidate();//Redraw icon
}
}
[Category("Trabalho PIM 4 - Aparência")]
public Color ListBackColor
{
get { return listBackColor; }
set
{
listBackColor = value;
cmbList.BackColor = listBackColor;
}
}
[Category("Trabalho PIM 4 - Aparência")]
public Color ListTextColor
{
get { return listTextColor; }
set
{
listTextColor = value;
cmbList.ForeColor = listTextColor;
}
}
[Category("Trabalho PIM 4 - Aparência")]
public Color BorderColor
{
get { return borderColor; }
set
{
borderColor = value;
base.BackColor = borderColor; //Border Color
}
}
[Category("Trabalho PIM 4 - Aparência")]
public int BorderSize
{
get { return borderSize; }
set
{
borderSize = value;
this.Padding = new Padding(borderSize);//Border Size
AdjustComboBoxDimensions();
}
}
[Category("Trabalho PIM 4 - Aparência")]
public override Color ForeColor
{
get { return base.ForeColor; }
set
{
base.ForeColor = value;
lblText.ForeColor = value;
}
}
[Category("Trabalho PIM 4 - Aparência")]
public override Font Font
{
get { return base.Font; }
set
{
base.Font = value;
lblText.Font = value;
cmbList.Font = value;//Optional
}
}
[Category("Trabalho PIM 4 - Aparência")]
public string Texts
{
get { return lblText.Text; }
set { lblText.Text = value; }
}
[Category("Trabalho PIM 4 - Aparência")]
public ComboBoxStyle DropDownStyle
{
get { return cmbList.DropDownStyle; }
set
{
if (cmbList.DropDownStyle != ComboBoxStyle.Simple)
cmbList.DropDownStyle = value;
}
}
//Properties
//-> Data
[Category("Trabalho PIM 4 - Dados")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[Editor("System.Windows.Forms.Design.ListControlStringCollectionEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
[Localizable(true)]
[MergableProperty(false)]
public ComboBox.ObjectCollection Items
{
get { return cmbList.Items; }
}
[Category("Trabalho PIM 4 - Dados")]
[AttributeProvider(typeof(IListSource))]
[DefaultValue(null)]
public object DataSource
{
get { return cmbList.DataSource; }
set { cmbList.DataSource = value; }
}
[Category("Trabalho PIM 4 - Dados")]
[Browsable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[Editor("System.Windows.Forms.Design.ListControlStringCollectionEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
[EditorBrowsable(EditorBrowsableState.Always)]
[Localizable(true)]
public AutoCompleteStringCollection AutoCompleteCustomSource
{
get { return cmbList.AutoCompleteCustomSource; }
set { cmbList.AutoCompleteCustomSource = value; }
}
[Category("Trabalho PIM 4 - Dados")]
[Browsable(true)]
[DefaultValue(AutoCompleteSource.None)]
[EditorBrowsable(EditorBrowsableState.Always)]
public AutoCompleteSource AutoCompleteSource
{
get { return cmbList.AutoCompleteSource; }
set { cmbList.AutoCompleteSource = value; }
}
[Category("Trabalho PIM 4 - Dados")]
[Browsable(true)]
[DefaultValue(AutoCompleteMode.None)]
[EditorBrowsable(EditorBrowsableState.Always)]
public AutoCompleteMode AutoCompleteMode
{
get { return cmbList.AutoCompleteMode; }
set { cmbList.AutoCompleteMode = value; }
}
[Category("Trabalho PIM 4 - Dados")]
[Bindable(true)]
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public object SelectedItem
{
get { return cmbList.SelectedItem; }
set { cmbList.SelectedItem = value; }
}
[Category("Trabalho PIM 4 - Dados")]
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public int SelectedIndex
{
get { return cmbList.SelectedIndex; }
set { cmbList.SelectedIndex = value; }
}
[Category("Trabalho PIM 4 - Dados")]
[DefaultValue("")]
[Editor("System.Windows.Forms.Design.DataMemberFieldEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
[TypeConverter("System.Windows.Forms.Design.DataMemberFieldConverter, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
public string DisplayMember
{
get { return cmbList.DisplayMember; }
set { cmbList.DisplayMember = value; }
}
[Category("Trabalho PIM 4 - Dados")]
[DefaultValue("")]
[Editor("System.Windows.Forms.Design.DataMemberFieldEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
public string ValueMember
{
get { return cmbList.ValueMember; }
set { cmbList.ValueMember = value; }
}
//Private methods
private void AdjustComboBoxDimensions()
{
cmbList.Width = lblText.Width;
cmbList.Location = new Point()
{
X = this.Width - this.Padding.Right - cmbList.Width,
Y = lblText.Bottom - cmbList.Height
};
}
private GraphicsPath GetFigurePath(Rectangle rect, float radius)
{
GraphicsPath path = new GraphicsPath();
float curveSize = radius * 2F;
path.StartFigure();
path.AddArc(rect.X, rect.Y, curveSize, curveSize, 180, 90);
path.AddArc(rect.Right - curveSize, rect.Y, curveSize, curveSize, 270, 90);
path.AddArc(rect.Right - curveSize, rect.Bottom - curveSize, curveSize, curveSize, 0, 90);
path.AddArc(rect.X, rect.Bottom - curveSize, curveSize, curveSize, 90, 90);
path.CloseFigure();
return path;
}
protected override void OnPaint(PaintEventArgs pevent)
{
base.OnPaint(pevent);
Rectangle rectSurface = this.ClientRectangle;
Rectangle rectBorder = Rectangle.Inflate(rectSurface, -borderSize, -borderSize);
int smoothSize = 2;
if (borderSize > 0)
smoothSize = borderSize;
if (borderRadius > 2) //Botão Redondo
{
using (GraphicsPath pathSurface = GetFigurePath(rectSurface, borderRadius))
using (GraphicsPath pathBorder = GetFigurePath(rectBorder, borderRadius - borderSize))
using (Pen penSurface = new Pen(this.Parent.BackColor, smoothSize))
using (Pen penBorder = new Pen(borderColor, borderSize))
{
pevent.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
//Superfície do Botão
this.Region = new Region(pathSurface);
//Desenha a superfície da borda para um resultado em alta definição
pevent.Graphics.DrawPath(penSurface, pathSurface);
//Borda do Botão
if (borderSize >= 1)
//Controle de Desenho da Borda
pevent.Graphics.DrawPath(penBorder, pathBorder);
}
}
else //Botão Normal
{
pevent.Graphics.SmoothingMode = SmoothingMode.None;
//Superfície do Botão
this.Region = new Region(rectSurface);
//Borda do Botão
if (borderSize >= 1)
{
using (Pen penBorder = new Pen(borderColor, borderSize))
{
penBorder.Alignment = PenAlignment.Inset;
pevent.Graphics.DrawRectangle(penBorder, 0, 0, this.Width - 1, this.Height - 1);
}
}
}
}
private void Button_Resize(object sender, EventArgs e)
{
if (borderRadius > this.Height)
borderRadius = this.Height;
}
//Event methods
//-> Default event
private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (OnSelectedIndexChanged != null)
OnSelectedIndexChanged.Invoke(sender, e);
//Refresh text
lblText.Text = cmbList.Text;
}
//-> Draw icon
private void Icon_Paint(object sender, PaintEventArgs e)
{
//Fields
int iconWidht = 14;
int iconHeight = 6;
var rectIcon = new Rectangle((btnIcon.Width - iconWidht) / 2, (btnIcon.Height - iconHeight) / 2, iconWidht, iconHeight);
Graphics graph = e.Graphics;
//Draw arrow down icon
using (GraphicsPath path = new GraphicsPath())
using (Pen pen = new Pen(iconColor, 2))
{
graph.SmoothingMode = SmoothingMode.AntiAlias;
path.AddLine(rectIcon.X, rectIcon.Y, rectIcon.X + (iconWidht / 2), rectIcon.Bottom);
path.AddLine(rectIcon.X + (iconWidht / 2), rectIcon.Bottom, rectIcon.Right, rectIcon.Y);
graph.DrawPath(pen, path);
}
}
//-> Items actions
private void Icon_Click(object sender, EventArgs e)
{
//Open dropdown list
cmbList.Select();
cmbList.DroppedDown = true;
}
private void Surface_Click(object sender, EventArgs e)
{
//Attach label click to user control click
this.OnClick(e);
//Select combo box
cmbList.Select();
if (cmbList.DropDownStyle == ComboBoxStyle.DropDownList)
cmbList.DroppedDown = true;//Open dropdown list
}
private void ComboBox_TextChanged(object sender, EventArgs e)
{
//Refresh text
lblText.Text = cmbList.Text;
}
//->Attach label events to user control event
private void Surface_MouseLeave(object sender, EventArgs e)
{
this.OnMouseLeave(e);
}
private void Surface_MouseEnter(object sender, EventArgs e)
{
this.OnMouseEnter(e);
}
//::::+
//Overridden methods
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
AdjustComboBoxDimensions();
}
}
}