C++ Builder

La Isla Bonita



Coloured Buttons Class
Coloured Buttons Class


Description

This example show how to create a Coloured Button object using the TColouredButton Class. This class encapsulates the attributes and the behavior of the Coloured Button. All the class methods are defined inside the class. The program creates two buttons, a normal TButton and a TColouredButton. These buttons are created dynamically on an empty form. The user can select both the colors of the button (up and down face colors) and the colors of its caption (up and down font colors).

ColouredButtons.cpp

#include <vcl.h>
#pragma hdrstop

#include "ColouredButtons.h"
//-----------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1*  Form1;
TButton* Button3;
//Global object
TColouredButton oColorButton;


__fastcall TForm1::TForm1( TComponent* Owner )
        : TForm(Owner)
{
   Color    = clBlack;   //Form Color
   Position = poScreenCenter;
   Width    = 600;
   Height   = 400;

   //Button3
   //Create a normal button
   Button3 = new TButton( this );
   Button3->Parent     = this;
   Button3->Top        = 60;
   Button3->Left       = 100 + 320;
   Button3->Height     = 100;
   Button3->Width      = 150;
   Button3->Caption    = "Button3 (Real)";
   Button3->OnClick    = Button3Click;
   Button3->OnKeyDown  = Button3KeyDown;
   Button3->OnKeyPress = Button3KeyPress;
   Button3->OnKeyUp    = Button3KeyUp;

   //Create a coloured button
   oColorButton.New( this,
                     50, 100, 20, 150,   //Position Top, Height, Left, Width
                     clBlue,             //Button Color: Button Up
                     clPurple,           //Button Color: Button Down
                     clWhite,            //Font Color Button Up
                     clWhite,            //Font Color Button Down
                     //Button
                     Button1Click,
                     Button1KeyDown,
                     Button1KeyPress,
                     Button1KeyUp,
                     //Panel
                     Panel1Click,
                     Panel1MouseDown,
                     Panel1MouseUp,
                     Panel1MouseMove );
}
//------------------------------------------------------------------------


//-------------------
//PANEL1 - BUTTON1.
//-------------------
void __fastcall TForm1::Button1Click( TObject *Sender )
{
    ShowMessage("Button1Click():\nthis->ControlCount = "
                 + String( this->ControlCount ) + "\n"
                 + "this->ComponentCount = "
                 + String( this->ComponentCount ) + "\n"
                 +"Button1->ControlCount = "
                 + String( oColorButton.GetButton()->ControlCount ) );
}
//------------------------------------------------------------------------

void __fastcall TForm1::Button1MouseMove( TObject *Sender,
                                          TShiftState Shift,
                                          int X, int Y)
{
    //TEST
    //ShowMessage( "Button1MouseMove()\n"
    //             + String("X = ") + String(X) + "   Y = " + String(Y) );
    //
}
//------------------------------------------------------------------------


void __fastcall TForm1::Button1KeyDown(TObject *Sender, WORD &Key,
      TShiftState Shift)
{
    oColorButton.ButtonKeyDown();
}
//------------------------------------------------------------------------

void __fastcall TForm1::Button1KeyPress(TObject *Sender, char &Key)
{
    oColorButton.ButtonKeyPress();
}
//------------------------------------------------------------------------

void __fastcall TForm1::Button1KeyUp(TObject *Sender, WORD &Key,
      TShiftState Shift)
{
    oColorButton.ButtonKeyUp();
}
//------------------------------------------------------------------------

void __fastcall TForm1::Panel1Click( TObject *Sender )
{
// This handler will not execute when the Panel event OnClick occurs
// because we replaced the event handler.
}
//------------------------------------------------------------------------

void __fastcall TForm1::Panel1MouseDown( TObject *Sender,
                                         TMouseButton Button,
                                         TShiftState Shift,
                                         int X, int Y)
{
    oColorButton.PanelMouseDown();
}
//------------------------------------------------------------------------

void __fastcall TForm1::Panel1MouseUp( TObject *Sender,
                                       TMouseButton Button,
                                       TShiftState Shift,
                                       int X, int Y)
{
    oColorButton.PanelMouseUp();
        
}
//------------------------------------------------------------------------

void __fastcall TForm1::Panel1MouseMove( TObject *Sender,
                                         TShiftState Shift,
                                         int X, int Y )
{
    //Use if needed
    Button1MouseMove( Sender, Shift, X, Y);
}
//------------------------------------------------------------------------


//Button 3 : Real Button
void __fastcall TForm1::Button3KeyDown(TObject *Sender, WORD &Key,
      TShiftState Shift)
{
    Button3->Caption = "Key Down";

}
//------------------------------------------------------------------------

void __fastcall TForm1::Button3KeyPress(TObject *Sender, char &Key)
{
    Button3->Caption = "Key Pressed";
}
//------------------------------------------------------------------------

void __fastcall TForm1::Button3KeyUp(TObject *Sender, WORD &Key,
      TShiftState Shift)
{
    Button3->Caption = "Button3 (Real)";
    
}
//------------------------------------------------------------------------

void __fastcall TForm1::Button3Click(TObject *Sender)
{
    ShowMessage("Button3Click():\nthis->ControlCount = "
                 + String( this->ControlCount ) + "\n"
                 + "this->ComponentCount = "
                 + String( this->ComponentCount ) + "\n"
                 +"Button3->ControlCount = "
                 + String( Button3->ControlCount ) );

}

ColouredButtons.h

#ifndef ColouredButtonsH
#define ColouredButtonsH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <Buttons.hpp>
#include <ExtCtrls.hpp>
//---------------------------------------------------------------------------

class TColouredButton
{
  private:
    TButton* Button1;
    TPanel*  Panel1;
    TColor   Color_ButtonUp;
    TColor   Color_ButtonDown;
    TColor   Color_ButtonFontUp;
    TColor   Color_ButtonFontDown;

  public:
    //CONSTRUCTOR
    TColouredButton( )
    {}

    TButton* GetButton()
    {
        return Button1;
    }

    TPanel* GetPanel()
    {
        return Panel1;
    }

    //Event Handlers Routines
    //Button
    void ButtonKeyDown()
    {
        Panel1->Color       = Color_ButtonDown;
        Panel1->Font->Color = Color_ButtonFontDown;
        Panel1->Caption     = "Button1";
    }

    void ButtonKeyPress()
    {
        Panel1->Color       = Color_ButtonDown;
        Panel1->Font->Color = Color_ButtonFontDown;
        Panel1->Caption     = "Button1";
    }

    void ButtonKeyUp()
    {
        Panel1->Color       = Color_ButtonUp;
        Panel1->Font->Color = Color_ButtonFontUp;
        Panel1->Caption     = " Button1 ";
    }

    //Panel
    void PanelMouseDown( )
    {
        Panel1->BevelInner  = bvLowered;
        Panel1->BevelOuter  = bvLowered;
        Panel1->Font->Color = Color_ButtonFontDown;
        Panel1->Caption     = "Button1";
        Panel1->Color       = Color_ButtonDown;
        //-------------------------------------
        Panel1->Top    += 1;
        Panel1->Left   += 2;
        Panel1->Height -= 1;
        Panel1->Width  -= 2;
        Button1->Top   += 2;
        Button1->Left  += 2;
    }

    void PanelMouseUp()
    {
        Panel1->BevelInner  = bvRaised;
        Panel1->BevelOuter  = bvRaised;
        Panel1->Font->Color = Color_ButtonFontUp;
        Panel1->Caption     = "Button1";    //
        Panel1->Color       = Color_ButtonUp;
        //-------------------------------------

        Panel1->Top    -= 1;
        Panel1->Left   -= 2;
        Panel1->Height += 1;
        Panel1->Width  += 2;
        Button1->Top   -= 2;
        Button1->Left  -= 2;
    }

    void New ( TWinControl* p,
               int top, int height, int left, int width,
               TColor color_buttonup,
               TColor color_buttondown,
               TColor color_buttonfontup,
               TColor color_buttonfontdown,
               TNotifyEvent    button_onclick,
               TKeyEvent       button_onkeydown,
               TKeyPressEvent  button_onkeypress,
               TKeyEvent       button_onkeyup,
               TNotifyEvent    panel_onclick,
               TMouseEvent     panel_onmousedown,
               TMouseEvent     panel_onmouseup,
               TMouseMoveEvent panel_onmousemove )

    {
        Color_ButtonUp       = color_buttonup;
        Color_ButtonDown     = color_buttondown;
        Color_ButtonFontUp   = color_buttonfontup;
        Color_ButtonFontDown = color_buttonfontdown;

          //Button1
        Button1 = new TButton( p );
        Button1->Parent = p;
        Button1->Top    = top;
        Button1->Left   = left;
        Button1->Height = height;
        Button1->Width  = width;
        //Events = Event Handlers
        Button1->OnClick    = button_onclick;    //Button1Click;
        Button1->OnKeyDown  = button_onkeydown;  //Button1KeyDown;
        Button1->OnKeyPress = button_onkeypress; //Button1KeyPress;
        Button1->OnKeyUp    = button_onkeyup;    //Button1KeyUp;
        Button1->Caption    = "Whoops";          //Shouldn't show (test)

          //Panel1
        //Button1 is the Owner of Panel1 so that
        //Button1 will free the memory allocated to Panel1.
        //Button1 is the Parent of Panel1 so that
        //Panel1 is displayed on Button1.
        Panel1 = new TPanel( Button1 );
        Panel1->Parent      = Button1;
        Panel1->Top         = 0;
        Panel1->Left        = 0;
        Panel1->Height      = Button1->Height -2;
        Panel1->Width       = Button1->Width  -2 ;
        Panel1->Color       = color_buttonup;
        Panel1->Font->Color = color_buttonfontup;
        Panel1->Caption     = " Button1";
        //Events = Event Handlers
        //-----------------------------------------------------
        //I have replaced thre panel_onclick by button_onclick
        Panel1->OnClick     = button_onclick;     //Button1Click
        //------------------------------------------------------
        Panel1->OnMouseDown = panel_onmousedown;  //Panel1MouseDown;
        Panel1->OnMouseUp   = panel_onmouseup;    //Panel1MouseUp;
        Panel1->OnMouseMove = panel_onmousemove;  //Panel1MouseMove;
        //-------------------------------
        Panel1->BevelInner  = bvRaised;
        Panel1->BevelOuter  = bvRaised;
        //------------------------------
    }
};

class TForm1 : public TForm
{
__published:	// IDE-managed Components
        void __fastcall Button1Click(TObject *Sender);
        void __fastcall Panel1Click(TObject *Sender);
        void __fastcall Panel1MouseUp(TObject *Sender, TMouseButton Button,
          TShiftState Shift, int X, int Y);
        void __fastcall Panel1MouseDown(TObject *Sender,
          TMouseButton Button, TShiftState Shift, int X, int Y);
        void __fastcall Button1MouseMove( TObject *Sender, TShiftState Shift,
                                         int X, int Y );
        void __fastcall Panel1MouseMove(TObject *Sender, TShiftState Shift,
          int X, int Y);
        void __fastcall Button1KeyDown(TObject *Sender, WORD &Key,
          TShiftState Shift);
        void __fastcall Button1KeyPress(TObject *Sender, char &Key);
        void __fastcall Button1KeyUp(TObject *Sender, WORD &Key,
          TShiftState Shift);
        void __fastcall Button3KeyDown(TObject *Sender, WORD &Key,
          TShiftState Shift);
        void __fastcall Button3KeyPress(TObject *Sender, char &Key);
        void __fastcall Button3KeyUp(TObject *Sender, WORD &Key,
          TShiftState Shift);
        void __fastcall Button3Click(TObject *Sender);



private:	// User declarations

public:		// User declarations
        __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

Homepage

Copyright © 1997-2002 Rodolfo A. Frino. All rights reserved.