如何更改组合框下拉按钮的颜色 [英] How to change the ComboBox dropdown button color

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

问题描述

如何更改滑动按钮的颜色?不是边框颜色,也不是滑动项目的颜色.

我已经更改了幻灯片项目的颜色

有什么方法可以改变颜色?

解决方案

平面组合框-更改边框颜色和下拉按钮颜色

您需要亲自处理 WM_PAINT 并绘制边框和下拉矩形.内部

这是代码:

 使用系统;使用System.ComponentModel;使用System.Drawing;使用System.Runtime.InteropServices;使用System.Windows.Forms;公共类FlatComboBox:ComboBox{私人颜色borderColor = Color.Gray;[DefaultValue(typeof(Color),"Gray"))公共颜色BorderColor{得到{return borderColor;}放{如果(borderColor!=值){borderColor =值;Invalidate();}}}私人颜色buttonColor = Color.LightGray;[DefaultValue(typeof(Color),"LightGray"))公共颜色ButtonColor{得到{return buttonColor;}放{如果(buttonColor!=值){buttonColor =值;Invalidate();}}}受保护的覆盖无效WndProc(参考消息m){如果(m.Msg == WM_PAINT& DropDownStyle!= ComboBoxStyle.Simple){var clientRect = ClientRectangle;var dropDownButtonWidth = SystemInformation.Horizo​​ntalScrollBarArrowWidth;var externalBorder = new Rectangle(clientRect.Location,new Size(clientRect.Width-1,clientRect.Height-1));var innerBorder =新Rectangle(outerBorder.X + 1,outerBorder.Y +1,outsideBorder.Width-dropDownButtonWidth-2,outerBorder.Height-2);var innerInnerBorder = new Rectangle(innerBorder.X + 1,innerBorder.Y + 1,innerBorder.Width-2,innerBorder.Height-2);var dropDownRect = new Rectangle(innerBorder.Right + 1,innerBorder.Y,dropDownButtonWidth,innerBorder.Height +1);如果(RightToLeft == RightToLeft.Yes){innerBorder.X = clientRect.Width-innerBorder.Right;innerInnerBorder.X = clientRect.Width-innerInnerBorder.Right;dropDownRect.X = clientRect.Width-dropDownRect.Right;dropDownRect.Width + = 1;}var innerBorderColor =启用?BackColor:SystemColors.Control;var externalBorderColor =启用?BorderColor:SystemColors.ControlDark;var buttonColor =启用?ButtonColor:SystemColors.Control;var middle = new Point(dropDownRect.Left + dropDownRect.Width/2,dropDownRect.Top + dropDownRect.Height/2);var arrow =新Point []{新点(中间X-3,中间Y-2),新点(中间X + 4,中间Y-2),新的Point(middle.X,middle.Y + 2)};var ps = new PAINTSTRUCT();bool shoulEndPaint = false;IntPtr dc;如果(m.WParam == IntPtr.Zero){dc = BeginPaint(Handle,ref ps);m.WParam = dc;shoulEndPaint = true;}别的{dc = m.WParam;}var rgn = CreateRectRgn(innerInnerBorder.Left,innerInnerBorder.Top,innerInnerBorder.Right,innerInnerBorder.Bottom);SelectClipRgn(dc,rgn);DefWndProc(ref m);DeleteObject(rgn);rgn = CreateRectRgn(clientRect.Left,clientRect.Top,clientRect.Right,clientRect.Bottom);SelectClipRgn(dc,rgn);使用(var g = Graphics.FromHdc(dc)){使用(var b = new SolidBrush(buttonColor)){g.FillRectangle(b,dropDownRect);}使用(var b = new SolidBrush(outerBorderColor)){g.FillPolygon(b,arrow);}使用(var p = new Pen(innerBorderColor)){g.DrawRectangle(p,innerBorder);g.DrawRectangle(p,innerInnerBorder);}使用(var p = new Pen(outerBorderColor)){g.DrawRectangle(p,externalBorder);}}如果(shoulEndPaint)EndPaint(Handle,ref ps);DeleteObject(rgn);}别的base.WndProc(ref m);}私有const int WM_PAINT = 0xF;[StructLayout(LayoutKind.Sequential)]公共结构{public int L,T,R,B;}[StructLayout(LayoutKind.Sequential)]公共结构PAINTSTRUCT{公共IntPtr hdc;公众嘲讽;public int rcPaint_left;public int rcPaint_top;public int rcPaint_right;public int rcPaint_bottom;公共布尔fRestore;公共布尔fIncUpdate;public int reserved1;public int reserved2;public int reserved3;public int reserved4;public int reserved5;public int reserved6;public int reserved7;public int reserved8;}[DllImport("user32.dll"))私有静态外部IntPtr BeginPaint(IntPtr hWnd,[In,Out] ref PAINTSTRUCT lpPaint);[DllImport("user32.dll"))私有静态外部布尔EndPaint(IntPtr hWnd,ref PAINTSTRUCT lpPaint);[DllImport("gdi32.dll"))公共静态外部int SelectClipRgn(IntPtr hDC,IntPtr hRgn);[DllImport("user32.dll"))公共静态外部int GetUpdateRgn(IntPtr hwnd,IntPtr hrgn,bool fErase);公共枚举RegionFlags{错误= 0,NULLREGION = 1SIMPLEREGION = 2COMPLEXREGION = 3,}[DllImport("gdi32.dll"))内部静态外部布尔DeleteObject(IntPtr hObject);[DllImport("gdi32.dll"))私有静态外部IntPtr CreateRectRgn(int x1,int y1,int x2,int y2);} 

How could I change the slide button color? not border color and not slide item colors.

I already change the slide item colors

Is there any way to change the color?

解决方案

Flat ComboBox - Change border color and Dropdown button color

You need to handle WM_PAINT yourself and draw the border and the dropdown rectangle. This is the way that internal ComboBox.FlatComboAdapter class of .Net Framework works.

In this post, I've created a FlatComboBox, which draws the border and the dropdown in a flat style, having the following additional properties:

  • BorderColor: used for border and for the dropdown arrow
  • ButtonColor: used for dropdown area color.

Here is the code:

using System;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
public class FlatComboBox : ComboBox
{
    private Color borderColor = Color.Gray;
    [DefaultValue(typeof(Color), "Gray")]
    public Color BorderColor
    {
        get { return borderColor; }
        set
        {
            if (borderColor != value)
            {
                borderColor = value;
                Invalidate();
            }
        }
    }
    private Color buttonColor = Color.LightGray;
    [DefaultValue(typeof(Color), "LightGray")]
    public Color ButtonColor
    {
        get { return buttonColor; }
        set
        {
            if (buttonColor != value)
            {
                buttonColor = value;
                Invalidate();
            }
        }
    }
    protected override void WndProc(ref Message m)
    {
        if (m.Msg == WM_PAINT && DropDownStyle != ComboBoxStyle.Simple)
        {
            var clientRect = ClientRectangle;
            var dropDownButtonWidth = SystemInformation.HorizontalScrollBarArrowWidth;
            var outerBorder = new Rectangle(clientRect.Location,
                new Size(clientRect.Width - 1, clientRect.Height - 1));
            var innerBorder = new Rectangle(outerBorder.X + 1, outerBorder.Y + 1,
                outerBorder.Width - dropDownButtonWidth - 2, outerBorder.Height - 2);
            var innerInnerBorder = new Rectangle(innerBorder.X + 1, innerBorder.Y + 1,
                innerBorder.Width - 2, innerBorder.Height - 2);
            var dropDownRect = new Rectangle(innerBorder.Right + 1, innerBorder.Y,
                dropDownButtonWidth, innerBorder.Height + 1);
            if (RightToLeft == RightToLeft.Yes)
            {
                innerBorder.X = clientRect.Width - innerBorder.Right;
                innerInnerBorder.X = clientRect.Width - innerInnerBorder.Right;
                dropDownRect.X = clientRect.Width - dropDownRect.Right;
                dropDownRect.Width += 1;
            }
            var innerBorderColor = Enabled ? BackColor : SystemColors.Control;
            var outerBorderColor = Enabled ? BorderColor : SystemColors.ControlDark;
            var buttonColor = Enabled ? ButtonColor : SystemColors.Control;
            var middle = new Point(dropDownRect.Left + dropDownRect.Width / 2,
                dropDownRect.Top + dropDownRect.Height / 2);
            var arrow = new Point[]
            {
                new Point(middle.X - 3, middle.Y - 2),
                new Point(middle.X + 4, middle.Y - 2),
                new Point(middle.X, middle.Y + 2)
            };
            var ps = new PAINTSTRUCT();
            bool shoulEndPaint = false;
            IntPtr dc;
            if (m.WParam == IntPtr.Zero)
            {
                dc = BeginPaint(Handle, ref ps);
                m.WParam = dc;
                shoulEndPaint = true;
            }
            else
            {
                dc = m.WParam;
            }
            var rgn = CreateRectRgn(innerInnerBorder.Left, innerInnerBorder.Top, 
                innerInnerBorder.Right, innerInnerBorder.Bottom);
            SelectClipRgn(dc, rgn);
            DefWndProc(ref m);
            DeleteObject(rgn);
            rgn = CreateRectRgn(clientRect.Left, clientRect.Top, 
                clientRect.Right, clientRect.Bottom);
            SelectClipRgn(dc, rgn);
            using (var g = Graphics.FromHdc(dc))
            {
                using (var b = new SolidBrush(buttonColor))
                {
                    g.FillRectangle(b, dropDownRect);
                }
                using (var b = new SolidBrush(outerBorderColor))
                {
                    g.FillPolygon(b, arrow);
                }
                using (var p = new Pen(innerBorderColor))
                {
                    g.DrawRectangle(p, innerBorder);
                    g.DrawRectangle(p, innerInnerBorder);
                }
                                    using (var p = new Pen(outerBorderColor))
                {
                    g.DrawRectangle(p, outerBorder);
                }
            }
            if (shoulEndPaint)
                EndPaint(Handle, ref ps);
            DeleteObject(rgn);
        }
        else
            base.WndProc(ref m);
    }

    private const int WM_PAINT = 0xF;
    [StructLayout(LayoutKind.Sequential)]
    public struct RECT
    {
        public int L, T, R, B;
    }
    [StructLayout(LayoutKind.Sequential)]
    public struct PAINTSTRUCT
    {
        public IntPtr hdc;
        public bool fErase;
        public int rcPaint_left;
        public int rcPaint_top;
        public int rcPaint_right;
        public int rcPaint_bottom;
        public bool fRestore;
        public bool fIncUpdate;
        public int reserved1;
        public int reserved2;
        public int reserved3;
        public int reserved4;
        public int reserved5;
        public int reserved6;
        public int reserved7;
        public int reserved8;
    }
    [DllImport("user32.dll")]
    private static extern IntPtr BeginPaint(IntPtr hWnd,
        [In, Out] ref PAINTSTRUCT lpPaint);

    [DllImport("user32.dll")]
    private static extern bool EndPaint(IntPtr hWnd, ref PAINTSTRUCT lpPaint);

    [DllImport("gdi32.dll")]
    public static extern int SelectClipRgn(IntPtr hDC, IntPtr hRgn);

    [DllImport("user32.dll")]
    public static extern int GetUpdateRgn(IntPtr hwnd, IntPtr hrgn, bool fErase);
    public enum RegionFlags
    {
        ERROR = 0,
        NULLREGION = 1,
        SIMPLEREGION = 2,
        COMPLEXREGION = 3,
    }
    [DllImport("gdi32.dll")]
    internal static extern bool DeleteObject(IntPtr hObject);

    [DllImport("gdi32.dll")]
    private static extern IntPtr CreateRectRgn(int x1, int y1, int x2, int y2);
}

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

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