【问题标题】:Changing the background color of a DateTimePicker in .NET在 .NET 中更改 DateTimePicker 的背景颜色
【发布时间】:2010-09-16 22:42:01
【问题描述】:

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

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

【问题讨论】:

    标签: c# .net vb.net winforms


    【解决方案1】:

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

    例 1:

    例2:

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

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

    【讨论】:

    • 这工作,即使在 Windows 7 中也是如此。但由于 Combobox,它改变了 DatetimePicker 的外观。不错的解决方案。
    【解决方案2】:

    根据MSDN

    设置BackColorDateTimePicker的出现。

    您需要编写一个扩展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);
    }
    

    【讨论】:

    • 抱歉这么久才接受。我希望有其他不涉及知道十六进制值的解决方案,然后我忘记了这一点。
    • 感谢您的这篇文章。我正在自定义绘制一个 DateTimePicker 控件,这让我朝着正确的方向前进。
    • 我在 Vista 下尝试了这个更改(有和没有视觉样式),但它不起作用。
    • @Qua 设置背景颜色后是否尝试调用 myDTPicker.Invalidate()?
    • 还可以确认这在启用视觉样式的 Win 7 上不起作用。不过,它确实可以在没有视觉样式的情况下工作。为了测试,我创建了一个继承自 DateTimePicker 的简单类,并复制/粘贴了 Vivek 的代码。我用 Color.Blue 替换了 _backColor,但启用视觉样式后,框永远不会是蓝色的。
    【解决方案3】:

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

    查看 CodeProject 网站:DateTimePicker with working BackColor

    【讨论】:

    • 这是一个完全脑叶切除的版本,但您只能使用日历下拉菜单来实际选择日期。
    猜你喜欢
    • 2013-08-08
    • 1970-01-01
    • 2018-03-04
    • 2021-01-14
    • 1970-01-01
    • 2022-11-23
    相关资源
    最近更新 更多