如何使用DropDownStyle(DropDownList)为ComboBox创建自定义外边框

时间:2017-02-11 14:16:37

标签: c# combobox controls

当comboBox处于活动状态时,我需要使用DropDownStyle = DropDownList为ComboBox绘制自定义外边框。 want the combobox to appear like this image

我尝试过WndProc,但它在DropDownList Style中出错。

1 个答案:

答案 0 :(得分:0)

我对WinForms中的combobox风格并不是很了解,但你可以使用System.Drawings.Graphics来完成你想要的。请检查以下代码段:

 public partial class Form1 : Form
{
    System.Drawing.Graphics graphics;
    System.Drawing.Rectangle rectangle;

    public Form1()
    {
        InitializeComponent();
        comboBox1.Enter += ComboBox1_Enter;
        comboBox1.Leave += ComboBox1_Leave;
    }

    private void ComboBox1_Leave(object sender, EventArgs e)
    {
        graphics.Clear(this.BackColor);
    }

    private void ComboBox1_Enter(object sender, EventArgs e)
    {
        ChangeActiveControlStyle((Control)sender);
    }

    private void ChangeActiveControlStyle(Control control)
    {
        graphics = this.CreateGraphics();          
        rectangle = new System.Drawing.Rectangle(control.Location.X -2, control.Location.Y-2, control.Width+4, control.Height+4);
        graphics.FillRectangle(Brushes.Yellow, rectangle);
    }
}