Click here to Skip to main content
15,884,836 members
Articles / Desktop Programming / MFC
Article

FxButton Class

Rate me:
Please Sign up or sign in to vote.
4.75/5 (23 votes)
24 Nov 20034 min read 189.7K   14.5K   78   27
FxButton allows developers to create customized buttons using captions, bitmaps, tooltips, etc.

First demo - play with one button

Second demo button gallery

Introduction

FxButton allows developers to create customized buttons using captions, bitmaps, tooltips, etc. It cannot do everything, so be sure to check its limitations. I'm a beginner in coding VC++ with MFC and I made this class for fun and - above all - to learn.

The aim of this class - but not the only one - is to make it as easy as possible to integrate it in an application and to have the maximum options available. If you don't know what a handle is and you want a bitmap button, you can use this class.

The first FxButton demo is not a button gallery; it contains only one FxButton but you can play with it. The demo gives access to about 30 button components. Each of these options can be applied to any button state: normal, pushed, over or disabled.

The second FxButton demo is a button gallery. It shows the use of clipping regions for buttons and transparent bitmaps applied to push, check or radio buttons.

What's new in this update

a very minor change but it killed a bad bug. Applications no more crash after a few minutes of execution. I didn't test with Win98-95 but it's ok for the others.

Background (optional)

Limitations

First of all, you may want to know what FxButton cannot do before reading through this document without finding what you're looking for. You should not use FxButton class if you :

  • Want to see animated bitmaps or AVIs on a button.
  • Want to see animated text, flashing colors, or use GIF pictures.
  • Need to use a check button with the 3States property.
  • Want an auto-sized button with the bitmap size drawn on it.
  • Want a button which automatically takes into account all the basic and extended styles available on the Resource Editor of Visual C++.
  • Want to have 5 different bitmaps and 5 different clipping windows using only 2 GDI objects.
  • Want to create a heavily customized button with only 3 lines of codes.

What Fx Button does

An FxButton can contain different data for the normal, pushed, over and disabled states. This means it can manage 4 bitmaps, 4 captions, 4 background colors, etc. Here is a list of all the button items :

  • Background color
  • Border color and size
  • Bitmap
  • Transparent bitmap
  • Backscreen bitmap
  • Text
  • Tooltips
  • Focus
  • Cursor
  • Sound
  • Clipping region

Some other features:

  • The bitmap can be stretched to any position on the button rectangle.
  • The caption can be drawn anywhere on the button
  • Many others options are available; just see the demo and the documents.
  • Button types include: push, radio, check, push-like for check and radio buttons.
  • Buttons can be rounded rectangles.
  • The focus can alter colors in more of the classic rectangle.
  • Use 2 transparent colors for bitmap and clipping regions. It can have different bitmaps while maintaining the same custom clipping region.

Using the code

All FxButton initialization methods start with SetFx, which is a great help when you make your button. Here's an example to show how simple it can be.

Let's say your button is called myButton.

// Init text
myButton.SetFxText("Ok");
myButton.SetFxTextFont("Arial");
// Init bitmap
myButton.SetFxBitmap("IDB_BITMAP1");
myButton.SetFxBitmapTransparentColor1(RGB(0,255,0));
// Init background color
myButton.SetFxBkcolor(RGB(100,100,100));
// Create the button
myButton.FxCreateButton();

Of interest is that the SetFx methods allow developers to set the values for the 4 button states. Almost all SetFx methods have this same rule. Here is one example to set the text:

SetFxText(CString Normal="", bool Duplicate=true,
    CString Pushed="", CString Over="", CString Disabled="");
The Normal, Pushed, Over and Disabled states are the text values for each button state. Duplicate indicates if the Normal value is to be applied to other button states if they are not set. The best way to test a button state is to set its text like:

myButton.SetFxText("Normal", true, "Pushed", "Over", "Disabled");

Now just play with mouse to see the text change.

This one has no text in its normal state, but silently tells you when the mouse is over it, thanks you when clicked, and it hates being disabled. This "silent notification" text button is:

myButton.SetFxText("", false, "Thanks", "Click me !", "Oh No !");

Now you can apply all the SetFx methods in the same way. Here is the SetFx methods list:

Button Sets

SetFxClippingMode
SetFxClippingColor1
SetFxClippingColor2

Background

SetFxBkColor
SetFxBkColorMode

Borders

SetFxBorderSize
SetFxBorderColorMode
SetFxBorderEffect

Bitmap

SetFxBitmap
SetFxBitmapRect
SetFxBitmapPosition
SetFxBitmapTransparentColor1
SetFxBitmapTransparentColor2
SetFxBitmapTransparentMode
SetFxBitmapEffect

Text

SetFxText
SetFxTextRect
SetFxTextPosition
SetFxTextFont
SetFxTextSize
SetFxTextColor
SetFxTextStyle
SetFxTextEffect

Media

SetFxSound
SetFxCursor
SetFxSystemCursor

Tooltip

SetFxToolTip

These SetFx methods initialize the data members. To avoid too many data members, they are stored in structures which are:

m_Background
m_Bitmap
m_Border
m_Text
m_Focus
m_Media
m_ToolTip
m_Button
m_Control

Only the 9 data members help performance. See the documentation for more details about them.

Points of Interest

As shown in the demo, the button can be entirely recreated. Since the clipping regions, bitmaps, colors, captions, etc., can easily be modified, this class could implement a skined application. Another interesting point is the facility to recreate a button. The FxButton class allows developers to change the button bitmaps and clipping regions easily.

History

    November 2003 : First update
    Fix bug
    application crashes - Bitmap drawing anywhere etc...

    Improvment
    No need to Invalidate if you change the text or style or color etc...

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
Software Developer (Junior)
France France
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionFast clicking on a "check box push button" sometimes makes the GetCheck function to return a incorrect answer. Pin
Sunny127025-Jun-12 20:37
Sunny127025-Jun-12 20:37 
AnswerRe: Fast clicking on a "check box push button" sometimes makes the GetCheck function to return a incorrect answer. Pin
RiriOnTheWeb26-Jun-12 6:52
RiriOnTheWeb26-Jun-12 6:52 
GeneralRe: Fast clicking on a "check box push button" sometimes makes the GetCheck function to return a incorrect answer. Pin
Sunny127026-Jun-12 18:40
Sunny127026-Jun-12 18:40 
GeneralRe: Fast clicking on a "check box push button" sometimes makes the GetCheck function to return a incorrect answer. Pin
RiriOnTheWeb28-Jun-12 11:05
RiriOnTheWeb28-Jun-12 11:05 
QuestionCan not enter the Function:void CFxButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) Pin
lablas6-Mar-11 22:34
lablas6-Mar-11 22:34 
AnswerRe: Can not enter the Function:void CFxButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) Pin
RiriOnTheWeb8-Mar-11 1:29
RiriOnTheWeb8-Mar-11 1:29 
GeneralNo text in buttons in windows vista Pin
sirnoname12-Jun-08 11:18
sirnoname12-Jun-08 11:18 
QuestionHow can enable button with Enter Pin
zit_gir26-Oct-05 0:40
zit_gir26-Oct-05 0:40 
GeneralActive with enter Pin
zit_gir23-Oct-05 22:21
zit_gir23-Oct-05 22:21 
Hi,
Your customized button was nice,I used it simply.But I need my FxButtons activated by "Enter" key instead of "Space",How it is possible?
Thanks in advanced.
Neda
GeneralBug Report Pin
Hing27-Nov-03 19:48
Hing27-Nov-03 19:48 
GeneralRe: Bug Report Pin
RiriOnTheWeb28-Nov-03 6:55
RiriOnTheWeb28-Nov-03 6:55 
GeneralRe: Bug Report Pin
Hing28-Nov-03 14:56
Hing28-Nov-03 14:56 
GeneralCFxButton and CFormView Pin
AurCe4-Nov-03 4:25
AurCe4-Nov-03 4:25 
GeneralRe: CFxButton and CFormView Pin
RiriOnTheWeb8-Nov-03 11:37
RiriOnTheWeb8-Nov-03 11:37 
GeneralSetFXBitmapTransparentColor1 does not work for me... Pin
tlee_HK29-Jul-03 18:34
tlee_HK29-Jul-03 18:34 
GeneralRe: SetFXBitmapTransparentColor1 does not work for me... Pin
RiriOnTheWeb25-Aug-03 0:41
RiriOnTheWeb25-Aug-03 0:41 
Generalcool Pin
shanila6-Jul-03 14:56
shanila6-Jul-03 14:56 
GeneralCreate in runtime Pin
azprivat12-Jun-03 23:14
azprivat12-Jun-03 23:14 
GeneralRe: Create in runtime Pin
RiriOnTheWeb16-Jun-03 10:27
RiriOnTheWeb16-Jun-03 10:27 
Generaluhm.. Pin
ector1-Feb-03 2:53
ector1-Feb-03 2:53 
GeneralRe: uhm.. Pin
RiriOnTheWeb2-Feb-03 23:48
RiriOnTheWeb2-Feb-03 23:48 
GeneralInvalid Archive Pin
Steve McLenithan31-Jan-03 7:37
Steve McLenithan31-Jan-03 7:37 
GeneralRe: Invalid Archive Pin
RiriOnTheWeb3-Feb-03 6:45
RiriOnTheWeb3-Feb-03 6:45 
Questionwow, is there more to the button? Pin
Chopper30-Jan-03 23:31
Chopper30-Jan-03 23:31 
AnswerRe: wow, is there more to the button? Pin
RiriOnTheWeb3-Feb-03 6:52
RiriOnTheWeb3-Feb-03 6:52 

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.