简体   繁体   English

在焦点上更改组合框的边框颜色

[英]Changing the border color of a Combobox on focus

I have a custom ComboBox .我有一个自定义ComboBox

I want to give a custom BorderColor to the ComboBox , when it is focused.我想在ComboBox聚焦时为其提供自定义BorderColor

To do this, I'm using the following code:为此,我使用以下代码:

Graphics g = Graphics.FromHwnd(Handle);
Rectangle bounds = new Rectangle(0, 0, Width, Height);

ControlPaint.DrawBorder(g, bounds, BaseConfigurations.StyleColor, ButtonBorderStyle.Solid);

The thing is, if I use the code inside the MouseHover event when I move the mouse on the ComboBox control, I can see that it works.问题是,如果我在ComboBox控件上移动鼠标时使用MouseHover事件中的代码,我可以看到它有效。 However, the same code does not work inside the GotFocus Event, and I can't figure out why.. any help is appreciated.但是,相同的代码在GotFocus事件中不起作用,我不知道为什么.. 任何帮助表示赞赏。

This is a simple Class that inherits from ComboBox and exposes two properties that allows to set the Active and Inactive border of the Control.这是一个简单的类,它继承自ComboBox并公开两个允许设置控件的活动和非活动边框的属性。

The painting is done using the Parent Form Paint() event, invalidating only the area around the selected control.绘制是使用父窗体Paint()事件完成的,仅使所选控件周围的区域无效。

The Parent Paint() event is subscribed in the custom ComboBox OnHandleCreated() event, along with the control's Enter() , Leave() and Move() events. Parent Paint()事件与控件的Enter()Leave()Move()事件一起在自定义 ComboBox OnHandleCreated()事件中订阅。
Subscribing the Move() event is required to paint a transparent border, otherwise the border will remain painted on the Parent client area while dragging the control at Design time.需要订阅Move()事件才能绘制透明边框,否则在设计时拖动控件时边框将保持绘制在父客户区上。

I've also added DropDownBackColor() and DropDownForeColor() properties, which become active if the custom ComboBox DrawMode is set to OwnerDrawVariable (as usual).我还添加了DropDownBackColor()DropDownForeColor()属性,如果自定义 ComboBox DrawMode设置为OwnerDrawVariable (像往常一样),它们就会变为活动状态。

This is how it looks like:这是它的样子:

在此处输入图片说明

public class CustomCombo : ComboBox
{
    private Color ActionBorderColor = Color.Empty;
    public CustomCombo()
    {
        InitializeComponent();
    }

    public Color BorderActive { get; set; }
    public Color BorderInactive { get; set; }
    public Color DropDownBackColor { get; set; }
    public Color DropDownForeColor { get; set; }

    private void InitializeComponent()
    {
        this.DrawMode = DrawMode.OwnerDrawVariable;
        this.BorderActive = Color.OrangeRed;
        this.BorderInactive = Color.Transparent;
        this.DropDownBackColor = Color.FromKnownColor(KnownColor.Window);
        this.DropDownForeColor = this.ForeColor;
        this.HandleCreated += new EventHandler(this.OnControlHandle);
    }

    protected void OnControlHandle(object sender, EventArgs args)
    {
        Form parent = this.FindForm();
        parent.Paint += new PaintEventHandler(this.ParentPaint);
        this.Enter += (s, ev) => { this.InvalidateParent(BorderActive); };
        this.Leave += (s, ev) => { this.InvalidateParent(BorderInactive); };
        this.Move += (s, ev) => { this.InvalidateParent(Color.Transparent); };
        base.OnHandleCreated(e);
    }

    private void InvalidateParent(Color bordercolor)
    {
        ActionBorderColor = bordercolor;
        Rectangle rect = this.Bounds;
        rect.Inflate(2, 2);
        this.FindForm().Invalidate(rect);
    }

    protected void ParentPaint(object sender, PaintEventArgs e)
    {
        Rectangle rect = this.Bounds;
        rect.Inflate(1, 1);
        using (Pen pen = new Pen(ActionBorderColor, 1))
            e.Graphics.DrawRectangle(pen, rect);
    }

    protected override void OnDrawItem(DrawItemEventArgs e)
    {
        using (SolidBrush bkBrush = new SolidBrush(this.DropDownBackColor))
            e.Graphics.FillRectangle(bkBrush, e.Bounds);
        using (SolidBrush foreBbrush = new SolidBrush(this.DropDownForeColor))
            e.Graphics.DrawString(this.Items[e.Index].ToString(),
                                  this.Font, foreBbrush, new PointF(e.Bounds.X, e.Bounds.Y));
        e.DrawFocusRectangle();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM