You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让Simple ComboBox编辑框高亮颜色与下拉项保持一致

问题描述

我已经成功修改了ComboBox下拉列表项的高亮选中颜色,但comboBox_DrawItem()事件触发后,编辑框仍显示默认蓝色高亮,怎么让编辑框的选中颜色和下拉项保持一致?

ComboBox编辑框与下拉项高亮颜色不一致

当前使用的代码

private void comboBoxChoices_DrawItem(object sender, DrawItemEventArgs e)
{
    if (e.Index < 0)
        return;

    ComboBox combo = sender as ComboBox;
    SolidBrush brushForeColor = new SolidBrush(combo.ForeColor);

    if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
    {
        e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(64, 64, 64)), e.Bounds);
        brushForeColor = new SolidBrush(Color.White);
    }
    else
    {
        e.Graphics.FillRectangle(new SolidBrush(combo.BackColor), e.Bounds);
    }

    e.Graphics.DrawString(((WorkflowDisplay)combo.Items[e.Index]).Display, e.Font,
                          brushForeColor,
                          new Point(e.Bounds.X, e.Bounds.Y));

    e.DrawFocusRectangle();
}
解决方法

DrawItem事件仅负责渲染下拉列表里的选项,编辑框的高亮是独立渲染的,需要额外处理Paint事件来统一样式:

步骤1:确认ComboBox样式

确保ComboBox的DropDownStyle设置为DropDown(如果是DropDownList,编辑框为只读,处理逻辑略有不同)。

步骤2:添加Paint事件处理

private void comboBoxChoices_Paint(object sender, PaintEventArgs e)
{
    ComboBox combo = sender as ComboBox;
    if (combo == null) return;

    // 定义编辑框的绘制区域
    Rectangle clientRect = combo.ClientRectangle;
    clientRect.Inflate(-1, -1); // 去掉边框占位

    bool isTextSelected = combo.Focused && combo.SelectionLength > 0;

    // 绘制背景/高亮
    using (SolidBrush bgBrush = new SolidBrush(isTextSelected ? Color.FromArgb(64, 64, 64) : combo.BackColor))
    {
        e.Graphics.FillRectangle(bgBrush, clientRect);
    }

    // 绘制选中文本的高亮区域(精确匹配选中范围)
    if (isTextSelected)
    {
        int startPos = combo.SelectionStart;
        int endPos = startPos + combo.SelectionLength;
        Point startPoint = combo.GetPositionFromCharIndex(startPos);
        Point endPoint = combo.GetPositionFromCharIndex(endPos);
        
        Rectangle selectionRect = new Rectangle(
            startPoint.X, 
            clientRect.Top, 
            endPoint.X - startPoint.X, 
            clientRect.Height
        );

        using (SolidBrush highlightBrush = new SolidBrush(Color.FromArgb(64, 64, 64)))
        {
            e.Graphics.FillRectangle(highlightBrush, selectionRect);
        }
    }

    // 绘制文本
    using (SolidBrush textBrush = new SolidBrush(isTextSelected ? Color.White : combo.ForeColor))
    {
        StringFormat format = new StringFormat { LineAlignment = StringAlignment.Center };
        e.Graphics.DrawString(combo.Text, combo.Font, textBrush, clientRect, format);
    }
}

额外注意事项

  • 所有GDI+对象(如SolidBrush)用using包裹,避免资源泄漏;
  • 如果是DropDownList样式,可通过重写WndProc拦截WM_CTLCOLORLISTBOX消息来自定义渲染;
  • 不建议修改SystemColors,会全局改变应用内所有控件的高亮样式。

内容的提问来源于Stack Exchange,提问作者TravelDev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 15:19:53