CCeButtonST v1.2

The reference control for MFC flat buttons with text and icons. Give your CE applications a professional look!

.

Sample Image

Sample Image

Sample Image

SoftechSoftware homepage
SoftechSoftware Email

Environment: eMbedded VC++ 3.0, Windows CE 3.0

Abstract

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

Main CCeButtonST features are:

  • Standard CButton properties
  • Text and icon on the same button
  • Only text or only icon buttons
  • Support for any size icons (max. 256 colors)
  • Standard or flat button style
  • Change runtime from flat to standard style
  • Buttons can have two images. One when the mouse is over the button and one when the mouse is outside (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
  • Built-in basic support for menus
  • Can be derived to create other button styles not supplied by default
  • Full source code included!
  • UNICODE compatible
  • Windows CE v3.0 compatible
  • Cost-less implementation in existing applications

How to integrate CCeButtonST in your application

In your project include the following files:

  • CeBtnST.h
  • CeBtnST.cpp
Create a CCeButtonST object statically
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:
CCeButtonST m_btnOk;

Now attach the button to CCeButtonST. For dialog-based applications, in your OnInitDialog:

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

// Create the IDOK button
m_btnOk.SubclassDlgItem(IDOK, this);

Or in your DoDataExchange:

// Call the base method
CDialog::DoDataExchange(pDX);

// Create the IDOK button
DDX_Control(pDX, IDOK, m_btnOk);

Create a CCeButtonST object dynamically
In your application, create a member variable for the button. Please note that this variable is a pointer:

CCeButtonST* m_pbtnOk;

Now create the button. For dialog-based applications, in your OnInitDialog:

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

// Create the IDOK button
m_pbtnOk = new CCeButtonST;
m_pbtnOk->Create(_T("&Ok"), 
                    WS_CHILD | WS_VISIBLE | WS_GROUP | WS_TABSTOP, 
                    CRect(10, 10, 200, 100), 
                    this, 
                    IDOK);
// Set the same font of the application
m_pbtnOk->SetFont(GetFont());

Remember to destroy the button or you will get a memory leak. This can be done, for example, in your class destructor:

if (m_pbtnOk) delete m_pbtnOk;

Class methods

SetIcon (using resources)
Assigns icons to the button.
Any previous icon will be removed.

// Parameters:
//      [IN]    nIconIn
//              ID number of the icon resource to show
//              when the mouse is over the button.
//              Pass NULL to remove any icon from the button.
//      [IN]    sizeIn
//              Size of the icon.
//      [IN]    nIconOut
//              ID number of the icon resource to show when 
//              the mouse is outside the button.
//              Can be NULL.
//      [IN]    sizeOut
//              Size of the icon.
//      [IN]    nIconDis
//              ID number of the icon resource to show when 
//              the button is disabled.
//              Can be NULL.
//      [IN]    sizeDis
//              Size of the icon.
//
// Return value:
//      BTNST_OK
//          Function executed successfully.
//
DWORD SetIcon( int nIconIn, 
               CSize sizeIn = CSize(32,32), 
               int nIconOut = NULL, 
               CSize sizeOut = CSize(32,32), 
               int nIconDis = NULL, 
               CSize sizeDis = CSize(32,32))

SetIcon (using handles)
Assigns icons to the button.
Any previous icon will be removed.

// Parameters:
//     [IN]   hIconIn
//            Handle fo the icon to show when the mouse is 
//            over the button.
//            Pass NULL to remove any icon from the button.
//     [IN]   sizeIn
//            Size of the icon.
//     [IN]   hIconOut
//            Handle to the icon to show when the mouse is
//            outside the button.
//            Can be NULL.
//     [IN]   sizeOut
//            Size of the icon.
//     [IN]   hIconDis
//            ID number of the icon resource to show when 
//            the button is disabled.
//            Can be NULL.
//     [IN]   sizeDis
//            Size of the icon.
//
// Return value:
//      BTNST_OK
//          Function executed successfully.
//
DWORD SetIcon( HICON hIconIn, 
               CSize sizeIn = CSize(32,32), 
               HICON hIconOut = NULL, 
               CSize sizeOut = CSize(32,32), 
               HICON hIconDis = NULL, 
               CSize sizeDis = CSize(32,32))

SetFlat
Sets the button to have a standard or flat style.

// Parameters:
//     [IN]   bFlat
//            If TRUE the button will have a flat style, else
//            will have a standard style.
//     [IN]   bRepaint
//            If TRUE the control will be repainted.
//
// Return value:
//     BTNST_OK
//        Function executed successfully.
//
DWORD SetFlat(BOOL bFlat = TRUE, BOOL bRepaint = TRUE)

SetAlign
Sets the alignment type between icon and text.

// Parameters:
//  [IN]   byAlign
//         Alignment type.      Can be one of following values:
//         ST_ALIGN_HORIZ       Icon on left, text on right
//         ST_ALIGN_VERT        Icon on top, text on bottom
//         ST_ALIGN_HORIZ_RIGHT Icon on right, text on left
//                              By default, CCeButtonST buttons 
//                              have ST_ALIGN_HORIZ alignment.
//  [IN]   bRepaint
//         If TRUE the control will be repainted.
//
// Return value:
//     BTNST_OK
//        Function executed successfully.
//     BTNST_INVALIDALIGN
//        Alignment type not supported.
//
DWORD SetAlign(BYTE byAlign, BOOL bRepaint = TRUE)

SetCheck
Sets the state of the checkbox.
If the button is not a checkbox, this function has no meaning.

// Parameters:
//     [IN]   nCheck
//            1 to check the checkbox.
//            0 to un-check the checkbox.
//     [IN]   bRepaint
//            If TRUE the control will be repainted.
//
// Return value:
//     BTNST_OK
//        Function executed successfully.
//
DWORD SetCheck(int nCheck, BOOL bRepaint = TRUE)

GetCheck
Returns the current state of the checkbox.
If the button is not a checkbox, this function has no meaning.

// Return value:
//     The current state of the checkbox.
//        1 if checked.
//        0 if not checked or the button is not a checkbox.
//
int GetCheck()

SetDefaultColors
Sets all colors to a default value.

// Parameters:
//     [IN]   bRepaint
//            If TRUE the control will be repainted.
//
// Return value:
//     BTNST_OK
//        Function executed successfully.
//
DWORD SetDefaultColors(BOOL bRepaint = TRUE)
SetColor
Sets the color to use for a particular state.
// Parameters:
//  [IN]  byColorIndex         
//        Index of the color to set. Can be one of the 
//        following values:
//        BTNST_COLOR_BK_IN     Background color when mouse 
//                              is over the button
//        BTNST_COLOR_FG_IN     Text color when mouse is 
//                              over the button
//        BTNST_COLOR_BK_OUT    Background color when mouse 
//                              is outside the button
//        BTNST_COLOR_FG_OUT    Text color when mouse is 
//                              outside the button
//        BTNST_COLOR_BK_FOCUS  Background color when the 
//                              button is focused
//        BTNST_COLOR_FG_FOCUS  Text color when the 
//                              button is focused
//  [IN]  crColor        New color.
//        
//  [IN]  bRepaint       If TRUE the control will 
//                       be repainted.
// Return value:
//     BTNST_OK
//        Function executed successfully.
//     BTNST_INVALIDINDEX
//        Invalid color index.
//
DWORD SetColor(BYTE byColorIndex, COLORREF crColor, BOOL bRepaint = TRUE)
GetColor
Returns the color used for a particular state.
// Parameters:
//  [IN]   byColorIndex
//         Index of the color to get. Can be one of 
//         the following values:
//         BTNST_COLOR_BK_IN     Background color when 
//                               mouse is over the button
//         BTNST_COLOR_FG_IN     Text color when mouse is
//                               over the button
//         BTNST_COLOR_BK_OUT    Background color when mouse
//                               is outside the button
//         BTNST_COLOR_FG_OUT    Text color when mouse is 
//                               outside the button
//         BTNST_COLOR_BK_FOCUS  Background color when 
//                               the button is focused
//         BTNST_COLOR_FG_FOCUS  Text color when the 
//                               button is focused
//  [OUT]  crpColor
//         A pointer to a COLORREF
//         that will receive the color.
//
// Return value:
//     BTNST_OK
//        Function executed successfully.
//     BTNST_INVALIDINDEX
//        Invalid color index.
//
DWORD GetColor(BYTE byColorIndex, COLORREF* crpColor)

SetAlwaysTrack
Sets the hilight logic for the button.
Applies only to flat buttons.

// Parameters:
//     [IN]   bAlwaysTrack
//            If TRUE the button will be hilighted even 
//            if the window that owns it, is not the 
//            active window.
//            If FALSE the button will be hilighted only if
//            the window that owns it, is the active window.
//
// Return value:
//     BTNST_OK
//        Function executed successfully.
//
DWORD SetAlwaysTrack(BOOL bAlwaysTrack = TRUE)

SetBtnCursor
Sets the cursor to be used when the mouse is over the button.

// Parameters:
//     [IN]   nCursorId
//            ID number of the cursor resource.
//            Pass NULL to rem/>k a previously loaded cursor.
//     [IN]   bRepaint
//            If TRUE the control will be repainted.
//
// Return value:
//     BTNST_OK
//        Function executed successfully.
//     BTNST_INVALIDRESOURCE
//        Failed loading the specified resource.
//
DWORD SetBtnCursor(int nCursorId = NULL, BOOL bRepaint = TRUE)

DrawBorder
Sets if the button border must be drawn.
Applies only to flat buttons.

// Parameters:
//     [IN]   bDrawBorder
//            If TRUE the border will be drawn.
//     [IN]   bRepaint
//            If TRUE the control will be repainted.
//
// Return value:
//     BTNST_OK
//        Function executed successfully.
//
DWORD DrawBorder( BOOL bDrawBorder = TRUE, 
                  BOOL bRepaint = TRUE)

DrawFlatFocus
Sets if the focus rectangle must be drawn for flat buttons.

// Parameters:
//     [IN]   bDrawFlatFocus
//            If TRUE the focus rectangle will be 
//            drawn also for flat buttons.
//     [IN]   bRepaint
//            If TRUE the control will be repainted.
//
// Return value:
//     BTNST_OK
//        Function executed successfully.
//
DWORD DrawFlatFocus( BOOL bDrawFlatFocus, 
                     BOOL bRepaint = TRUE)

GetDefault
Returns if the button is the default button.

// Return value:
//     TRUE
//        The button is the default button.
//     FALSE
//        The button is not the default button.
//
BOOL GetDefault()

SetURL
Sets the URL that will be opened when the button is clicked.

// Parameters:
//     [IN]   lpszURL
//            Pointer to a null-terminated string that 
//            contains the URL.
//            Pass NULL to removed any previously specified URL.
//
// Return value:
//     BTNST_OK
//        Function executed successfully.
//
DWORD SetURL(LPCTSTR lpszURL = NULL)

SetMenu
Associates a menu to the button.
The menu will be displayed clicking the button.

// Parameters:
//     [IN]   nMenu
//            ID number of the menu resource.
//            Pass NULL to remove any menu from the button.
//     [IN]   hParentWnd
//            Handle to the window that owns the menu.
//            This window receives all messages from the menu.
//     [IN]   bRepaint
//            If TRUE the control will be repainted.
//
// Return value:
//     BTNST_OK
//        Function executed successfully.
//     BTNST_INVALIDRESOURCE
//        Failed loading the specified resource.
//
DWORD SetMenu( UINT nMenu, 
               HWND hParentWnd, 
               BOOL bRepaint = TRUE)

OnDrawBackground
This function is called every time the button background needs to be painted. This is a virtual function that can be rewritten in CCeButtonST-derived classes to produce a whole range of buttons not available by default.

// Parameters:
//     [IN]   pDC
//            Pointer to a CDC object that indicates 
//            the device context.
//     [IN]   pRect
//            Pointer to a CRect object that indicates the
//            bounds of the area to be painted.
//
// Return value:
//     BTNST_OK
//        Function executed successfully.
//
virtual DWORD OnDrawBackground(CDC* pDC, LPCRECT pRect)

OnDrawBorder
This function is called every time the button border needs to be painted. This is a virtual function that can be rewritten in CCeButtonST-derived classes to produce a whole range of buttons not available by default.

// Parameters:
//     [IN]   pDC
//            Pointer to a CDC object that indicates 
//            the device context.
//     [IN]   pRect
//            Pointer to a CRect object that indicates 
//            the bounds of the area to be painted.
//
// Return value:
//     BTNST_OK
//        Function executed successfully.
//
virtual DWORD OnDrawBorder(CDC* pDC, LPCRECT pRect)

GetVersionI
Returns the class version as a short value.

// Return value:
//     Class version. Divide by 10 to get actual version.
//
static short GetVersionI()

GetVersionC
Returns the class version as a string value.

// Return value:
//     Pointer to a null-terminated string 
//     containig the class version.
//
static LPCTSTR GetVersionC()

  • v1.2 (18/December/2001)
    Corrected a CE bug in OnLButtonDown
  • v1.1 (28/November/2001)
    Corrected the "DestroyCursor" problem
  • v1.0 (22/October/2001)
    First release

Remarks

The demo application shows nearly all the features of the CCeButtonST class. It includes project settings for all the emulators included in Visual C++ eMbedded Tools v3.0 plus settings to compile and run on the Advantech PCM-4823 single board computer. CCeButtonST architecture makes possible to produce a whole range of buttons not available by default. If someone implements new button styles I will be happy to include his code in the next CCeButtonST demo application.

Downloads

Download demo project - 55 Kb
Download source - 10 Kb


Comments

  • Compiling error too.

    Posted by Legacy on 07/20/2003 12:00am

    Originally posted by: Raymond

    I happened same case.

    comiiling error - alex_kenobe (2003/02/14)

    All the best ... Raymond

    Reply
  • comiiling error

    Posted by Legacy on 02/14/2003 12:00am

    Originally posted by: alex_kenobe

    Hi
    when i compile the demo project using embeded vc++3.0
    I got the following error

    unresolved external symbol " int_stdcall wce_FrameRect(....)
    unresolved external symbol "struct HPEN__*_stdcall wce_CreatPen(int ...)

    am I missing something in the project?
    can you please help me?

    Alex

    Reply
  • Cannt be transpraent in PocketPC

    Posted by Legacy on 12/29/2002 12:00am

    Originally posted by: Rajesh

    Hi,
    
    

    In the emulation, The Button transparent color is work fine. but when i try to run in Pocket PC. transparent doesnt work. Bcoz by default the pocket PC background color is White. so how do i set it transparent?.

    more over In a pocket PC i cant able to see the SetCursor(HAND). Becoz for the Pocket PC i used a Stylus. becoz of that or what?...

    Give me ur suggestion?....

    thanks,
    regards,
    Rajesh. S

    Reply
  • Can it work with CE.Net?

    Posted by Legacy on 12/05/2002 12:00am

    Originally posted by: Frank

    The code works fine with CE3.0, but when I port it to CE.Net, it doesn't work. The icon is gone, only text is left, Is there something I missed?

    Thanks

    Reply
  • Excellent tutorial, problem & fix for DDX_Check()

    Posted by Legacy on 05/03/2002 12:00am

    Originally posted by: Attila Dobozi

    I found this code to be an excellent tutorial for working
    
    with buttons and with custom controls in general.

    The problem:

    I am using the CCeButtonST as a checkbox in a data entry
    form that uses DDX_Check to load/save the checkbox data.
    Unfortunately the DDX_Check does not work with CCeButtonST
    because it is not a real checkbox. The problem lies in the
    DDX asking for the the checkbox's internal check/uncheck
    state which is not used in CCeButtonST. The DDX uses two
    messages to get, BM_GETCHECK, and set, BM_SETCHECK, the checkbox state.

    The fix:

    Handle the BM_GETCHECK and MB_SETCHECK messages within
    CCeButtonST. Doing this will replace the default message
    handlers to return the proper check value.

    .h additions:

    :private
    LRESULT MsgGetCheck(WPARAM wParam, LPARAM lParam);
    LRESULT MsgSetCheck(WPARAM wParam, LPARAM lParam);

    .cpp additions:

    in the BEGIN_MESSAGEMAP... section add

    ON_MESSAGE(BM_GETCHECK, MsgGetCheck)
    ON_MESSAGE(BM_SETCHECK, MsgSetCheck)

    add the following functions

    LRESULT CCeButtonST::MsgGetCheck(WPARAM wParam, LPARAM lParam)
    {
    ASSERT(m_bIsCheckBox); // only for checkboxes
    return (GetCheck() );
    }

    LRESULT CCeButtonST::MsgSetCheck(WPARAM wParam, LPARAM lParam)
    {
    ASSERT(m_bIsCheckBox); // only for checkboxes
    SetCheck(wParam);
    return 0;
    }

    Thanks again for the great code,
    Attila

    Reply
  • Good

    Posted by Legacy on 02/01/2002 12:00am

    Originally posted by: seowontae

    Thank you for your code ...

    Reply
  • Treeview control

    Posted by Legacy on 01/10/2002 12:00am

    Originally posted by: senthil


    How to create treeview control in evc++ using win32 API?

    Reply
  • Suggestions....

    Posted by Legacy on 12/20/2001 12:00am

    Originally posted by: Ramesh

    Hi Davide!
    
    You've done a Good Work. Congrats.
    I'd like to suggest some features.
    1. Button Highliting and showing the border could be optional (may be based on a flag).
    2. Option for Assigning another Image for disabled Status.

    i've tried these and working fine..

    regards

    Ramesh

    Reply
  • You rock..

    Posted by Legacy on 12/12/2001 12:00am

    Originally posted by: theSublime

    all your controls are VERY great.. keep up the good work davide..
    thanks..

    bernhard

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • This paper introduces IBM Java on the IBM PowerLinux 7R2 server and describes IBM's implementation of the Java platform, which includes IBM's Java Virtual Machine and development toolkit.

  • Not all enterprise applications are created equal. Sophisticated applications need developer support but other more basic apps do not. With the right tools, everyone is a potential app developer with ideas and a perspective to share. Trends such as low-code development and model driven development are fundamentally changing how and who creates applications. Is your organization ready? Read this report and learn: The seven personas of enterprise app delivery How application ownership is spreading to the …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds