Click here to Skip to main content
15,885,244 members
Articles / Desktop Programming / MFC
Article

CFlexiButton Class

Rate me:
Please Sign up or sign in to vote.
3.27/5 (8 votes)
19 Feb 20043 min read 78.6K   7.5K   50   4
A class that lets you take a break from those rectangular buttons

Image 1

Image 2

Introduction

Don't be discouraged at the sight of the pictures above. Well, it is after all, a demo.

This is a class useful to those of my fellow programmers who are tired of the ever familiar gray rectangles on the screen. Well, here is something that might give u relief from that... something for a change!!! I had written this code sometime back and hope some of you will find some use for it.

Ahem… what's the big deal?? Could use CBitmapButton? Yes you could but you might have to give 4 different pictures for each state; but not with CflexiButton!! You have to give just one pic and voila!! It generates its own pictures for all the button states! And there is another funny little thing in there; if you were to change the theme of your system, then the button will change the button's look in synchrony with the theme. Yes it is a cool little thing.

Each of the pictures you see on that dialog up there is a button. Each of them demonstrate a different style of this flexi button. The first one on the left is a button which changes to the theme color in force in the system. The funny little up-turned “Next” button is enabled and it represents the active area of the button exactly. Then the bottom most picture which behaves something like a normal Bitmap button. Try it and look what happens. The first button gets disabled. Then finally the ‘grayed’ play picture on top, that is, a ‘grayed’ play button.

And yes, I have taken help from code project a lot. I had to go through a few articles and borrow functions from some of them (and fix the bugs) to complete this neat little class. CXImage, PictureEx, WindowBitmap... to name a few... thanks to the authors!!

Background

I was programming a mp3 database that works along side my winamp. Then I thought I'd change the look of the interface from the conventional rectangles. That's what led me to change the look of the buttons and thus, the creation of this class.

Using the code

BUTTON EVENTS

#define FB_BTNCLIK WM_USER+111
//Fired when the button is clicked
#define FB_BTNHOVR WM_USER+112
//Fired when the mouse hovers the button
#define FB_BTNLEAV WM_USER+113
//Fired when the mouse leaves the button

PRESETS

//used if the FBS_PRESSEFFECT style is specified 
//and when the mouse leaves the button
#define BTN_PRS_DISP 1
// Effects Supported
#define FB_CREATEBMPS_SUPPORT 1

BUTTON STYLES

** Styles used during Bitmap Loading only
// specifies if the button size is to be adjusted to the size of the window
#define FBS_ADJUSTTOBMP 0x00000010
** Other Styles
// The press effect if only one bitmap is specified
#define FBS_PRESSEFFECT 0x00000001 
// Specifies if the bitmaps are to be blended to system color.
#define FBS_BLENDSYSCLR 0x00000100
// Specifies if all bitmaps are to be created from the first bitmap.
#define FBS_CREATE4ALL 0x00001000
// Specifies if all bitmaps are to be updated if the system color changes.
#define FBS_UPDATEBMPS 0x00010000

MEMBER FUNCTIONS

// Enable And Disable Window
bool CFlexiButton::EnableWindow(bool bEnable) 
// Show Button or hide it
bool CFlexiButton::ShowButton(bool bShow)
// Show button region
bool CFlexiButton::ShowButtonRegion(HRGN hRgn)
// A predefined region can be set as the visible region of the button.
// Delete region with specified color and tolerance from lpszBitmapResource
bool CFlexiButton::DeleteButtonRegion(COLORREF sRef,COLORREF sTol = 0x000000)
// A color with tolerance can be set to be deleted from the
// region of the button. The rest would be the region of the button.
// Load Bitmaps
bool CFlexiButton::LoadBitmaps (LPCTSTR lpszBitmapResource, 
 LPCTSTR lpszBitmapResourceSel = NULL, 
 LPCTSTR lpszBitmapResourceFocus = NULL, 
 LPCTSTR lpszBitmapResourceDisabled = NULL, 
 bool bFromFile = 0)
  1. First bitmap is compulsory, the disabled bitmap is generated automatically if is is not specified.
  2. If the FBS_CREATE4ALL style is set, this function will take only the first bitmap and generate images for all other button states.
  3. If the FBS_BLENDSYSCLR style is set, then all bitmap colors are blended to the current window frame color.
// Get Button's Handle
HWND CFlexiButton::GetWindowHandle()
// Create Button with Styles and button sizes
HWND CFlexiButton::CreateButton (HWND hWnd, int iStyle, 
 int iButtonID, RECT sRect)
  1. The FBS_ADJUSTTOBMP should be specified if here if button is to be adjusted accordingly. Removing or adding this style after this function does not have any effect.
  2. A resource ID identifying the Button must be set. And the bounding rectangle of the button i.e., the coordinates must be in relation to the client area of the parent window.
  3. If u specify a MFC Button with handler for the BN_CLICKED event, and pass the resource ID of that button to the CreateButton Function, the CFlexibutton will use that handler of that MFC button. there is no need to trap any event in OnCommand of the parent window.

I hope the above description of the methods will make things clear to most of you. Feel free to contact me if you have any trouble.

// Please take care to make necessary change according to your circumstances..
 
//
//*** Do this where u would like to declare the object ***//
#include "FlexiButton.h"
 
using namespace C001_FlexButton;
 
     CFlexiButton oFButton;
 
//*** Do similarly where u would like to create and show the button ***//
     oR.left = 200;
     oR.top =250;
     oR.bottom = 400;
     oR.right = 300;
     if(!oFButton4.CreateButton(m_hWnd,FBS_ADJUSTTOBMP,IDC_FLEXBUTTON4,oR))
         return 0;
     if(!oFButton4.LoadBitmaps("wire13.bmp","wire12.bmp","wire1.bmp","wire14.bmp",1))
         return 0;
     oFButton4.ShowButton(1);
//

The above is the code to create the button on the bottom of the dialog in the picture above. Remember there are some more styles provided by this class.

Points of Interest

Oh yes... you just might find some non-intentional interesting behaviour while using this class, please let me know about it. I was considering writing a log file... for a button control?? NAAAAY!!!!

History

This is my first submit to code project or any programmer’s site for that matter; I'll call it... Ummmm... CFlexiButton No.1.0.

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

Comments and Discussions

 
GeneralDll do not functionate Pin
Devil for ever27-May-06 3:19
Devil for ever27-May-06 3:19 
GeneralCrash with Bitmaps at 256 Colors Pin
Michele Solazzi15-Feb-05 6:05
Michele Solazzi15-Feb-05 6:05 
Generalhelp me Pin
iipc14-Jul-04 1:58
iipc14-Jul-04 1:58 
GeneralRe: help me Pin
iipc15-Jul-04 20:05
iipc15-Jul-04 20:05 
we can use functional key by using VK_ followed by functional key name inplace of VkKeyScan() used in RegisterHotKey().

ankoor

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.