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

Making Standard ComboBox appear flat

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

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 Smile | :)

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

Share

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

 
Generalgood job and small suggestion PinmemberGiancarlo Aguilera17-Jan-05 5:37 
GeneralRe: good job and small suggestion PinmemberFadrian Sudaman17-Jan-05 12:16 
GeneralBug on 1900x1200 resolution PinmemberElizabeth Gee13-Dec-04 8:58 
GeneralRe: Bug on 1900x1200 resolution PinmemberFadrian Sudaman13-Dec-04 12:19 
GeneralRe: Bug on 1900x1200 resolution PinmemberElizabeth Gee13-Dec-04 12:33 
GeneralRe: Bug on 1900x1200 resolution PinmemberElizabeth Gee13-Dec-04 12:43 
GeneralRe: Bug on 1900x1200 resolution PinmemberFadrian Sudaman13-Dec-04 13:30 
GeneralRe: Bug on 1900x1200 resolution PinmemberElizabeth Gee14-Dec-04 6:18 
Here is a link to a screenshot of your Flat Combo on my Sony Vaio at 16oox1200 resolution:
http://www32.brinkster.com/icontoo/Temp/FlatComboOnSonyVaio.html
 
Also, here is your code with the two modifications made by others, which I compiled down to a C# DLL as depicted in the screenshot:
 
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Runtime.InteropServices;
 
namespace DrawFlat
{
[ToolboxBitmap(typeof(System.Windows.Forms.ComboBox))]
public class FlatComboBox: ComboBox
{
public const int WM_ERASEBKGND = 0x14;
public const int WM_PAINT = 0xF;
public const int WM_NC_PAINT = 0x85;
public const int WM_PRINTCLIENT = 0x318;
 
[DllImport("user32.dll", EntryPoint="SendMessageA")]
public static extern int SendMessage (IntPtr hwnd, int wMsg, IntPtr wParam, object lParam);
 
[DllImport("user32")]
public static extern IntPtr GetWindowDC (IntPtr hWnd );
 
[DllImport("user32")]
public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC );
 
public FlatComboBox()
: base()
{
this.SetStyle(ControlStyles.DoubleBuffer, true);
}
 
protected override void OnSelectedValueChanged(EventArgs e)
{
base.OnSelectedValueChanged (e);
this.Invalidate();
}
 
protected override void WndProc(ref Message m)
{
if (this.DropDownStyle == ComboBoxStyle.Simple)
{
base.WndProc(ref m);
return;
}
 
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();
}
private void SendPrintClientMsg()
{
// We send this message for the control to redraw the client area
Graphics gClient = this.CreateGraphics();
IntPtr ptrClientDC = gClient.GetHdc();
SendMessage(this.Handle, WM_PRINTCLIENT, ptrClientDC, 0);
gClient.ReleaseHdc(ptrClientDC);
gClient.Dispose();
}
 
private void PaintFlatControlBorder(Control ctrl, Graphics g)
{
Rectangle rect = new Rectangle(0, 0, ctrl.Width, ctrl.Height);
if (ctrl.Focused == false || ctrl.Enabled == false )
ControlPaint.DrawBorder(g, rect, SystemColors.ControlDark, ButtonBorderStyle.Solid);
else
ControlPaint.DrawBorder(g, rect, Color.Black, ButtonBorderStyle.Solid);
}
public static void PaintFlatDropDown(Control ctrl, Graphics g)
{
const int DROPDOWNWIDTH = 18;
int iWidth = (int) ((g.DpiX/96.0f) * DROPDOWNWIDTH);
//Rectangle rect = new Rectangle(ctrl.Width-DROPDOWNWIDTH, 0, DROPDOWNWIDTH, ctrl.Height);
Rectangle rect = new Rectangle(ctrl.Width-iWidth, 0, iWidth, ctrl.Height);
ControlPaint.DrawComboButton(g, rect, ButtonState.Flat);
}
 
protected override void OnLostFocus(System.EventArgs e)
{
base.OnLostFocus(e);
this.Invalidate();
}
 
protected override void OnGotFocus(System.EventArgs e)
{
base.OnGotFocus(e);
this.Invalidate();
}

protected override void OnResize(EventArgs e)
{
base.OnResize(e);
this.Invalidate();
}
}
}
 
Elizabeth Gee
Information Architect - .Net Developer
http://www.nwtd.com
GeneralRe: Bug on 1900x1200 resolution PinmemberFadrian Sudaman14-Dec-04 12:22 
GeneralRe: Bug on 1900x1200 resolution PinmemberElizabeth Gee14-Dec-04 13:26 
GeneralRe: Bug on 1900x1200 resolution PinmemberFadrian Sudaman14-Dec-04 18:30 
GeneralRe: Bug on 1900x1200 resolution PinmemberElizabeth Gee15-Dec-04 12:33 
GeneralRe: Bug on 1900x1200 resolution PinsussJuan Pedro Gonzalez20-Jan-05 17:22 
GeneralRe: Bug on 1900x1200 resolution PinmemberJuan Pedro Gonzalez21-Jan-05 0:04 
GeneralRe: Bug on 1900x1200 resolution PinmemberFadrian Sudaman18-May-05 18:51 
GeneralCool Combox PinmemberSawjai29-Sep-04 18:49 
Generalconst int DROPDOWNWIDTH = 18; Pinmember_DD_MORPH_4-Aug-04 23:13 
GeneralRe: const int DROPDOWNWIDTH = 18; Pinmember_DD_MORPH_5-Aug-04 0:07 
GeneralRe: const int DROPDOWNWIDTH = 18; PinmemberFadrian Sudaman5-Aug-04 14:50 
QuestionHi Fadrian, can I use flat combo in VB.Net? PinmemberKingVikram24-May-04 21:41 
AnswerRe: Hi Fadrian, can I use flat combo in VB.Net? PinmemberFadrian Sudaman24-May-04 21:59 
GeneralRe: Hi Fadrian, can I use flat combo in VB.Net? PinmemberDuc_de_Belfort28-Jun-04 1:34 
GeneralRe: Hi Fadrian, can I use flat combo in VB.Net? PinmemberFadrian Sudaman1-Jul-04 17:19 
GeneralRe: Hi Fadrian, can I use flat combo in VB.Net? PinmemberDuc_de_Belfort2-Jul-04 5:43 
GeneralRe: Hi Fadrian, can I use flat combo in VB.Net? PinmemberDuc_de_Belfort2-Jul-04 5:45 

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
Web03 | 2.8.150129.1 | Last Updated 18 May 2005
Article Copyright 2004 by Fadrian Sudaman
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid