C# 在 .NET 中更改 DateTimePicker 的背景颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/198532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 17:41:06  来源:igfitidea点击:

Changing the background color of a DateTimePicker in .NET

c#.netvb.netwinforms

提问by Daniel H

In .NET(at least in the 2008 version, and maybe in 2005 as well), changing the BackColorproperty of a DateTimePickerhas absolutely no affect on the appearance. How do I change the background color of the text area, not of the drop-down calendar?

.NET(至少在 2008 版本中,也许在 2005 中),更改 a 的BackColor属性DateTimePicker对外观绝对没有影响。如何更改文本区域的背景颜色,而不是下拉日历的背景颜色?

Edit:I was talking about Windows forms, not ASP.

编辑:我说的是 Windows 窗体,而不是 ASP。

采纳答案by Vivek

According to MSDN:

根据MSDN

Setting the BackColorhas no effect on the appearance of the DateTimePicker.

设置BackColor对 的外观没有影响DateTimePicker

You need to write a custom control that extends DateTimePicker. Override the BackColorproperty and the WndProcmethod.

您需要编写一个扩展的自定义控件DateTimePicker。覆盖BackColor属性和WndProc方法。

Whenever you change the BackColor, don't forget to call the myDTPicker.Invalidate()method. This will force the control to redrawn using the new color specified.

无论何时更改BackColor,都不要忘记调用该myDTPicker.Invalidate()方法。这将强制使用指定的新颜色重新绘制控件。

const int WM_ERASEBKGND = 0x14;
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);
}

回答by Gustavo

There is a free implementation derived from DateTimePickerthat allows you to change BackColorproperty on change.

有一个从DateTimePicker它派生的免费实现,允许您在更改时更改BackColor属性。

See the CodeProject website: DateTimePickerwith working BackColor

请参阅 CodeProject 网站:DateTimePicker与工作BackColor

回答by Carlos Borau

Based on this CodeProject: A DateTimePicker with working BackColor(as posted above) I've rewritten a custom datepicker class (in VB.NET) which allows customizing the Background color, the TextColor and the small Image appearing next to the dropdown button.

基于此 CodeProject: A DateTimePicker with Working BackColor(如上所示),我重写了一个自定义 datepicker 类(在 VB.NET 中),它允许自定义背景颜色、TextColor 和出现在下拉按钮旁边的小图像。

Eg.1:

例如1:

enter image description here

在此处输入图片说明

Eg.2:

例2:

enter image description here

在此处输入图片说明

To make it work just create a new class in your project with the following code and Rebuild the Solution.
A new control called MyDateTimePickershould now appear in the toolbox list:

要使其工作,只需使用以下代码在您的项目中创建一个新类并重新构建解决方案。
一个名为的新控件MyDateTimePicker现在应该出现在工具箱列表中:

Public Class MyDateTimePicker 
    Inherits System.Windows.Forms.DateTimePicker
    Private _disabled_back_color As Color
    Private _image As Image
    Private _text_color As Color = Color.Black

    Public Sub New()
        MyBase.New()
        Me.SetStyle(ControlStyles.UserPaint, True)
        _disabled_back_color = Color.FromKnownColor(KnownColor.Control)
    End Sub

    ''' <summary>
    '''     Gets or sets the background color of the control
    ''' </summary>
    <Browsable(True)>
    Public Overrides Property BackColor() As Color
        Get
            Return MyBase.BackColor
        End Get
        Set
            MyBase.BackColor = Value
        End Set
    End Property

    ''' <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 Property BackDisabledColor() As Color
        Get
            Return _disabled_back_color
        End Get
        Set
            _disabled_back_color = Value
        End Set
    End Property

    ''' <summary>
    '''     Gets or sets the Image next to the dropdownbutton
    ''' </summary>
    <Category("Appearance"),
    Description("Get or Set the small Image next to the dropdownbutton")>
    Public Property Image() As Image
        Get
            Return _image
        End Get
        Set(ByVal Value As Image)
            _image = Value
            Invalidate()
        End Set
    End Property

    ''' <summary>
    '''     Gets or sets the text color when calendar is not visible
    ''' </summary>
    <Category("Appearance")>
    Public Property TextColor As Color
        Get
            Return _text_color
        End Get
        Set(value As Color)
            _text_color = value
        End Set
    End Property


    Protected Overrides Sub OnPaint(e As System.Windows.Forms.PaintEventArgs)
        Dim g As Graphics = Me.CreateGraphics()
        g.TextRenderingHint = Drawing.Text.TextRenderingHint.ClearTypeGridFit

        'Dropdownbutton rectangle
        Dim ddb_rect As New Rectangle(ClientRectangle.Width - 17, 0, 17, ClientRectangle.Height)
        'Background brush
        Dim bb As Brush

        Dim visual_state As ComboBoxState

        'When enabled the brush is set to Backcolor, 
        'otherwise to color stored in _disabled_back_Color
        If Me.Enabled Then
            bb = New SolidBrush(Me.BackColor)
            visual_state = ComboBoxState.Normal
        Else
            bb = New SolidBrush(Me._disabled_back_color)
            visual_state = ComboBoxState.Disabled
        End If

        'Filling the background
        g.FillRectangle(bb, 0, 0, ClientRectangle.Width, ClientRectangle.Height)

        'Drawing the datetime text
        g.DrawString(Me.Text, Me.Font, New SolidBrush(TextColor), 5, 2)

        'Drawing icon
        If Not _image Is Nothing Then
            Dim im_rect As New Rectangle(ClientRectangle.Width - 40, 4, ClientRectangle.Height - 8, ClientRectangle.Height - 8)
            g.DrawImage(_image, im_rect)
        End If

        'Drawing the dropdownbutton using ComboBoxRenderer
        ComboBoxRenderer.DrawDropDownButton(g, ddb_rect, visual_state)

        g.Dispose()
        bb.Dispose()
    End Sub
End Class

*Note that this class is simplified, so it has limited functionallity

*注意这个类是简化的,所以它的功能有限