【问题标题】:Changing the border color of a Combobox on focus更改焦点上组合框的边框颜色
【发布时间】:2018-03-15 14:44:13
【问题描述】:

我有一个自定义的ComboBox

我想给ComboBox一个自定义的BorderColor,当它被聚焦时。

为此,我使用以下代码:

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

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

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

【问题讨论】:

  • 该代码只是在控件顶部喷溅像素,您通常无法预测当 ComboBox 重新绘制自身时它们何时会被透支。只需将窗口拖出屏幕并返回即可查看另一种基本故障模式。从那里开始变得更糟,文本框部分在不使用绘制事件的情况下绘制。 30年前重要的罪行。你不能让这个可靠的,容器可以在它周围画一个矩形。
  • 嗯,我注意到(有人让我)我在这里发布了(非常相似的)另一个网站上提出的问题的答案(这得到了你的 :)。如果碰巧您已经看到了这一点,并且代码看起来与图形演示有些(!)不同,那么它现在是正确的代码。 (对不起)。
  • 您可以使用这里分享的解决方案:Change ComboBox Border Color - Flash when SelectedIndex changed.

标签: c# winforms graphics combobox custom-controls


【解决方案1】:

这是一个简单的类,它继承自 ComboBox,并公开了两个允许设置控件的活动和非活动边框的属性。

使用父窗体Paint() 事件完成绘制,仅使选定控件周围的区域无效。

在自定义组合框OnHandleCreated() 事件中订阅了父Paint() 事件,以及控件的Enter()Leave()Move() 事件。
绘制透明边框需要订阅Move() 事件,否则在设计时拖动控件时,边框将保持绘制在父客户区。

我还添加了DropDownBackColor()DropDownForeColor() 属性,如果自定义组合框DrawMode 设置为OwnerDrawVariable(像往常一样),它们将变为活动状态。

看起来是这样的:


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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-05
    • 1970-01-01
    • 2019-06-29
    • 2022-11-27
    • 1970-01-01
    • 2020-10-22
    • 1970-01-01
    相关资源
    最近更新 更多