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

Image Button and Simulate Windows Media Player UI

By , 27 May 2008
 

Introduction

In my own shareware “LanTing Music Center”, I want the UI looks with more personality, so I decided to design some UI elements to build the application. Image button is the first and basic element. With image button, it is easy to build a very beautiful user interface.

In this sample, I use image button to build a form that looks like Microsoft’s Windows Media Player.

Background

In a skinned form, some buttons look irregular, but indeed it is also a rectangle button. Only because the image makes it look irregular, we can use image to represent a button. Normally a button has four states: disabled, normal, hover and pressed, so we can use four images to represent the four states of a button.

Sometimes we use PNG format image, and we know PNG supports transparent background. We will hope the button can support transparent, and then the background of the button will not be covered.

The Demo Solution

Andy.UI

This is a UI library, now it only contains one control-ImageButton. I will add more UI controls to this library - a WinForm application to show the usage of UI element. Now there is only the demo of image button, use image button to build a form that looks like Windows Media Player.

Using the Code

Add assembly Andy.UI to your project reference, or add the image button control to Visual Studio toolbox, and then you can use it.

Points of Interest

1. Double buffer

To improve performance, image button uses double buffer to prevent flicker. To do this, only use the below code in the constructor:

DoubleBuffered = true;

2. Transparent

To support transport only, you need the code given below. But I want to say that this is really transparent. When image button paints, it will ask its parent to draw its background, so it looks like it is transparent.

SetStyle ( ControlStyles.UserPaint, true ); 
SetStyle ( ControlStyles.SupportsTransparentBackColor, true ); 
BackColor = Color.Transparent;

3. Image Properties

We need four images to represent the button states; normally we need four variables which are of image type. It is a little bit of a bother, so I use a dictionary to store the images, button state is the key.

 /// <summary>
 /// images that state used
 /// </summary>
 private Dictionary<ButtonState,Image> mImages = new Dictionary<ButtonState, Image> ( ); 
        /// <summary>
        /// Get or set normal state image
        /// </summary>
        [DefaultValue ( null )]
        public Image NormalImage
        {
            get
            {
                if ( !mImages.ContainsKey ( ButtonState.Normal ) )
                    mImages.Add ( ButtonState.Normal, null );
                return mImages [ ButtonState.Normal ];
            }
            set
            {
                mImages [ ButtonState.Normal ] = value;
                Invalidate ( );
            }
        }

Note, if in this pattern it is recommended using property in ImageButton class. This is because the key can only be added to the dictionary when it gets the property. Or you can add the states key to the dictionary in the constructor.

4. Designer

The first time you add an ImageButton to the form, there are no state images, so there is no display on the ImageButton. It’s not convenient to judge where the button is when you design your form. So there is an ImageButtonDesigner, only draw a gray rectangle on the button in design time and no normal state image, thus you know where the button is.

History

  • 2008-5-28 – Created the article

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Andy Lang
Architect
China China
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralA couple of pointers...memberthomasswilliams28-May-08 12:55 
G'day Andy, this is an interesting looking control, I think your presentation could be improved in three ways:
* remove unneeded files in your download (e.g. obj folders, *.pdb files, vshost files)
* perhaps explain that you have used images for the buttons instead of generating them via code/GDI
* allow the demo window to resize, and demonstrate at least one drop-down on the tabs
 
Good luck with your media player, and keep up the good work!
 
Thomas
GeneralRe: A couple of pointers...memberAndy Lang28-May-08 16:01 
Thanks for your good advice. This is the first time that I publish such a article on CodeProject, I will learn how to explain my idear detail and more clear step by step.
 
Recent I am a little busy, I will update this in next days.
 
Thanks and good luck.
 
My music manage tool "Music master", http://www.longs-soft.com

GeneralRe: A couple of pointers...memberbytex9-Mar-09 3:32 
Great article ! How to change this button's type to RadioButton ?
GeneralRe: A couple of pointers...memberAndy Lang9-Mar-09 15:16 
thanks for your reply, this button only use 4 images to represent the 4 status of a button, I think it is also doable to use 4 images simulate a radio button.
 
My music manage tool "Music master", http://www.longs-soft.com

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130617.1 | Last Updated 28 May 2008
Article Copyright 2008 by Andy Lang
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid