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

Images on XP-Style Buttons

, 27 Oct 2004
Rate this:
Please Sign up or sign in to vote.
A .NET Windows Forms Button using Visual Styles on Windows XP, that can display an image.


One thing I love best about XP is the capability to have themed controls. You can even load new themes much like it's been possible with most window managers for the X Window System.

These themed controls can be used with .NET Windows Forms by supplying a manifest and – for some controls like Button – setting the FlatStyle property to FlatStyle.System.

Unfortunately, setting the FlatStyle property of a Button control to FlatStyle.System makes it impossible to display an image on the face of the button. Somehow, setting the Image property has no effect on these controls.


A number of workarounds have been proposed, e.g., this one by MalteseFalcon. All approaches known to me use owner-drawn buttons to mimic the built in visual styles of XP but provide no ability to match third party visual styles.

The code snippet in this article makes it possible to have images on your XP-themed buttons while retaining the capability to adapt to custom visual styles.

The capability to draw controls using different visual styles was introduced by Microsoft with comctl32.dll, version 6, which has been shipping with the newest versions of Windows, i.e., Windows XP and Windows Server 2003. With this version, a number of new API calls have been introduced, among them is BCM_SETIMAGELIST which assigns an image list to a button control. The code in this article uses Platform Invocation Services (PInvoke) to use the BCM_SETIMAGELIST message.

The BCM_SETIMAGELIST message has another interesting property: if you supply more than one image in the image list, you can have the button display a different image for each of a number of states the button is in:

  • Normal
  • Hover
  • Pressed
  • Disabled
  • Focused

This behavior might be achieved using events as well, but using BCM_SETIMAGELIST, you get it without having to code the event handlers.

Using the code

The code wraps the System.Windows.Forms.Button class in a class called ImageButton. You can use this class just like a normal button except for the additional overloaded method SetImage. If you just want to display an image on the face of the button, you can use the method like this:

Bitmap searchBitmap = new Bitmap("Search_16X16_32bpp.png");
ImageButton searchImageButton = new ImageButton();

If you want more control over the alignment, padding, or set images for different button states, use the overloaded methods:

Bitmap goBitmap = new Bitmap("Go_48X48_32bpp.png");
Bitmap goHoverBitmap = new Bitmap("Go!_48X48_32bpp.png");
Bitmap goDisabledBitmap = new Bitmap("GoDisabled_48X48_32bpp.png");
ImageButton goImageButton = new ImageButton();
goImageButton.SetImage(goBitmap, goHoverBitmap, goBitmap, goDisabledBitmap, 
                       goBitmap, ImageButton.Alignment.Center);

Images with alpha channel are supported.

Points of Interest

Windows Forms ImageLists have a bug: if you add an Image to an ImageList, the alpha channel is lost. Therefore, the class in this article uses a workaround by resetting the pixel values of the Image after it has been added to the ImageList.

The demo image shows some buttons with the default XP visual style (top) as well as the custom Chaninja RC5 visual style (bottom).

The images in the demo are from the Qute collection originally drawn for Mozilla Firebird by Arvid "Quadrone" Axelsson.


  • February 24, 2004: Version 1.1.
    • Added alignment for non-XP-style buttons.
    • Added speed-up to Bitmap copying, contributed by Richard Deeming.
  • February 26, 2004: Version 1.2.
    • Added image scaling suggested by nxtwothou.
  • February 28, 2004: Version 1.3.
    • Added bicubic interpolation for rescaling.
  • May 8, 2004: Version 1.4.
    • Added drop down arrow capability
  • May 14, 2004: Version 1.5.
    • Added ThemedImage property for the designer.
    • Added auto-generation of disabled image (thanks to Carlos Leyva).
  • October 25, 2004: Version 1.6.
    • Fixed themes on/off detection bug (thanks to Leonid Kunin).
    • Added NAnt file.
    • Added I18n Smile | :)


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

Michael Ganss
Software Developer (Senior) UpdateStar
Germany Germany
Michael Ganss is Managing Director of UpdateStar. UpdateStar offers complete protection from PC vulnerability caused by outdated software. The award-winning UpdateStar offers comfortable software installation, uninstallation, and keeps all of your programs up-to-date. UpdateStar recognizes more than 135,000 software products and lets you know once an update is available for you - for optimized PC security.

Comments and Discussions

GeneralBroke out of the box! PinmemberAl_S1-Sep-09 4:34 
GeneralEasier Method PinmemberEvit-Morningstar5-Jul-07 6:42 
NewsCode is obsolete for .NET 2.0... Pinmemberxlouk7-Jul-06 5:13 
GeneralRe: Code is obsolete for .NET 2.0... Pinmemberearnshaw19433-Apr-07 0:54 
GeneralRe: Code is obsolete for .NET 2.0... Pinmemberxlouk3-Apr-07 2:24 
GeneralRe: Code is obsolete for .NET 2.0... PinmemberJagdPanther14-Aug-07 6:37 
GeneralRe: Code is obsolete for .NET 2.0... [modified] PinmemberDotNetInterest13-Aug-07 8:15 
GeneralRe: Code is obsolete for .NET 2.0... Pinmemberxlouk19-Aug-07 23:32 
Generalbugs in DropdownButton PinmemberPunCha21-Apr-06 4:28 
GeneralI could not get it work with VS 2003 pro Pinmemberriscy25-Feb-06 6:49 
GeneralRe: I could not get it work with VS 2003 pro PinmemberMichael Ganss27-Feb-06 0:38 
Question"pushed" button style for a dropdown button ? Pinmemberjoebarthib6-Dec-05 5:58 
AnswerRe: "pushed" button style for a dropdown button ? PinmemberMichael Ganss6-Dec-05 23:08 
GeneralRe: pushed button style for a dropdown button ? Pinmemberjoebarthib8-Dec-05 0:20 
GeneralImageCheckBox Pinmemberwolfgang_hg28-Nov-05 5:31 
QuestionNo XP Style PinmemberGreg Osborne26-Oct-05 6:57 
AnswerRe: No XP Style PinmemberMichael Ganss26-Oct-05 22:55 
QuestionProblem after using ImageButton PinmemberRagulKumar29-Aug-05 18:28 
AnswerRe: Problem after using ImageButton PinmemberMichael Ganss29-Aug-05 23:00 
QuestionRe: Problem after using ImageButton PinsussAnonymous31-Aug-05 20:05 
AnswerRe: Problem after using ImageButton PinmemberMichael Ganss31-Aug-05 23:15 
AnswerRe: Problem after using ImageButton PinmemberGreg Osborne26-Oct-05 7:01 
GeneralGDI-Exceptions due to unsafe code ?! Pinmemberwolfgang_hg25-Feb-05 6:51 
GeneralRe: GDI-Exceptions due to unsafe code ?! PinmemberMichael Ganss25-Feb-05 7:02 
GeneralRe: GDI-Exceptions due to unsafe code ?! PinmemberJoel Lucsy3-Mar-05 10:14 
GeneralException: Invalid parament used Pinmemberrj040211zyw@hotmail.com11-Jan-05 16:12 
GeneralRe: Exception: Invalid parament used PinmemberMichael Ganss11-Jan-05 23:27 
GeneralRe: Exception: Invalid parament used Pinmemberrj040211zyw@hotmail.com12-Jan-05 22:36 
GeneralRe: Exception: Invalid parament used Pinmemberrj040211zyw12-Jan-05 22:36 
GeneralAlpha rendering bug PinsussJ-F Menard22-Nov-04 4:14 
GeneralRe: Alpha rendering bug (Solution 1) PinsussJ-F Menard25-Nov-04 4:13 
GeneralRe: Alpha rendering bug (Solution 1) PinmemberMichael Ganss25-Nov-04 7:22 
GeneralRe: Alpha rendering bug (Solution 1) Pinmemberneal andrews13-Dec-04 1:58 
GeneralRe: Alpha rendering bug (Solution 1) PinmemberMichael Ganss13-Dec-04 7:43 
GeneralRe: Alpha rendering bug (Solution 1) Pinmemberflaushi24-May-07 13:24 
GeneralRe: Alpha rendering bug PinmemberMartiankeeper16-Nov-05 2:28 
GeneralC# Controls PinmemberKSBmail16-Nov-04 18:50 
GeneralC# with VC6 PinmemberAlex Evans27-Oct-04 18:26 
Questionproblem with run this project? Pinmemberraha8023-Oct-04 1:45 
AnswerRe: problem with run this project? PinmemberMichael Ganss25-Oct-04 5:21 
GeneralProblem with MDI form PinmemberAlive99920-Oct-04 6:03 
GeneralRe: Problem with MDI form PinmemberAlive99920-Oct-04 7:54 
GeneralRe: Problem with MDI form Pinmemberphanf21-Oct-04 5:36 
GeneralRe: Problem with MDI form PinmemberAlive99922-Oct-04 2:23 
GeneralRe: Problem with MDI form PinmemberAlive99922-Oct-04 2:28 
GeneralSmall problem with disabled buttons Pinmemberkuninl2-Oct-04 14:27 
GeneralRe: Small problem with disabled buttons PinmemberMichael Ganss25-Oct-04 5:12 
GeneralSome Suggestions PinmemberYan Khai Ng5-May-04 15:43 
GeneralRe: Some Suggestions PinmemberMichael Ganss8-May-04 5:36 
GeneralRe: Some Suggestions PinmemberYan Khai Ng13-May-04 17:17 

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
Web01 | 2.8.150305.1 | Last Updated 27 Oct 2004
Article Copyright 2004 by Michael Ganss
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid