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

时间:2008-10-13 18:28:29

标签: c# .net vb.net winforms

.NET中(至少在2008版本中,也可能在2005年),更改BackColor的{​​{1}}属性对外观完全没有影响。如何更改文本区域的背景颜色,而不是下拉日历的背景颜色?

编辑: 我说的是Windows表单,而不是ASP。

4 个答案:

答案 0 :(得分:15)

根据MSDN

  

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

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

每当您更改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);
}

答案 1 :(得分:4)

有一个源自DateTimePicker的免费实现,允许您在更改时更改BackColor属性。

请参阅CodeProject网站:DateTimePicker with working BackColor

答案 2 :(得分:2)

对于文本区域,您可以在将扩展器添加到实际文本框上的情况下执行此操作 对于c#,你会这样做:

<asp:TextBox runat="server" ID="txt" BackColor="Aqua" Text="Date"></asp:TextBox>

答案 3 :(得分:2)

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

Eg.1:

enter image description here

Eg.2:

enter image description here

要使其正常工作,只需使用以下代码在项目中创建一个新类,然后重新构建解决方案 现在,名为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

*请注意,此类已简化,因此功能有限