DateTimePicker背景色修改(默认是白色)

codeproject上下载的,感谢那位国际友.

代码如下

// *****************************************
// ** Author: Vincenzo Rossi              **
// ** Year  : 2008                        **
// ** Mail  : redmaster@tiscali.it        **
// **                                     **
// ** Released under                      **
// **   The Code Project Open License     **
// *****************************************

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms.VisualStyles;

namespace DateTimePickerWithBackColor
{
    
/// <summary>
    
///     A derivation of DateTimePicker allowing to change background color
    
/// </summary>
    class BCDateTimePicker : System.Windows.Forms.DateTimePicker
    {
        
private Color _backDisabledColor;

        
public BCDateTimePicker() : base()
        {
            
this.SetStyle(ControlStyles.UserPaint, true);
            _backDisabledColor 
= Color.FromKnownColor(KnownColor.Control);
        }

        
/// <summary>
        
///     Gets or sets the background color of the control
        
/// </summary>
        [Browsable(true)]
        
public override Color BackColor
        {
            
get { return base.BackColor; }
            
set { base.BackColor = value; }
        }

        
/// <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 Color BackDisabledColor
        {
            
get { return _backDisabledColor; }
            
set { _backDisabledColor = value; }
        }


        
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            Graphics g 
= this.CreateGraphics();
            
//Graphics g = e.Graphics;
            
            
//The dropDownRectangle defines position and size of dropdownbutton block, 
            
//the width is fixed to 17 and height to 16. The dropdownbutton is aligned to right
            Rectangle dropDownRectangle = new Rectangle(ClientRectangle.Width - 1701716);
            Brush bkgBrush;
            ComboBoxState visualState;

            
//When the control is enabled the brush is set to Backcolor, 
            
//otherwise to color stored in _backDisabledColor
            if (this.Enabled) {
                 bkgBrush 
= new SolidBrush(this.BackColor);
                 visualState 
= ComboBoxState.Normal;
            }
            
else {
                bkgBrush 
= new SolidBrush(this._backDisabledColor);
                visualState 
= ComboBoxState.Disabled;
            }

            
// Painting...in action

            
//Filling the background
            g.FillRectangle(bkgBrush, 00, ClientRectangle.Width, ClientRectangle.Height);
            
            
//Drawing the datetime text
            g.DrawString(this.Text, this.Font, Brushes.Black, 02);

            
//Drawing the dropdownbutton using ComboBoxRenderer
            ComboBoxRenderer.DrawDropDownButton(g, dropDownRectangle, visualState);

            g.Dispose();
            bkgBrush.Dispose();
        }
    }
}


posted @ 2011-01-11 17:54  jisuanji110  阅读(3919)  评论(0编辑  收藏  举报