问题描述
这是对以下文章的答复:(否则,我无法找到直接回复该文章的链接)
具有有效BackColor的DateTimePicker
文森佐·罗西(Vincenzo Rossi)
很抱歉想出一个没有限制的更简单的解决方案.我遇到了与本文相同的问题,并得出了相同的结论.但是我不喜欢这些限制,即不能再直接编辑控件中的天/月/年.
然后我在这里找到了一个很好的解决方案:
http:///stackoverflow.com/questions/198532/changing-the-background-color-of-a-datetimepicker-net
基本上,您创建一个从DateTimePicker继承的控件,并添加以下替代:
This is in answer to the following article: (I can''t otherwise find a link to simply respond to that article)
A DateTimePicker with working BackColor
By Vincenzo Rossi
Sorry to come up with a simpler solution that has no limitations. I hit the same problem and came to the same conclusions as in this post. However I didn''t like the limitations, namely that one couldn''t anymore edit directly the days/months/years in the control.
Then I found a great solution here:
http://stackoverflow.com/questions/198532/changing-the-background-color-of-a-datetimepicker-in-net
Basically you create a control inheriting from DateTimePicker and add this override:
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);
}
(其中_backColor是您的选择之一...)
如果要更改颜色,只需在更改_backColor之后调用DateTimePicker的Invalidate()方法.
对我来说,这很好,没有任何限制.
(Where _backColor is the one of your choice...)
If you want to change color, simply call the Invalidate() method of your DateTimePicker after changing _backColor.
To me that works wonderful, without any limitation.
推荐答案
No, it doesn't. I think the original article already explained that in order for it to work you must use SetStyle(UserPaint, true).
But then if you do that, you loose all the system implementation regarding the DateTimePicker... and it doesn't show much useful except your nice back color... It's one of these Microsoft not quite user friendly stuff... However we must thank them for the number of other good stuff they also provide us...)
这篇关于更改DateTimePicker的背景颜色的简单解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!