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

C# 如何使用DropDownStyle(DropDownList)为ComboBox创建自定义外部边框,c#,combobox,controls,C#,Combobox,Controls,例如,当ComboBox处于活动状态时,我需要使用DropDownStyle=DropDownList为ComboBox绘制自定义外部边框。 我尝试了WndProc,但它在DropDownList样式中出现了错误。我对WinForms中combobox的样式了解不多,但您可以使用System.Drawings.Graphics来完成您想要的操作。检查以下代码段: public partial class Form1 : Form { System.Drawing.Graphics g

例如,当ComboBox处于活动状态时,我需要使用DropDownStyle=DropDownList为ComboBox绘制自定义外部边框。


我尝试了WndProc,但它在DropDownList样式中出现了错误。

我对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);
    }
}