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

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

代码如下

//  *****************************************
//  ** Author: Vincenzo Rossi              **
//  ** Year  : 2008                        **
//  ** Mail  : [email protected]        **
//  **                                     **
//  ** 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
{
    
///  
    
///      A derivation of DateTimePicker allowing to change background color
    
///  

     class  BCDateTimePicker : System.Windows.Forms.DateTimePicker
    {
        
private  Color _backDisabledColor;

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

        
///  
        
///      Gets or sets the background color of the control
        
///  

        [Browsable( true )]
        
public   override  Color BackColor
        {
            
get  {  return   base .BackColor; }
            
set  {  base .BackColor  =  value; }
        }

        
///  
        
///      Gets or sets the background color of the control when disabled
        
///  

        [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  -   17 0 17 16 );
            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,  0 0 , ClientRectangle.Width, ClientRectangle.Height);
            
            
// Drawing the datetime text
            g.DrawString( this .Text,  this .Font, Brushes.Black,  0 2 );

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

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


转载于:https://www.cnblogs.com/chenjiang1014/archive/2011/01/11/1933115.html

你可能感兴趣的:(DateTimePicker背景色修改(默认是白色))