Click here to Skip to main content
6,630,586 members and growing! (18,629 online)
Email Password   helpLost your password?
Desktop Development » Combo & List Boxes » General     Intermediate

Making Standard ComboBox appear flat

By Fadrian Sudaman

A simple and easy class that draws the standard ComboBox as flat control.
C#.NET 1.1, Win2K, WinXP, Visual Studio, Dev
Posted:6 May 2004
Updated:18 May 2005
Views:240,921
Bookmarked:65 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
39 votes for this article.
Popularity: 7.22 Rating: 4.54 out of 5
1 vote, 2.6%
1

2
2 votes, 5.1%
3
9 votes, 23.1%
4
27 votes, 69.2%
5

Sample Image - flatcombo.jpg

Introduction

Many flat combobox controls out there are not based on the standard ComboBox control that is supplied by .NET, instead they have their own interfaces and requires a customized type of ComboBoxItem when inserting into the Items container. If you already have code written based on the standard .NET ComboBox, changing to the flat look may also require you to modify your codes. I have personally experienced that as a problem, and someone else has brought that up as a problem as well in my recent article DateTimePicker appears Flat. Many of the information provided here may appear in the DateTimePicker appears Flat article already, or maybe explained better in there, so please check it out.

Using the Class

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

So instead of doing this:

ComboBox  cmb = new ComboBox();

You will do:

ComboBox cmb = new FlatComboBox();
// OR

FlatComboBox cmb = new FlatComboBox();

That is how simple it is :)

Code Explanation

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

IntPtr hDC = GetWindowDC(this.Handle);

Graphics gdc = Graphics.FromHdc(hDC);

switch (m.Msg)

{
    case WM_NC_PAINT: 
        SendMessage(this.Handle, WM_ERASEBKGND, hDC, 0);
        SendPrintClientMsg(); // send to draw client area

        PaintFlatControlBorder(this, gdc);
        m.Result = (IntPtr) 1; // indicate msg has been processed 

        break;
    case WM_PAINT: 
        base.WndProc(ref m);
        // flatten the border area again

        Pen p = new Pen((this.Enabled? BackColor:SystemColors.Control), 2); 
        gdc.DrawRectangle(p, new Rectangle(2, 2, this.Width-3, this.Height-3));
        PaintFlatDropDown(this, gdc);
        PaintFlatControlBorder(this, gdc);
        break;
    default:
        base.WndProc(ref m);
        break;

}

ReleaseDC(m.HWnd, hDC);

gdc.Dispose(); 

}

WM_NC_PAINT message is received when the control needs to paint its border. Here I trapped the message and send WM_PRINTCLIENT message so that the ComboBox will draw its client area properly, then followed by drawing a flat border around it.

WM_PAINT message is received when the control needs to paint portion of its windows. ComboBox internally embeds a textbox and will draw the textbox with 3D border. A quick way to achieve the flat look is to paint a rectangle overlaying the 3D border of the textbox, therefore it will appear flat. We then paint the flat dropdown and border over it. 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.

Limitation

Currently, this FlatComboBox does not draw with ComboBox.Simple style. When this style is set for the ComboBox object, this class will let the base class perform the standard drawing.

History

  • 18 May 2005
    • DROPDOWNWIDTH value is dynamic now based on the system setting hence will work well in different screen resolutions.
    • Only acquires and creates window DC when necessary.

License

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


Member
Fadrian Sudaman is currently working as a development team lead for a software company (www.ucube.net.au) specializing in developing enterprise system. He is also a part time computer science research student at Monash University, Australia. His development experiences span across low-level language interpeter to high-level enterprise workflow solutions.
Occupation: Web Developer
Location: Australia Australia

Other popular Combo & List Boxes articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 69 (Total in Forum: 69) (Refresh)FirstPrevNext
Generalthanks Pinmembergavin.r6:01 4 Feb '09  
GeneralIn VS2008/Vista PinmvpJohn Simmons / outlaw programmer9:12 25 Nov '08  
GeneralRe: In VS2008/Vista PinmemberFadrian Sudaman2:53 3 Dec '08  
QuestionA call to PInvoke function Error PinmemberIs-sanga1:06 12 Jun '08  
AnswerRe: A call to PInvoke function Error PinmemberFadrian Sudaman2:17 3 Dec '08  
GeneralGet the DropDawnList handle (graphics) PinmemberElmaho9:28 11 Apr '08  
GeneralEnable = false Pinmemberjorge.ramos9:54 21 Nov '07  
GeneralRe: Enable = false PinmemberFadrian Sudaman3:16 22 Nov '07  
GeneralRe: Enable = false Pinmemberjorge.ramos4:53 22 Nov '07  
GeneralNeed Direction PinmemberSE-Mrun19:59 20 Sep '07  
GeneralRe: Need Direction PinmemberFadrian Sudaman22:46 20 Sep '07  
GeneralRe: Need Direction PinmemberSE-Mrun22:55 20 Sep '07  
GeneralQuestion Pinmemberdavidbeseke12:42 16 Aug '07  
GeneralRe: Question PinmemberFadrian Sudaman23:24 18 Aug '07  
GeneralRe: Question Pinmemberdavidbeseke5:16 19 Aug '07  
Generalwhy win32? Pinmemberjuras910:57 24 Jul '07  
GeneralRe: why win32? PinmemberFadrian Sudaman14:36 24 Jul '07  
Generalgood job! Pinmemberapache1o13:24 5 Jun '07  
GeneralC3 PinmemberThimani20:47 3 Apr '07  
Generalhow to make standard TextBox appear flat Pinmembersuperkaier18:01 13 Apr '06  
GeneralRe: how to make standard TextBox appear flat PinmemberFadrian Sudaman17:37 17 Apr '06  
GeneralSupport for .net 2005 Pinmemberhuppy1011:59 4 Jan '06  
GeneralRe: Support for .net 2005 PinmemberFadrian Sudaman2:25 4 Jan '06  
GeneralRe: Support for .net 2005 Pinmemberhuppy1013:48 4 Jan '06  
GeneralRe: Support for .net 2005 PinmemberFadrian Sudaman22:02 4 Jan '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 18 May 2005
Editor: Smitha Vijayan
Copyright 2004 by Fadrian Sudaman
Everything else Copyright © CodeProject, 1999-2009
Web20 | Advertise on the Code Project