Click here to Skip to main content
Click here to Skip to main content

DateTimePicker appears flat

, 17 May 2005
Rate this:
Please Sign up or sign in to vote.
A simple and easy class that draws the standard DateTimePicker as a flat control.

Sample Image - FlatDateTimePicker.jpg


All the while, we have been using Lumisoft flat controls for our project. However the date picker control supplied does not have support for the time element. I decided to use the standard .NET DateTimePicker control, but the 3D appearance of the control looked really awful in the middle of all the flat controls. After much searching and reading, I couldn't find a readily usable class/control for the purpose that I wanted, and some suggested codes were rather complicated. Based on these information and suggestions by other authors, I came out with this class which is simple and yet functional. It does exactly what I want in a very simple way as well, by using the ControlPaint class supplied in the .NET framework.

Using the Class

This class inherits from DateTimePicker, therefore you can use it in exactly the same way as the DateTimePicker control or even as a replacement.

So instead of doing this:

DateTimePicker dtp = new DateTimePicker();

You will do:

DateTimePicker dtp = new FlatDateTimePicker();

// OR

FlatDateTimerPicker dtp = new FlatDateTimePicker();

That is how simple it is Smile | :)

Code Explanation

To achieve the flat look for the control, I had to override the WndProc method, which is the method that processes all the window messages for this control. We are particularly interested with WM_NC_PAINT, WM_PAINT and WM_NCHITTEST messages.

Note that in this method, we need to acquire the actual window DC for the control using WinAPI GetWindowDC and release it later by using ReleaseDC. One main reason of using this over simply creating the Graphics object is because we need the entire DC of the control (bound rectangle) not just the client rectangle, therefore we can paint the border.

  protected override void WndProc(ref Message m)
   IntPtr hDC = GetWindowDC(m.HWnd);
   Graphics gdc = Graphics.FromHdc(hDC);
   switch (m.Msg)
    case WM_NC_PAINT:
     SendMessage(this.Handle, WM_ERASEBKGND, hDC, 0);
     SendMessage(this.Handle, WM_PAINT, IntPtr.Zero, 0);

     m.Result = (IntPtr)1; // indicate msg has been processed
    case WM_PAINT: base.WndProc(ref m);
      case  WM_NC_HITTEST:
     base.WndProc(ref m);
     if (DroppedDown)
      this.Invalidate(this.ClientRectangle, false);
     base.WndProc(ref m);
   ReleaseDC(m.HWnd, hDC);

WM_NC_PAINT message is received when the control needs to paint its border. Here I trapped the message and do the border painting myself without calling the base class method, because the base class will try to draw this with a 3D border. I also clear the area prior to the border drawing otherwise it may cause some potential redraw problem when a control/dialog overlays on top such as when a combo box pops up.

WM_PAINT message is received when the control needs to paint portions of its windows. Here we just pass the message to the base class and let the control take care of the drawing (including the display value), but after, we will override the border and the drop down button to ensure that they are flat. Overriding the border is optional here, but I did it for the user experience where if the control is in focus, it will have a black line border or otherwise none.

WM_NCHITTEST message is received when you move your mouse around within the control. Many have attempted to write flat controls but were unable to draw the pulldown arrow in flat when the popup appears, because of not trapping this message properly. Every time the pulldown is clicked, the CloseUp or DropDown is called and followed by a redraw which will paint the 3D arrow onto the control; therefore to achieve the flat look, we have to redraw it here. Simply calling Invalidate in this case block will cause flickering, so we will only want to redraw it when the Calendar control actually pops up.

Change Logs

  • 13 April 2004
    • Posted.
  • 22 April 2004
    • Fixed the missing text problem reported.
  • 18 May 2005
    • Fixed flickering problem.
    • DROPDOWNWIDTH value is dynamic now based on the system setting, hence will work well in different screen resolutions.
    • Fixed drop down button redraw problem when mouse is not moved.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Fadrian Sudaman
Architect SMS Management and Technology
Australia Australia
Fadrian Sudaman is an experienced IT professional who has worked with .NET technology since the early beta. His background stems from a strong C/C++ development experience in building large commercial applications and great appreciation for best practice and modern approaches for building quality software. Currently, Fadrian works as a senior consultant specialises in .NET technology involved in variety of roles including project management, solution architecture, presales and application development. Fadrian is also completing his PhD part time at Monash University, Australia.
Follow on   Twitter   Google+

Comments and Discussions

Questionflat drop-down arrow button? PinmemberKaine2-Aug-10 1:46 
GeneralDropdown Icon PinmemberDewayne Dodd6-Apr-09 16:02 
Generalfine control with one question PinmemberthirstyDev2-Dec-08 20:56 
I must say this is fine control.
I want to support showupdown property, so up-down box should be flat control.
The code that I change is:
private void OverrideDropDown(Graphics g)
            if (!this.ShowUpDown)
                Rectangle rect = new Rectangle(this.Width - DropDownButtonWidth, 0, DropDownButtonWidth, this.Height);
                ControlPaint.DrawComboButton(g, rect, ButtonState.Flat);
                Rectangle rect = new Rectangle(this.Width - DropDownButtonWidth,
                    0, DropDownButtonWidth, this.Height/2);
                ControlPaint.DrawScrollButton(g, rect, ScrollButton.Up, ButtonState.Flat);
                rect.Y += this.Height / 2;
                ControlPaint.DrawScrollButton(g, rect, ScrollButton.Down, ButtonState.Flat);
But it does not display, flat up-down button continuously. I want to understand the reason for same.
Whether this is solvable or not, that is secondary thing.
GeneralRe: fine control with one question PinmemberFadrian Sudaman3-Dec-08 3:10 
GeneralGood work!! Just a small correction on PInvoke declaration PinmemberCaio Proiete11-Jul-08 6:27 
GeneralRed Rectangle Pinmemberdiazpablomiguel23-Apr-08 6:22 version PinmemberMSP129-May-07 23:20 
GeneralRe: version PinmemberMSP129-May-07 23:35 
GeneralRe: version PinmemberS. Töpfer22-Feb-10 2:20 
GeneralDateTimePicker with Popup FlatStyle support Pinmembervachaun4-May-07 11:30 
GeneralRe: DateTimePicker with Popup FlatStyle support PinmemberFadrian Sudaman5-May-07 16:00 
GeneralRe: DateTimePicker with Popup FlatStyle support [modified] Pinmembervachaun6-May-07 11:33 
GeneralRe: DateTimePicker with Popup FlatStyle support PinmemberFadrian Sudaman9-May-07 3:57 
GeneralRe: DateTimePicker with Popup FlatStyle support Pinmembervachaun10-May-07 6:14 
GeneralRe: DateTimePicker with Popup FlatStyle support PinmemberFadrian Sudaman10-May-07 14:22 
GeneralRe: DateTimePicker with Popup FlatStyle support Pinmembervachaun11-May-07 3:25 version Pinmemberrgf2119-Mar-07 2:55 
AnswerRe: version PinmemberFadrian Sudaman9-May-07 4:01 
GeneralRe: version PinmemberMSP129-May-07 23:40 
QuestionPInvokeStackInbalanceException raised PinmemberJacek Osiecki16-Jan-07 3:12 
AnswerRe: PInvokeStackInbalanceException raised PinmemberFadrian Sudaman16-Jan-07 14:00 
GeneralRe: PInvokeStackInbalanceException raised PinmemberJacek Osiecki17-Jan-07 0:38 
GeneralTransparencyKey problem PinmemberAlexDias3-Oct-06 9:12 
GeneralRe: TransparencyKey problem PinmemberFadrian Sudaman4-Oct-06 4:25 
GeneralTime select didn not appear Pinmemberjcui@activplant.com13-Jun-05 9:12 
GeneralRe: Time select didn not appear PinmemberFadrian Sudaman15-Jun-05 1:30 
GeneralNice but it's not all flat Pinmemberzxcvasda9-Jun-05 19:59 
GeneralRe: Nice but it's not all flat PinmemberFadrian Sudaman9-Jun-05 20:25 
GeneralNice control, two questions though. PinmemberJazzJackRabbit13-May-05 4:31 
GeneralRe: Nice control, two questions though. PinmemberFadrian Sudaman14-May-05 18:28 
GeneralButton still flickers PinmemberDuc de Belfort9-Mar-05 3:55 
GeneralRe: Button still flickers PinmemberDuc de Belfort9-Mar-05 4:52 
GeneralRe: Button still flickers PinmemberFadrian Sudaman10-Mar-05 13:31 
GeneralRe: Button still flickers PinmemberJazzJackRabbit13-May-05 5:41 
GeneralRe: Button still flickers PinmemberFadrian Sudaman16-May-05 7:04 
GeneralRe: Button still flickers PinmemberJazzJackRabbit16-May-05 17:55 
GeneralRe: Button still flickers PinmemberFadrian Sudaman17-May-05 4:38 
GeneralAdd highlight color property Pinmembernguyenquanghung7-Nov-04 17:37 
GeneralRe: Add highlight color property PinsussFadrian Sudaman9-Nov-04 19:42 
QuestionUndesidered Pixel? PinmemberJordi Corominas4-Nov-04 23:33 
AnswerRe: Undesidered Pixel? PinsussFadrian Sudaman5-Nov-04 2:39 
GeneralRe: Undesidered Pixel? PinmemberJordi Corominas5-Nov-04 3:17 
GeneralRe: Undesidered Pixel? PinmemberFadrian Sudaman7-Nov-04 12:58 
GeneralRe: Undesidered Pixel? PinsussFadrian Sudaman9-Nov-04 19:34 
GeneralRe: Undesidered Pixel? PinmemberJazzJackRabbit16-May-05 5:10 
GeneralDrop-down bug Pinmembersmako7-Aug-04 10:41 
GeneralNeed sample of how to use Pinmemberrarmstrong222230-Jun-04 12:31 
GeneralRe: Need sample of how to use PinmemberFadrian Sudaman1-Jul-04 17:26 
GeneralSome comments... PinmemberVictor Boctor18-May-04 20:57 
GeneralRe: Some comments... PinmemberFadrian Sudaman24-May-04 21:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150327.1 | Last Updated 18 May 2005
Article Copyright 2004 by Fadrian Sudaman
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid