Click here to Skip to main content
Licence 
First Posted 3 Feb 2003
Views 111,520
Bookmarked 42 times

Owner-draw button

By | 3 Feb 2003 | Article
A button-derived class that makes push buttons looks like a 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:

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:
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)
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 being useful with my owner-drawn button:)

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

Georgi Atanasov

Team Leader
Telerik Corp.
Bulgaria Bulgaria

Member

Follow on Twitter Follow on Twitter
Windows Phone 7 addicted. Lead of WP7 team @ telerik

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. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 1 Pinmember12Monkeys22:19 20 Apr '09  
GeneralMy vote of 1 PinmemberChewsHumans12:34 8 Dec '08  
GeneralShorter Method PinmemberpraetorianCP18:04 19 Oct '05  
GeneralCould be a lot better PinmemberQuentin Pouplard6:28 14 Mar '03  
GeneralKeyboard PinmemberMav3122:47 13 Mar '03  
QuestionWhy hate the focus rectangle? Pinmemberector4:37 3 Mar '03  

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.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 4 Feb 2003
Article Copyright 2003 by Georgi Atanasov
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid