改变.NET一个DateTimePicker的背景颜色颜色、背景、NET、DateTimePicker

2023-09-03 00:29:36 作者:唐僧洗頭用飘柔

.NET (至少在2008年的版本,也许在2005年为好),改变了背景色的DateTimePicker财产绝对没有对外观的影响。如何更改不是下拉日历的文本区域的背景色,?

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?

编辑:的我在谈论Windows窗体,而不是ASP

I was talking about Windows forms, not ASP.

推荐答案

根据 MSDN

设置背景色有没有影响   外观的的DateTimePicker

Setting the BackColor has no effect on the appearance of the DateTimePicker.

您需要编写一个自定义的控制扩展的DateTimePicker 。重写背景色属性和的WndProc 方法。

You need to write a custom control that extends DateTimePicker. Override the BackColor property and the WndProc method.

当你修改背景色,不要忘记调用 myDTPicker.Invalidate()方法。这将迫使控制使用指定的新颜色重绘。

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.

const int WM_ERASEBKGND = 0x14;
protected override void WndProc(ref System.Windows.Forms.Message m)
{
     if(m.Msg == WM_ERASEBKGND)
     {
       Graphics g = Graphics.FromHdc(m.WParam);
       g.FillRectangle(new SolidBrush(_backColor), ClientRectangle);
       g.Dispose();
       return;
     }

     base.WndProc(ref m);
}