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
Changing the background color of a DateTimePicker in .NET
提问by Daniel H
In .NET
(at least in the 2008 version, and maybe in 2005 as well), changing the BackColor
property of a DateTimePicker
has 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
BackColor
has no effect on the appearance of theDateTimePicker
.
设置
BackColor
对 的外观没有影响DateTimePicker
。
You need to write a custom control that extends DateTimePicker
. Override the BackColor
property and the WndProc
method.
您需要编写一个扩展的自定义控件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 DateTimePicker
that allows you to change BackColor
property on change.
有一个从DateTimePicker
它派生的免费实现,允许您在更改时更改BackColor
属性。
See the CodeProject website: DateTimePicker
with 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:
Eg.2:
例2:
To make it work just create a new class in your project with the following code and Rebuild the Solution.
A new control called MyDateTimePicker
should 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
*注意这个类是简化的,所以它的功能有限