更改焦点上的组合框的边框颜色 [英] Changing the border color of a Combobox on focus

查看:102
本文介绍了更改焦点上的组合框的边框颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义 ComboBox

我想给一个自定义 BorderColor ComboBox 时,

I want to give a custom BorderColor to the ComboBox, when it is focused.

为此, m使用以下代码:

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);

问题是,如果我使用 MouseHover 事件
当我将鼠标移到 ComboBox 控件上时,可以看到它有效。
但是,相同的代码在 GotFocus 事件中不起作用,我不知道为什么。任何帮助都应得到感谢。

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. However, the same code does not work inside the GotFocus Event, and I can't figure out why.. any help is appreciated.

推荐答案

这是一个简单的类,继承自 ComboBox ,并公开了两个属性,可用于设置

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.

使用父窗体 Paint()事件,仅使所选控件周围的区域无效。

The painting is done using the Parent Form Paint() event, invalidating only the area around the selected control.

Paint()事件为订阅自定义ComboBox OnHandleCreated()事件,以及控件的 Enter() Leave() Move()事件。

订阅 Move()事件需要绘制透明边框,否则在设计时拖动控件时,边框仍将绘制在父级客户区域上。

The Parent Paint() event is subscribed in the custom ComboBox OnHandleCreated() event, along with the control's Enter(), Leave() and Move() events.
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.

我还添加了 DropDownBackColor() DropDownForeColor()属性,如果将自定义ComboBox DrawMode 设置为 OwnerDrawVariable (通常)。

I've also added DropDownBackColor() and DropDownForeColor() properties, which become active if the custom ComboBox DrawMode is set to OwnerDrawVariable (as usual).

它是这样的:

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();
    }
}

这篇关于更改焦点上的组合框的边框颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆