Click here to Skip to main content
15,867,453 members
Articles / Programming Languages / C#

Owner-draw Button

Rate me:
Please Sign up or sign in to vote.
2.71/5 (28 votes)
3 Feb 20031 min read 138.4K   847   43   6
A button-derived class that makes push buttons look like toolbar buttons

How the button looks on mouse leave event

How the button looks on mouse enter event

Introduction

I have always hated the so called "focused rectangle" Windows draws on push buttons having the focus. So I decided to derive my own class that does not display such a rectangle. Later, I put an image support for the button and that's how it began.

Using the Code

In the download is the whole solution, so you have just to unzip it and start Controls.sln and see all the files of the project (I suppose you have Visual Studio.NET). Then you may create your own push buttons from the "But" class. I supplied three different constructors for it. The first one uses a bitmap object to display on the button:

C#
public But(string str, Bitmap bitmap, int cx, int cy)
{
    ResizeRedraw = true;            
    txt = str;//text caption of the button
    bmp = bitmap;//bitmap image to display
    bmp.MakeTransparent();
            
    MeasureSizes(cx, cy);//cx and cy are the desired width and height for the button
}

The next is working with icon:

C#
public But(string str, System.Drawing.Icon ico, int cx, int cy, int IconWidth, int IconHeight)
{
    ResizeRedraw = true;            
    txt = str;
    ico = new Icon(ico, IconWidth, IconHeight);
    bmp = ico.ToBitmap();
    bmp.MakeTransparent();

    MeasureSizes(cx, cy);
}

And the last one (I use mostly) is without specifying cx and cy (in MeasureSizes function, I supply the minimum size required for proper displaying of the button).

C#
public But(string str, System.Drawing.Icon ico, int IconWidth, int IconHeight)
{
    ResizeRedraw = true;            
    txt = str;
    ico = new Icon(ico, IconWidth, IconHeight);
    bmp = ico.ToBitmap();
    bmp.MakeTransparent();

    MeasureSizes(Width, Height);

Well, that's it! I hope my owner-drawn button will be useful. :)

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.


Written By
Team Leader Telerik Corp.
Bulgaria Bulgaria
.NET & C# addicted. Win8 & WinRT enthusiast and researcher @Telerik.

Comments and Discussions

 
GeneralMy vote of 1 Pin
12Monkeys20-Apr-09 22:19
12Monkeys20-Apr-09 22:19 
GeneralMy vote of 1 Pin
ChewsHumans8-Dec-08 12:34
ChewsHumans8-Dec-08 12:34 
GeneralShorter Method Pin
praetorianCP19-Oct-05 18:04
praetorianCP19-Oct-05 18:04 
If you hate focus rectangles, disable them,

protected override bool ShowFocusCues
{
get
{
return false;
}
}


GeneralCould be a lot better Pin
Quentin Pouplard14-Mar-03 6:28
Quentin Pouplard14-Mar-03 6:28 
GeneralKeyboard Pin
Member 2181813-Mar-03 22:47
Member 2181813-Mar-03 22:47 
QuestionWhy hate the focus rectangle? Pin
ector3-Mar-03 4:37
ector3-Mar-03 4:37 

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

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