【问题标题】:Windows form combobox custom form colorWindows 窗体组合框自定义窗体颜色
【发布时间】:2013-12-28 07:49:53
【问题描述】:

我一直在玩自定义 WinForm 组合框...到目前为止,我有以下内容:

使用此代码:

public class ComboBoxWithBorder : ComboBox
{
    private Color _borderColor = Color.Black;
    private ButtonBorderStyle _borderStyle = ButtonBorderStyle.Solid;
    private static int WM_PAINT = 0x000F;

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

        if (m.Msg == WM_PAINT)
        {
            Graphics g = Graphics.FromHwnd(Handle);
            Rectangle bounds = new Rectangle(0, 0, Width, Height);
            ControlPaint.DrawBorder(g, bounds, _borderColor, _borderStyle);
        }
    }

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

但是,我正在尝试实现类似的目标:

是否可以将白色下拉框的背景颜色更改为较深的颜色?


是否可以将下拉列表边框从白色更改为其他颜色?

【问题讨论】:

    标签: c# winforms combobox


    【解决方案1】:

    试试这个课程

    Refer

    using Microsoft.VisualBasic;
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Data;
    using System.Diagnostics;
    public class FlatCombo : ComboBox
    {
    
        private Brush BorderBrush = new SolidBrush(SystemColors.Window);
        private Brush ArrowBrush = new SolidBrush(SystemColors.ControlText);
        private Brush DropButtonBrush = new SolidBrush(SystemColors.Control);
    
        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(m);
    
            switch (m.Msg) {
                case 0xf:
                    //Paint the background. Only the borders
                    //will show up because the edit
                    //box will be overlayed
                    Graphics g = this.CreateGraphics;
                    Pen p = new Pen(Color.White, 2);
                    g.FillRectangle(BorderBrush, this.ClientRectangle);
    
                    //Draw the background of the dropdown button
                    Rectangle rect = new Rectangle(this.Width - 15, 3, 12, this.Height - 6);
                    g.FillRectangle(DropButtonBrush, rect);
    
                    //Create the path for the arrow
                    Drawing2D.GraphicsPath pth = new 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 = 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; // TODO: might not be correct. Was : Exit Select
    
                    break;
            }
        }
    
        //Override mouse and focus events to draw
        //proper borders. Basically, set the color and Invalidate(),
        //In general, Invalidate causes a control to redraw itself.
        #region "Mouse and focus Overrides"
        protected override void OnMouseEnter(System.EventArgs e)
        {
            base.OnMouseEnter(e);
            BorderBrush = new SolidBrush(SystemColors.Highlight);
            this.Invalidate();
        }
    
        protected override void OnMouseLeave(System.EventArgs e)
        {
            base.OnMouseLeave(e);
            if (this.Focused)
                return;
            BorderBrush = new SolidBrush(SystemColors.Window);
            this.Invalidate();
        }
    
        protected override void OnLostFocus(System.EventArgs e)
        {
            base.OnLostFocus(e);
            BorderBrush = new SolidBrush(SystemColors.Window);
            this.Invalidate();
        }
    
        protected override void OnGotFocus(System.EventArgs e)
        {
            base.OnGotFocus(e);
            BorderBrush = new SolidBrush(SystemColors.Highlight);
            this.Invalidate();
        }
    
        protected override void OnMouseHover(System.EventArgs e)
        {
            base.OnMouseHover(e);
            BorderBrush = new SolidBrush(SystemColors.Highlight);
            this.Invalidate();
        }
        #endregion
    }
    

    【讨论】:

    • 经过一些编辑,这段代码运行良好。现在,我唯一需要修复的是下拉列表边框本身(请参阅上面的问题帖子)...基本上我想将白色边框更改为其他内容。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多