【问题标题】:Change combobox drop down list border color in C#在 C# 中更改组合框下拉列表边框颜色
【发布时间】:2013-12-30 01:05:26
【问题描述】:

是否可以在c#中更改组合框下拉列表的边框颜色?

我想将白色边框更改为较暗的阴影以匹配深色方案。我搜索了 .net 文档并找到了 DropDownList.BorderStyle 属性。但是,我不确定这是否可行。我正在使用 WinForms。

这是我用于组合框的类:

public class FlattenCombo : ComboBox
{
    private Brush BorderBrush = new SolidBrush(SystemColors.WindowFrame);
    private Brush ArrowBrush = new SolidBrush(SystemColors.ControlText);
    private Brush DropButtonBrush = new SolidBrush(SystemColors.Control);

    private Color _borderColor = Color.Black;
    private ButtonBorderStyle _borderStyle = ButtonBorderStyle.Solid;
    private static int WM_PAINT = 0x000F; 

    private Color _ButtonColor = SystemColors.Control;

    public Color ButtonColor
    {
        get { return _ButtonColor; }
        set
        {
            _ButtonColor = value;
            DropButtonBrush = new SolidBrush(this.ButtonColor);
            this.Invalidate();
        }
    }

    protected override void WndProc(ref Message m)
    {
        base.WndProc(ref m);

        switch (m.Msg)
        {
            case 0xf:
                Graphics g = this.CreateGraphics();
                Pen p = new Pen(Color.Black);
                g.FillRectangle(BorderBrush, this.ClientRectangle);

                //Draw the background of the dropdown button
                Rectangle rect = new Rectangle(this.Width - 17, 0, 17, this.Height);
                g.FillRectangle(DropButtonBrush, rect);

                //Create the path for the arrow
                System.Drawing.Drawing2D.GraphicsPath pth = new System.Drawing.Drawing2D.GraphicsPath();
                PointF TopLeft = new PointF(this.Width - 13, (this.Height - 5) / 2);
                PointF TopRight = new PointF(this.Width - 6, (this.Height - 5) / 2);
                PointF Bottom = new PointF(this.Width - 9, (this.Height + 2) / 2);
                pth.AddLine(TopLeft, TopRight);
                pth.AddLine(TopRight, Bottom);

                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

                //Determine the arrow's color.
                if (this.DroppedDown)
                {
                    ArrowBrush = new SolidBrush(SystemColors.HighlightText);
                }
                else
                {
                    ArrowBrush = new SolidBrush(SystemColors.ControlText);
                }

                //Draw the arrow
                g.FillPath(ArrowBrush, pth);

                break;
            default:
                break;
        }
    }

    [Category("Appearance")]
    public Color BorderColor
    {
        get { return _borderColor; }
        set
        {
            _borderColor = value;
            Invalidate(); // causes control to be redrawn
        }
    }

    [Category("Appearance")]
    public ButtonBorderStyle BorderStyle
    {
        get { return _borderStyle; }
        set
        {
            _borderStyle = value;
            Invalidate();
        }
    }

    protected override void OnLostFocus(System.EventArgs e)
    {
        base.OnLostFocus(e);
        this.Invalidate();
    }

    protected override void OnGotFocus(System.EventArgs e)
    {
        base.OnGotFocus(e);
        this.Invalidate();
    }
    protected override void OnResize(EventArgs e)
    {
        base.OnResize(e);
        this.Invalidate();
    }
}

【问题讨论】:

  • 你完成了这个组合框控件吗?如果是的话,我可以得到它吗?

标签: c# winforms combobox


【解决方案1】:

虽然 FlatComboBoxAdapter 确实不可用。仍然可以捕获 WM_CTLCOLORLISTBOX 窗口消息并将本机 GDI 矩形应用于下拉边框。这是一项工作,但它确实完成了工作。 (如果您想跳过此部分,请在底部提供一个示例链接)

首先,如果您不熟悉 WM_CTLCOLORLISTBOX 窗口消息,它是这样描述的:

"在系统绘制列表框之前发送到列表框的父窗口。通过响应此消息,父窗口可以使用指定的显示设备设置列表框的文本和背景颜色上下文句柄。”

消息常量的定义如下:

const int WM_CTLCOLORLISTBOX = 0x0134;

定义消息常量后,在自定义 ComboBox 的覆盖 WndProc() 事件中为其设置条件:

protected override void WndProc(ref Message m)
{
    // Filter window messages
    switch (m.Msg)
    {
        // Draw a custom color border around the drop down pop-up
        case WM_CTLCOLORLISTBOX:
            base.WndProc(ref m);
            DrawNativeBorder(m.LParam);
            break;

        default: base.WndProc(ref m); break;
    }
}

DrawNativeBorder() 方法是我们要使用 Win API 来绘制矩形的地方。它接受下拉菜单的句柄作为参数。然而,在我们这样做之前,我们需要定义将要使用的本地方法、枚举和结构:

public enum PenStyles
{
    PS_SOLID = 0,
    PS_DASH = 1,
    PS_DOT = 2,
    PS_DASHDOT = 3,
    PS_DASHDOTDOT = 4
}

public enum ComboBoxButtonState
{
    STATE_SYSTEM_NONE = 0,
    STATE_SYSTEM_INVISIBLE = 0x00008000,
    STATE_SYSTEM_PRESSED = 0x00000008
}

[StructLayout(LayoutKind.Sequential)]
public struct COMBOBOXINFO
{
    public Int32 cbSize;
    public RECT rcItem;
    public RECT rcButton;
    public ComboBoxButtonState buttonState;
    public IntPtr hwndCombo;
    public IntPtr hwndEdit;
    public IntPtr hwndList;
}

[DllImport("user32.dll")]
public static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);

[DllImport("user32.dll")]
public static extern IntPtr GetWindowDC(IntPtr hWnd);

[DllImport("user32.dll")]
public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);

[DllImport("user32.dll")]
public static extern IntPtr SetFocus(IntPtr hWnd);

[DllImport("user32.dll")]
public static extern bool GetComboBoxInfo(IntPtr hWnd, ref COMBOBOXINFO pcbi);

[DllImport("gdi32.dll")]
public static extern int ExcludeClipRect(IntPtr hdc, int nLeftRect, int nTopRect, int nRightRect, int nBottomRect);

[DllImport("gdi32.dll")]
public static extern IntPtr CreatePen(PenStyles enPenStyle, int nWidth, int crColor);

[DllImport("gdi32.dll")]
public static extern IntPtr SelectObject(IntPtr hdc, IntPtr hObject);

[DllImport("gdi32.dll")]
public static extern bool DeleteObject(IntPtr hObject);

[DllImport("gdi32.dll")]
public static extern void Rectangle(IntPtr hdc, int X1, int Y1, int X2, int Y2);

public static int RGB(int R, int G, int B)
{
    return (R | (G << 8) | (B << 16));
}

[Serializable, StructLayout(LayoutKind.Sequential)]
public struct RECT
{
    public int Left;
    public int Top;
    public int Right;
    public int Bottom;

    public RECT(int left_, int top_, int right_, int bottom_)
    {
        Left = left_;
        Top = top_;
        Right = right_;
        Bottom = bottom_;
    }

    public override bool Equals(object obj)
    {
        if (obj == null || !(obj is RECT))
        {
            return false;
        }
        return this.Equals((RECT)obj);
    }

    public bool Equals(RECT value)
    {
        return this.Left == value.Left &&
        this.Top == value.Top &&
        this.Right == value.Right &&
        this.Bottom == value.Bottom;
    }

    public int Height
    {
        get
        {
             return Bottom - Top + 1;
        }
    }

    public int Width
    {
        get
        {
            return Right - Left + 1;
        }
    }

    public Size Size { get { return new Size(Width, Height); } }
    public Point Location { get { return new Point(Left, Top); } }
    // Handy method for converting to a System.Drawing.Rectangle
    public System.Drawing.Rectangle ToRectangle()
    {
        return System.Drawing.Rectangle.FromLTRB(Left, Top, Right, Bottom);
    }

    public static RECT FromRectangle(Rectangle rectangle)
    {
        return new RECT(rectangle.Left, rectangle.Top, rectangle.Right, rectangle.Bottom);
    }

    public void Inflate(int width, int height)
    {
        this.Left -= width;
        this.Top -= height;
        this.Right += width;
        this.Bottom += height;
    }

    public override int GetHashCode()
    {
        return Left ^ ((Top << 13) | (Top >> 0x13))
                          ^ ((Width << 0x1a) | (Width >> 6))
                          ^ ((Height << 7) | (Height >> 0x19));
    }

    public static implicit operator Rectangle(RECT rect)
    {
        return System.Drawing.Rectangle.FromLTRB(rect.Left, rect.Top, rect.Right, rect.Bottom);
    }

    public static implicit operator RECT(Rectangle rect)
    {
        return new RECT(rect.Left, rect.Top, rect.Right, rect.Bottom);
    }
}

DrawNativeBorder() 方法定义为:

/// <summary>
/// Non client area border drawing
/// </summary>
/// <param name="handle">The handle to the control</param>
public static void DrawNativeBorder(IntPtr handle)
{
    // Define the windows frame rectangle of the control
    RECT controlRect;
    GetWindowRect(handle, out controlRect);
    controlRect.Right -= controlRect.Left; controlRect.Bottom -= controlRect.Top;
    controlRect.Top = controlRect.Left = 0;

    // Get the device context of the control
    IntPtr dc = GetWindowDC(handle);

    // Define the client area inside the control rect so it won't be filled when drawing the border
    RECT clientRect = controlRect;
    clientRect.Left += 1;
    clientRect.Top += 1;
    clientRect.Right -= 1;
    clientRect.Bottom -= 1;
    ExcludeClipRect(dc, clientRect.Left, clientRect.Top, clientRect.Right, clientRect.Bottom);

    // Create a pen and select it
    Color borderColor = Color.Magenta;
    IntPtr border = WinAPI.CreatePen(WinAPI.PenStyles.PS_SOLID, 1, RGB(borderColor.R,
        borderColor.G, borderColor.B));

    // Draw the border rectangle
    IntPtr borderPen = SelectObject(dc, border);
    Rectangle(dc, controlRect.Left, controlRect.Top, controlRect.Right, controlRect.Bottom);
    SelectObject(dc, borderPen);
    DeleteObject(border);

    // Release the device context
    ReleaseDC(handle, dc);
    SetFocus(handle);
}

我使用洋红色来清楚地显示这幅画。这将为边框画做。然而,还有一个问题。当下拉菜单显示并且鼠标没有移动到下拉项目上时,默认边框仍然显示。要处理该问题,您必须确定下拉菜单何时完全打开。然后发送我们自己的 WM_CTLCOLORLISTBOX 消息来更新边框。

我使用计时器粗略地检查下拉显示时刻。我尝试了各种其他选择,但没有成功。老实说,如果有人有更好的解决方案,那就太好了。

您需要一个计时器来检查下拉菜单何时完全下降:

private Timer _dropDownCheck = new Timer();

计时器是自定义组合框中的一个字段。在 InitializeComponent() 之后在您的自定义组合框构造函数中设置它:

_dropDownCheck.Interval = 100;
_dropDownCheck.Tick += new EventHandler(dropDownCheck_Tick);

覆盖自定义组合框的 OnDropDown() 事件,并设置计时器滴答事件:

/// <summary>
/// On drop down
/// </summary>
protected override void OnDropDown(EventArgs e)
{
    base.OnDropDown(e);

    // Start checking for the dropdown visibility
    _dropDownCheck.Start();
}

/// <summary>
/// Checks when the drop down is fully visible
/// </summary>
private void dropDownCheck_Tick(object sender, EventArgs e)
{
    // If the drop down has been fully dropped
    if (DroppedDown)
    {
        // Stop the time, send a listbox update
        _dropDownCheck.Stop();
        Message m = GetControlListBoxMessage(this.Handle);
        WndProc(ref m);
    }
}

最后,创建以下方法来获取下拉句柄并创建 WM_CTLCOLORLISTBOX 消息以发送到控件:

/// <summary>
/// Creates a default WM_CTLCOLORLISTBOX message
/// </summary>
/// <param name="handle">The drop down handle</param>
/// <returns>A WM_CTLCOLORLISTBOX message</returns>
public Message GetControlListBoxMessage(IntPtr handle)
{
    // Force non-client redraw for focus border
    Message m = new Message();
    m.HWnd = handle;
    m.LParam = GetListHandle(handle);
    m.WParam = IntPtr.Zero;
    m.Msg = WM_CTLCOLORLISTBOX;
    m.Result = IntPtr.Zero;
    return m;
}

/// <summary>
/// Gets the list control of a combo box
/// </summary>
/// <param name="handle">Handle of the combo box itself</param>
/// <returns>A handle to the list</returns>
public static IntPtr GetListHandle(IntPtr handle)
{
    COMBOBOXINFO info;
    info = new COMBOBOXINFO();
    info.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(info);
    return GetComboBoxInfo(handle, ref info) ? info.hwndList : IntPtr.Zero;
}

差不多就是这样,如果您仍然感到困惑,那么查看我提供的这个示例 VS 2010 自定义组合框项目中的控件可能会更容易:

http://www.pyxosoft.com/downloads/CustomComboBoxBorderColor.zip

【讨论】:

    【解决方案2】:

    我为此纠结了太久。我从您之前提出的问题中看到,您的代码来自:http://www.codeproject.com/Articles/2433/Flatten-that-Combobox 并设置了 BackColor:

    protected override void WndProc(ref Message m)
    {
        base.WndProc(ref m);
    
        switch (m.Msg)
        {
            case 0xf:
                base.BackColor = Color.Black;
    

    使用 WndProc,这是我得到的最接近的,但它没有为下拉列表的 Popup/ItemSelection 边框着色:

    ...

    if (this.DroppedDown)
    {
        ArrowBrush = new SolidBrush(SystemColors.HighlightText);
    
        Rectangle dropDownBounds = new Rectangle(0, 0, Width,Height + DropDownHeight );
        //ControlPaint.DrawBorder(g, dropDownBounds, _borderColor, _borderStyle);
        ControlPaint.DrawBorder(g, dropDownBounds, _borderColor,1, ButtonBorderStyle.Dotted ,Color.GreenYellow,1,ButtonBorderStyle.Solid ,Color.Gold,1,ButtonBorderStyle.Dashed,Color.HotPink,1,ButtonBorderStyle.Solid);
    
    }
    

    事实证明,执行此操作所需的类 FlatComboBoxAdapter 是私有的,建议使用 WPF: ComboBox DropDown-Area Border Color

    更多信息: Combobox borderstyle - 但即使有 LarsTech 和 Hans 的建议(使用非客户端绘制消息),它仍然无法正常工作并且闪烁可怕。

    除WPF之外的其他建议,重写Combobox .Net Framework代码:http://www.dotnetframework.org/default.aspx/FX-1434/FX-1434/1@0/untmp/whidbey/REDBITS/ndp/fx/src/WinForms/Managed/System/WinForms/ComboBox@cs/2/ComboBox@cs

    【讨论】:

    • 感谢您的回复,杰里米。我很可能会求助于使用 WPF 创建组合框。感谢您的第二意见。
    猜你喜欢
    • 2021-09-03
    • 2018-12-27
    • 1970-01-01
    • 1970-01-01
    • 2016-04-24
    • 2015-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多