CButtonST

09/09/99

Author: davide_calabro@yahoo.com
ICQ: 
HOMEPAGE: http://members.tripod.com/~SoftechSoftware/cbtnst.html

Copyright (C) 1998-99 by SoftechSoftware
Thank you for use this SoftechSoftware! Thanks very much!

OverView
Class Members
Base Class CButton

 

Overview

#include <BtnST.h>

Hierarchy Chart

 


CButtonST is a class derived from MFC CButton class.
With this class your applications can have standard buttons or new and modern buttons with "flat" style!

CButtonST features:

  • Standard CButton properties
  • Text and icon buttons
  • Only text or only icon buttons
  • 16x16 32x32 48x48 16 and 256 colors icon support
  • Support for transparent buttons
  • Standard or new "flat" button style
  • Change runtime from "flat" to standard styles
  • Button can have two icons. One when the mouse is over the button
    and one when the mouse is off (only for "flat" buttons)
  • Every color can be customized
  • Can be used via DDX_ calls
  • Can be used in DLLs
  • Can be dinamically created
  • Each button can have its own mouse pointer
  • Button is hilighted also when the window is inactive, like happens
    in Internet Explorer (this is user configurable)
  • Built-in support for tooltips
  • Written in Visual C++ v5.0
  • Full source code included!
  • It's free!


You are encouraged to use this class everywhere you want; there is no fee required for CButtonST. Freely add modifications and/or fix bugs, but please, send any of these to SoftechSoftware!

How to integrate CButtonST in your application

    In your project include the following files:

BtnST.h
BtnST.cpp

    With dialog editor create a standard button called, for example, IDOK
    (you don't need to make it owner drawn) and create a member variable for this button:

       CButtonST m_btnOk;

 
    Now, with resource editor, create an icon called, for example, IDI_OK256
    You can also import it from one that already exists, such as, one included
    in the sample project (i.e. 32x32x256_Ok.ico).

    Remember to use 32x32 pixel icons. Even if there is support for icons wich
    dimension is not 32x32 they can be drawn incorrectly.

    Now attach the button to CButtonST. In your OnInitDialog procedure:

       // Call the base method
        CDialog::OnInitDialog();

        // Create the IDOK button
        m_btnOk.SubclassDlgItem(IDOK, this);
        // Assign the icon
        m_btnOk.SetIcon(IDI_OK256);

 

    You may use two icons for the same button. First icon will be displayed when
    the mouse pointer is over the button. Second icon will be displayed when the
    mouse pointer is outside the button. If you design two same icons but one
    with colors and one black & white you can have a great visual effect!

        // Assign two icons
        m_btnOk.SetIcon(IDI_OK256, IDI_OK256_BW);

 

    Please note that two icons must have the same size!

    By default the button will have the icon on the left and the text on
    the right. If you want a button with the icon on the top and the text
    on the bottom, you must set the alignment of the button:

        // Align icon vertically
        m_btnOk.SetAlign(CButtonST::ST_ALIGN_VERT);


    
    Again, by default, the button will have the new "flat" style. If you want
    a standard button just use the following piece of code:

        // Draw the button as a standard button
        m_btnOk.SetFlat(FALSE);

 

    The following applies only with "flat" buttons: by default when the mouse
    pointer passes over the button, this highlights itself. If you don't want
    this just disable it. (In the demo program look what happens at the button
    with the CD-ROM inside it!)

        // Don't draw border for this button
        m_btnOk.DrawBorder(FALSE);

    
    Every button's color can be customized.
    Background color is the button's face color while foreground color is the
    text color. Inactive colors are that colors shown when the mouse is off the
    button while active colors are that colors shown when the mouse is over.

        // Set some color effect
        COLORREF crStandard = ::GetSysColor(COLOR_BTNFACE);
        m_btnOk.SetInactiveBgColor(crStandard - RGB(20,20,20));
        m_btnOk.SetActiveBgColor(crStandard + RGB(20,20,20));
        m_btnOk.SetInactiveFgColor(RGB(0,255,0));
        m_btnOk.SetActiveFgColor(RGB(255,0,0));
   

    Each button can have its own mouse pointer.
    For example a button that runs an internet browser could change the pointer
    to a hand when the user moves the mouse on it!

        // Set a hand cursor
        m_btnOk.SetBtnCursor(IDC_HAND);

    
    Every good control has its own tooltip! The little short text displayed when the mouse
    stops over it for a certain time.

        // Assign a tooltip to the button
        m_btnOk.SetTooltipText(IDS_TT_OK);

    
    Recent beatyful interfaces have a bitmapped background! A transparent button is a button
    that shows the dialog/window background.
    Note: read "DrawTransparent" documentation for important notes!

        // Make a transparent button
        m_btnOk.DrawTransparent();

 

    Your button is now a CButtonST!

    Look inside the demo program to learn more about CButtonST. This is the best way!

 

Class Members

Properties

Public:

SetIcon

Set icon(s) for the button

SetAlign

Set icon position (if one is defined)

GetAlign

Get current icon position 

SetFlat

Set button style ("flat" or standard)

GetFlat

Return current button style 

DrawBorder

Set highlight ON/OFF (only for "flat" buttons)

SetShowText

Add or remove text (caption) from button at runtime

GetShowText

Return current text status (displayed or not)

SetDefaultActiveFgColor

Sets to the system default the text's color.

SetDefaultActiveBgColor

Sets to the system default the button's color 

SetDefaultInactiveFgColor

Sets to the system default the text's color 

SetDefaultInactiveBgColor

Sets to the system default the button's color 

SetActiveFgColor

Sets the text's color 

SetActiveBgColor

Sets the button's color 

SetInactiveFgColor

Sets the text's color 

SetInactiveBgColor

Sets the button's color 

GetActiveFgColor

Returns the current text's color 

GetActiveBgColor

Returns the current button's color 

GetInactiveFgColor

Returns the current button's color 

GetInactiveBgColor

Returns the current button's color 

SetFlatFocus

Enable/Disable the drawing of the focus rectangle

GetFlatFocus

Returns the state of the focus rectangle

SetBtnCursor

Assign a cursor to the button
SetTooltipText Assign a tooltip text to the button

ActivateTooltip

Enable/Disable the button's tooltip

DrawTransparent

Draws the button transparently

GetDefault

Returns if the button is the default-button

Protected:

None

Private:

None

 

CButtonST::SetIcon

void SetIcon(int nIconInId, int nIconOutId = NULL);

Return Value:

None

Arguments:

nIconInId

The icon resource (when the mouse pointer is inside)

nIconOutId

The icon resource (when the mouse pointer is outside)
If NULL the first icon will be used instead

Remarks:

Set icon(s) for the button

Example:

m_btnOk.SetIcon(IDI_OK256);
m_btnOk.SetIcon(IDI_OK256, IDI_OK256_BW);
m_btnOk.SetIcon(NULL); // To get a button without icon(s)

 

CButtonST::SetAlign

void SetAlign(int nAlign)

Return Value:

None

Arguments:

nAlign

Input values:
  • ST_ALIGN_HORIZ
    Icon on the left and text on the right (default)
  • ST_ALIGN_VERT
    Icon on top and text on bottom

Remarks:

Set icon position (if one is defined)

Example:

m_btnOk.SetAlign(CButtonST::ST_ALIGN_VERT);

  

CButtonST::GetAlign

int GetAlign();

Return Value:

int

Possible return values.
  • ST_ALIGN_HORIZ
    Icon on the left and text on the right (default)
  • ST_ALIGN_VERT
    Icon on top and text on bottom

Arguments:

None

Remarks:

Get current icon position 

Example:

int nRetValue = m_btnOk.GetAlign();

  

CButtonST::SetFlat

void SetFlat(BOOL bState = TRUE)

Return Value:

None

Arguments:

bState

Input values:
  • TRUE
    Button is drawn as "flat" (default)
  • FALSE
    Button is drawn as standard

Remarks:

Set button style ("flat" or standard)

Example:

m_btnOk.SetFlat();
m_btnOk.SetFlat(FALSE);

 

CButtonST::GetFlat

BOOL GetFlat()

Return Value:

BOOL

Input values:

  • TRUE
    Button is drawn as "flat" (default)
  • FALSE
    Button is drawn as standard

Arguments:

None

Remarks:

Return current button style 

Example:

int nRetValue = m_btnOk.GetFlat();

 

CButtonST::DrawBorder

void DrawBorder(BOOL bEnable = TRUE)

Return Value:

None

Arguments:

bEnable

Input values:
  • TRUE
    Button highlights itself (default)
  • FALSE
    Button doesn't highlight itself

Remarks:

Set highlight ON/OFF (only for "flat" buttons)

Example:

m_btnOk.DrawBorder();
m_btnOk.DrawBorder(FALSE);

 

CButtonST::SetShowText

void SetShowText(BOOL bShow = TRUE)

Return Value:

None

Arguments:

bShow

Input values:
  • TRUE
    Text is displayed
  • FALSE
    Text is not displayed (but still exists!)

Remarks:

Add or remove text (caption) from button at runtime

Example:

m_btnOk.SetShowText();
m_btnOk.SetShowText(FALSE);

 

CButtonST::GetShowText

BOOL GetShowText();

Return Value:

BOOL

Return current text status (displayed or not)

Arguments:

None

Remarks:

Return current text status (displayed or not)

Example:

int nRetValue = m_btnOk.GetShowText();

 

CButtonST::SetDefaultActiveFgColor

void SetDefaultActiveFgColor()

Return Value:

None

Arguments:

None

Remarks:

Sets to the system default the text's color (when the mouse is over the button)
This is automatically called when a button is created.

Example:

m_btnOk.SetDefaultActiveFgColor();

 

CButtonST::SetDefaultActiveBgColor

void SetDefaultActiveBgColor()

Return Value:

None

Arguments:

None

Remarks:

Sets to the system default the button's color (when the mouse is over it)
This is automatically called when a button is created.

Example:

m_btnOk.SetDefaultActiveBgColor();

 

CButtonST::SetDefaultInactiveFgColor

void SetDefaultInactiveFgColor()

Return Value:

None

Arguments:

None

Remarks:

Sets to the system default the text's color (when the mouse is outside the button)
This is automatically called when a button is created.

Example:

m_btnOk.SetDefaultInactiveFgColor();

 

CButtonST::SetDefaultInactiveBgColor

void SetDefaultInactiveBgColor();

Return Value:

None

Arguments:

None

Remarks:

Sets to the system default the button's color (when the mouse is outside it)
This is automatically called when a button is created.

Example:

m_btnOk.SetDefaultInactiveBgColor();

 

CButtonST::SetActiveFgColor

void SetActiveFgColor(COLORREF crNew, BOOL bRepaint = FALSE)

Return Value:

None

Arguments:

crNew

The new color

bRepaint

If TRUE the button is immediately redrawn

Remarks:

Sets the text's color (when the mouse is over the button)

Example:

m_btnOk.SetActiveFgColor(RGB(255, 255, 0));

 

CButtonST::SetActiveBgColor

void SetActiveBgColor(COLORREF crNew, BOOL bRepaint = FALSE);

Return Value:

None

Arguments:

crNew

The new color

bRepaint

If TRUE the button is immediately redrawn

Remarks:

Sets the button's color (when the mouse is over it)

Example:

m_btnOk.SetActiveBgColor(RGB(128, 128, 128));
m_btnOk.SetActiveBgColor(::GetSysColor(COLOR_BTNFACE), TRUE);

 

CButtonST::SetInactiveFgColor

void SetInactiveFgColor(COLORREF crNew, BOOL bRepaint = FALSE);

Return Value:

None

Arguments:

crNew

The new color

bRepaint

If TRUE the button is immediately redrawn

Remarks:

Sets the text's color (when the mouse is outside the button)

Example:

m_btnOk.SetInactiveFgColor(RGB(255, 255, 255));

 

CButtonST::SetInactiveBgColor

void SetInactiveBgColor(COLORREF crNew, BOOL bRepaint = FALSE)

Return Value:

None

Arguments:

crNew

The new color

bRepaint

If TRUE the button is immediately redrawn

Remarks:

Sets the button's color 

Example:

m_btnOk.SetInactiveBgColor(RGB(128, 128, 128));

 

CButtonST::GetActiveFgColor

const COLORREF GetActiveFgColor()

Return Value:

COLORREF

Returns the current text's color 

Arguments:

None

Remarks:

Returns the current text's color (when the mouse is over the button)

Example:

COLORREF crCurrent = m_btnOk.GetActiveFgColor();

 

CButtonST::GetActiveBgColor

const COLORREF GetActiveBgColor()

Return Value:

COLORREF

Returns the current button's color 

Arguments:

None

Remarks:

Returns the current button's color (when the mouse is over it)

Example:

COLORREF crCurrent = m_btnOk.GetActiveBgColor();

 

CButtonST::GetInactiveFgColor

const COLORREF GetInactiveFgColor();

Return Value:

COLORREF

Returns the current text's color 

Arguments:

None

Remarks:

Returns the current text's color (when the mouse is outside the button)

Example:

COLORREF crCurrent = m_btnOk.GetInactiveFgColor();

 

CButtonST::GetInactiveBgColor

const COLORREF GetInactiveBgColor();

Return Value:

COLORREF

Returns the current button's color 

Arguments:

None

Remarks:

Returns the current button's color (when the mouse is outside it)

Example:

COLORREF crCurrent = m_btnOk.GetInactiveBgColor();

 

CButtonST:SetFlatFocus

void SetFlatFocus(BOOL bDrawFlatFocus, BOOL bRepaint = FALSE);

Return Value:

None

Arguments:

bDrawFlatFocus

When TRUE the "flat" buttons will have the focus rectangle
By default "flat" buttons have not the focus rectangle

bRepaint

If TRUE the button is immediately redrawn

Remarks:

Enable/Disable the drawing of the focus rectangle
This is valid ONLY for "flat" buttons; standard buttons always have the focus rectangle.

Example:

m_btnOk.SetFlatFocus(TRUE);
m_btnOk.SetFlatFocus(FALSE, TRUE);

 

CButtonST::GetFlatFocus

BOOL GetFlatFocus();

Return Value:

BOOL

Return value:
  • TRUE
    Focus rectangle will be drawn
  • FALSE
    Focus rectangle will not be drawn

Arguments:

None

Remarks:

Returns the state of the focus rectangle
This is valid ONLY for "flat" buttons; standard buttons always have the focus rectangle.

Example:

BOOL bDrawFlatFocus = m_btnOk.GetFlatFocus();

 

CButtonST::SetBtnCursor

BOOL SetBtnCursor(int nCursorId = -1);

Return Value:

BOOL

Return value:
  • TRUE
    Cursor assigned
  • FALSE
    Cannot load cursor resource

Arguments:

nCursorId

The cursor resource identifier

Remarks:

Assign a cursor to the button
The mouse pointer will change when over the button.

Example:

BOOL bRetValue = m_btnOk.SetBtnCursor(IDC_HAND);
m_btnOk.SetBtnCursor(); // To remove the assigned cursor

  

CButtonST::SetTooltipText

void SetTooltipText(int nId, BOOL bActivate = TRUE)
void SetTooltipText(CString* spText, BOOL bActivate = TRUE)

Return Value:

None

Arguments:

nId

A string resource containing the tooltip text

spText

A pointer to a CString object containing the tooltip text (cannot be NULL)

bActivate

If TRUE the tooltip will be active (and shown when the mouse stops over the button)

Remarks:

Assign a tooltip text to the button
Note: use ActivateTooltip() member to enable/disable the tooltip (see below)

Example:

m_btnOk.SetTooltipText(IDS_TT_OK);
m_btnOk.SetTooltipText(&CString("Some text"), FALSE); // Assign a tooltip but create it disabled

  

CButtonST::ActivateTooltip

void ActivateTooltip(BOOL bEnable = TRUE)

Return Value:

None

Arguments:

bEnable

If TRUE the tooltip will be active (and shown when the mouse stops over the button)

Remarks:

Enable/Disable the button's tooltip

Example:

m_btnOk.ActivateTooltip(FALSE);

  

CButtonST::DrawTransparent

void DrawTransparent()

Return Value:

None

Arguments:

None

Remarks:

Draws the button transparently

Note: this operation is not reversible. DrawTransparent should be called just after the button is created. Do not use trasparent buttons until you really need it (you have a bitmapped background) since each transparent button makes a copy in memory of its background. This may bring unnecessary memory use and execution overload.

Example:

m_btnOk.DrawTransparent();

  

CButtonST::GetDefault

BOOL GetDefault();

Return Value:

BOOL

Return value:
  • TRUE
    Button is the default-button
  • FALSE
    Button is NOT the default-button

Arguments:

None

Remarks:

Returns if the button is the default-button
Note: use dialog-editor to set the default button

Example:

BOOL bDefault = m_btnOk.GetDefault();