C# DateTimePicker的自定义背景色

C# DateTimePicker的自定义背景色,c#,.net,visual-studio,winforms,C#,.net,Visual Studio,Winforms,我当时正在研究如何在c#中设置日期时间选择器的背景颜色 我创建了一个扩展DateTimePicker控件的自定义控件,但是在\u backColor上出现语法错误,表示名称“\u backColor”在当前上下文中不存在 我是否必须在自定义类中创建颜色变量,或者如何避免此错误 编辑: 我确信我在这里犯了一些非常明显的错误,但我无法理解。这是我目前的自定义类: class CustomDatePicker : DateTimePicker { const int WM_ERASEBKGND

我当时正在研究如何在c#中设置日期时间选择器的背景颜色

我创建了一个扩展DateTimePicker控件的自定义控件,但是在
\u backColor
上出现语法错误,表示
名称“\u backColor”在当前上下文中不存在

我是否必须在自定义类中创建颜色变量,或者如何避免此错误

编辑:

我确信我在这里犯了一些非常明显的错误,但我无法理解。这是我目前的自定义类:

class CustomDatePicker : DateTimePicker
{
    const int WM_ERASEBKGND = 0x14;
    public Color _backColor;

    protected override void WndProc(ref System.Windows.Forms.Message m)
    {
        if (m.Msg == WM_ERASEBKGND)
        {
            using (var g = Graphics.FromHdc(m.WParam))
            {
                using (var b = new SolidBrush(_backColor))
                {
                    g.FillRectangle(b, ClientRectangle);
                }
            }
            return;
        }

        base.WndProc(ref m);
    }

    public new Color BackColor
    {
        get
        {
            return base.BackColor;
        }
        set
        {
            _backColor = value;
        }
    }
}
这就是我改变颜色的方法,但它仍然不起作用

customDatePicker1.BackColor = Color.FromArgb(23, 21, 32);
customDatePicker1.Invalidate();

您可以尝试以下代码来更改Datetimepicker的背景色

自定义类:

public class MyDateTimePicker: DateTimePicker
{
    private Color _backDisabledColor;

    public MyDateTimePicker() : base()
    {
        this.SetStyle(ControlStyles.UserPaint, true);
        _backDisabledColor = Color.FromKnownColor(KnownColor.Control);
    }

    /// <summary>
    ///     Gets or sets the background color of the control
    /// </summary>
    [Browsable(true)]
    public override Color BackColor
    {
        get { return base.BackColor; }
        set { base.BackColor = value; }
    }

    /// <summary>
    ///     Gets or sets the background color of the control when disabled
    /// </summary>
    [Category("Appearance"), Description("The background color of the component when disabled")]
    [Browsable(true)]
    public Color BackDisabledColor
    {
        get { return _backDisabledColor; }
        set { _backDisabledColor = value; }
    }


    protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
    {
        Graphics g = this.CreateGraphics();
        //Graphics g = e.Graphics;

        //The dropDownRectangle defines position and size of dropdownbutton block, 
        //the width is fixed to 17 and height to 16. The dropdownbutton is aligned to right
        Rectangle dropDownRectangle = new Rectangle(ClientRectangle.Width - 17, 0, 17, 16);
        Brush bkgBrush;
        ComboBoxState visualState;

        //When the control is enabled the brush is set to Backcolor, 
        //otherwise to color stored in _backDisabledColor
        if (this.Enabled)
        {
            bkgBrush = new SolidBrush(this.BackColor);
            visualState = ComboBoxState.Normal;
        }
        else
        {
            bkgBrush = new SolidBrush(this._backDisabledColor);
            visualState = ComboBoxState.Disabled;
        }

        // Painting...in action

        //Filling the background
        g.FillRectangle(bkgBrush, 0, 0, ClientRectangle.Width, ClientRectangle.Height);

        //Drawing the datetime text
        g.DrawString(this.Text, this.Font, Brushes.Black, 0, 2);

        //Drawing the dropdownbutton using ComboBoxRenderer
        ComboBoxRenderer.DrawDropDownButton(g, dropDownRectangle, visualState);

        g.Dispose();
        bkgBrush.Dispose();
    }
}
结果:


\u backColor
只是一个独立的私有字段或公共属性的支持字段,用于在设计器中从PropertyGrid更改backColor属性。好的,那么,您知道如何解决此问题吗?什么问题?没有问题。如果要允许从PropertyGrid更改BackColor值,请覆盖公共
BackColor
属性。绘制背景时,请使用该公共属性的“背景”字段。该字段名为
\u backColor
,请随意命名。请查看编辑人员,我肯定有一天会遇到这种情况@HansPassant添加了一个选项,使控件为纯白色,没有文本
 private void Form1_Load(object sender, EventArgs e)
    {
        MyDateTimePicker picker = new MyDateTimePicker();
        picker.BackColor = Color.Yellow;
        this.Controls.Add(picker);
    }