Example1
This example shows how to create "coloured buttons" dynamically
with TPanel components.
The three-dimensional effect of the simulated button is created
by initializing the panel properties BevelInner and BevelOuter to:
bvRaised. When the button is pressed (OnMouseDown event) these
properties are set to bvLowered. When the button is not pressed (OnMouseUp
event) both properties are set to bvRaised.
The event handlers Panel1Click() and Panel2Click() simulate the corresponding
handlers for the buttons. This method has its limitations since there are
"button events" to which a panel cannot respond to, such as OnKeyDown,
OnKeyPress, OnKeyUp, etc. This method is suitable for those
applications in which the buttons are ckicked via the mouse only.
ColouredButtons.cpp
ColouredButtons.h
Example2
This example shows how to "create" coloured buttons dynamically
by placing a TPanel component on the button.
This method is applied to both TButton objects and TBitBtn objects.
All components (Button1, Panel1, BitBtn1, Panel2, Button3 and Timer1)
on Form1 are created dynamically.
ColouredButtons.cpp
ColouredButtons.h
Copyright © 1997-2002 Rodolfo A. Frino. All rights reserved.
Description
Example 2 simulates buttons by using panels on the top of real buttons (such
as TButton and TBitBtn). This method enhances the behaviour of the simulated
button, so that they also respond to other events.
#include <vcl.h>
#pragma hdrstop
#include "ColouredButtons.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1* Form1;
TPanel* Panel1;
TPanel* Panel2;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
Color = clBlack; //Form Color
Position = poScreenCenter;
Width = 600;
Height = 400;
Panel1 = new TPanel( this );
Panel1->Parent = this;
Panel1->Top = 60;
Panel1->Left = 30;
Panel1->Height = 100;
Panel1->Width = 130;
Panel1->Color = clPurple;
Panel1->Font->Color = clWhite;
Panel1->Caption = " Button1";
Panel1->OnClick = Panel1Click;
Panel1->OnMouseDown = Panel1MouseDown;
Panel1->OnMouseUp = Panel1MouseUp;
Panel1->OnMouseMove = Panel1MouseMove;
//-------------------------------
Panel1->BevelInner = bvRaised;
Panel1->BevelOuter = bvRaised;
//-------------------------------
Panel2 = new TPanel( this );
Panel2->Parent = this;
Panel2->Top = Panel1->Top + Panel1->Height + 40;
Panel2->Left = Panel1->Left;
Panel2->Height = Panel1->Height;
Panel2->Width = Panel1->Width;
Panel2->Color = clPurple;
Panel2->Font->Color = clWhite;
Panel2->Caption = " Button2";
Panel2->OnClick = Panel2Click;
Panel2->OnMouseDown = Panel2MouseDown;
Panel2->OnMouseUp = Panel2MouseUp;
Panel2->OnMouseMove = Panel2MouseMove;
//-------------------------------
Panel2->BevelInner = bvRaised;
Panel2->BevelOuter = bvRaised;
//-------------------------------
}
//---------------------------------------------------------------------------
//-------------------
//PANEL1
//-------------------
void __fastcall TForm1::Panel1Click( TObject *Sender )
{
//
//ShowMessage("Panel1Click");
}
//------------------------------------------------------
void __fastcall TForm1::Panel1MouseDown( TObject *Sender,
TMouseButton Button,
TShiftState Shift,
int X, int Y)
{
//
Panel1->BevelInner = bvLowered;
Panel1->BevelOuter = bvLowered;
Panel1->Font->Color = clBlack;
Panel1->Caption = "Button1";
Panel1->Color = clRed;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Panel1MouseUp( TObject *Sender,
TMouseButton Button,
TShiftState Shift,
int X, int Y)
{
//
Panel1->BevelInner = bvRaised;
Panel1->BevelOuter = bvRaised;
Panel1->Font->Color = clWhite;
Panel1->Caption = " Button1";
Panel1->Color = clPurple;
}
//------------------------------------------------------
void __fastcall TForm1::Panel1MouseMove( TObject *Sender,
TShiftState Shift,
int X, int Y )
{
//
}
//---------------------------------------------------------------------------
//-------------------
//PANEL2
//-------------------
void __fastcall TForm1::Panel2Click(TObject *Sender)
{
//
//ShowMessage("Panel2Click");
}
//------------------------------------------------------
void __fastcall TForm1::Panel2MouseDown( TObject *Sender,
TMouseButton Button,
TShiftState Shift,
int X, int Y)
{
//
Panel2->BevelInner = bvLowered;
Panel2->BevelOuter = bvLowered;
Panel2->Font->Color = clBlack;
Panel2->Caption = "Button2";
Panel2->Color = clRed;
}
//---------------------------------------------------------------
void __fastcall TForm1::Panel2MouseUp( TObject *Sender,
TMouseButton Button,
TShiftState Shift,
int X, int Y)
{
//
Panel2->BevelInner = bvRaised;
Panel2->BevelOuter = bvRaised;
Panel2->Font->Color = clWhite;
Panel2->Caption = " Button2";
Panel2->Color = clPurple;
}
//---------------------------------------------------
void __fastcall TForm1::Panel2MouseMove( TObject *Sender,
TShiftState Shift,
int X, int Y )
{
//
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
//
delete Panel1;
Panel1 = NULL;
delete Panel2;
Panel2 = NULL;
}
//---------------------------------------------------------------------------
#ifndef ColouredButtonsH
#define ColouredButtonsH
//---------------------------------------------------------------------------
#include Classes.hpp>
#include Controls.hpp>
#include StdCtrls.hpp>
#include Forms.hpp>
#include Buttons.hpp>
#include ExtCtrls.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
//PANEL1
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 Panel1MouseMove(TObject *Sender, TShiftState Shift,
int X, int Y);
//------------------------------------------
//PANEL2
void __fastcall Panel2Click(TObject *Sender);
void __fastcall Panel2MouseUp(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y);
void __fastcall Panel2MouseDown(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y);
void __fastcall Panel2MouseMove( TObject *Sender,
TShiftState Shift,
int X, int Y );
void __fastcall FormClose(TObject *Sender, TCloseAction &Action);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
Description
The three-dimensional effect of the button is created by initializing
the panel properties BevelInner and BevelOuter to: bvRaised.
When the button is pressed (OnMouseDown event) these properties are
set to bvLowered. When the button is not pressed (OnMouseUp
event) both properties are set to bvRaised.
Button3 is a "real TButton" (there is no panel on it). This button is used to compare the behaviour of the simulated buttons
with the real one.
//---------------------------------------------------------------------------
#include vcl.h>
#pragma hdrstop
#include "ColouredButtons.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1* Form1;
TButton* Button1;
TBitBtn* BitBtn1;
TButton* Button3;
TPanel* Panel1;
TPanel* Panel2;
TTimer* Timer1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
Color = clBlack; //Form Color
Position = poScreenCenter;
Width = 600;
Height = 400;
//Button1
Button1 = new TButton( this );
Button1->Parent = this;
Button1->Top = 60;
Button1->Left = 30;
Button1->Height = 100;
Button1->Width = 130;
Button1->OnClick = Button1Click;
Button1->OnKeyDown = Button1KeyDown;
Button1->OnKeyPress = Button1KeyPress;
Button1->OnKeyUp = Button1KeyUp;
//BitBtn1
BitBtn1 = new TBitBtn( this );
BitBtn1->Parent = this;
BitBtn1->Top = Button1->Top +
Button1->Height + 40;
BitBtn1->Left = Button1->Left;
BitBtn1->Height = Button1->Height;
BitBtn1->Width = Button1->Width;
BitBtn1->OnClick = BitBtn1Click;
BitBtn1->OnKeyDown = BitBtn1KeyDown;
BitBtn1->OnKeyPress = BitBtn1KeyPress;
BitBtn1->OnKeyUp = BitBtn1KeyUp;
//Button3
//This Button will have no panel on top
//so this will be a "Real Button"
Button3 = new TButton( this );
Button3->Parent = this;
Button3->Top = 60;
Button3->Left = Button1->Left + 400;
Button3->Height = 100;
Button3->Width = 130;
Button3->Caption = "Button3 (Real)";
Button3->OnClick = Button3Click;
Button3->OnKeyDown = Button3KeyDown;
Button3->OnKeyPress = Button3KeyPress;
Button3->OnKeyUp = Button3KeyUp;
//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;
Panel1->Width = Button1->Width;
Panel1->Color = clPurple;
Panel1->Font->Color = clWhite;
Panel1->Caption = " Button1";
Panel1->OnClick = Panel1Click;
Panel1->OnMouseDown = Panel1MouseDown;
Panel1->OnMouseUp = Panel1MouseUp;
Panel1->OnMouseMove = Panel1MouseMove;
//-------------------------------
Panel1->BevelInner = bvRaised;
Panel1->BevelOuter = bvRaised;
//-------------------------------
//Panel2
//BitBut1 is the Owner of Panel2 so that
//BitBut1 will free the memory allocated to Panel2.
//BitBut1 is the Parent of Panel2 so that
//Panel2 is displayed on BitBut1
Panel2 = new TPanel( BitBtn1 );
Panel2->Parent = BitBtn1;
Panel2->Top = 0;
Panel2->Left = 0;
Panel2->Height = BitBtn1->Height;
Panel2->Width = BitBtn1->Width;
Panel2->Color = clPurple;
Panel2->Font->Color = clWhite;
Panel2->Caption = " BitBtn1";
Panel2->OnClick = Panel2Click;
Panel2->OnMouseDown = Panel2MouseDown;
Panel2->OnMouseUp = Panel2MouseUp;
Panel2->OnMouseMove = Panel2MouseMove;
//-------------------------------
Panel2->BevelInner = bvRaised;
Panel2->BevelOuter = bvRaised;
//-------------------------------
Timer1 = new TTimer( this );
Timer1->Interval = 100;
Timer1->Enabled = true;
Timer1->OnTimer = Timer1Timer;
}
//---------------------------------------------------------------------------
//-------------------
//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( Button1->ControlCount ) );
}
//------------------------------------------------------
void __fastcall TForm1::Panel1Click( TObject *Sender )
{
//
//ShowMessage("Panel1Click");
Button1Click( Sender );
}
//------------------------------------------------------
void __fastcall TForm1::Panel1MouseDown( TObject *Sender,
TMouseButton Button,
TShiftState Shift,
int X, int Y)
{
//
Panel1->BevelInner = bvLowered;
Panel1->BevelOuter = bvLowered;
Panel1->Font->Color = clBlack;
Panel1->Caption = "Button1";
Panel1->Color = clRed;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Panel1MouseUp( TObject *Sender,
TMouseButton Button,
TShiftState Shift,
int X, int Y)
{
//
Panel1->BevelInner = bvRaised;
Panel1->BevelOuter = bvRaised;
Panel1->Font->Color = clWhite;
Panel1->Caption = " Button1";
Panel1->Color = clPurple;
}
//------------------------------------------------------
void __fastcall TForm1::Panel1MouseMove( TObject *Sender,
TShiftState Shift,
int X, int Y )
{
//
Button1MouseMove( Sender, Shift, X, Y);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1MouseMove( TObject *Sender,
TShiftState Shift,
int X, int Y)
{
//
//ShowMessage( "Button1MouseMove()\n"
// + String("X = ") + String(X) + " Y = " + String(Y) );
}
//------------------------------------------------------
//-------------------
//PANEL2 - BitBtn1
//-------------------
void __fastcall TForm1::BitBtn1Click(TObject *Sender)
{
ShowMessage("BitBtn1Click():\nthis->ControlCount = "
+ String( this->ControlCount ) + "\n"
+ "this->ComponentCount = "
+ String( this->ComponentCount ) + "\n"
+"BitBtn1->ControlCount = "
+ String( BitBtn1->ControlCount ) );
}
//-------------------------------------------------------
void __fastcall TForm1::Panel2Click(TObject *Sender)
{
//
BitBtn1Click( Sender );
}
//------------------------------------------------------
void __fastcall TForm1::Panel2MouseDown( TObject *Sender,
TMouseButton Button,
TShiftState Shift,
int X, int Y)
{
//
Timer1->Enabled = false;
Panel2->BevelInner = bvLowered;
Panel2->BevelOuter = bvLowered;
Panel2->Font->Color = clBlack;
Panel2->Caption = "BitBtn1";
Panel2->Color = clRed;
}
//---------------------------------------------------------------
void __fastcall TForm1::Panel2MouseUp( TObject *Sender,
TMouseButton Button,
TShiftState Shift,
int X, int Y)
{
//
Panel2->BevelInner = bvRaised;
Panel2->BevelOuter = bvRaised;
Panel2->Font->Color = clWhite;
Panel2->Caption = " BitBtn1";
Panel2->Color = clPurple;
}
//---------------------------------------------------
void __fastcall TForm1::Panel2MouseMove( TObject *Sender,
TShiftState Shift,
int X, int Y )
{
//
BitBtn1MouseMove( Sender, Shift, X, Y );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn1MouseMove( TObject *Sender,
TShiftState Shift,
int X, int Y)
{
//
//ShowMessage( "BitBtn1MouseMove()\n"
// + String("X = ") + String(X) + " Y = " + String(Y) );
}
//------------------------------------------------------
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
//
delete Button1;
Button1 = NULL;
delete BitBtn1;
BitBtn1 = NULL;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1KeyDown(TObject *Sender, WORD &Key,
TShiftState Shift)
{
//
Panel1->BevelInner = bvLowered;
Panel1->BevelOuter = bvLowered;
Panel1->Font->Color = clBlack;
Panel1->Caption = "Button1";
Panel1->Color = clRed;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1KeyPress(TObject *Sender, char &Key)
{
//
Panel1->BevelInner = bvLowered;
Panel1->BevelOuter = bvLowered;
Panel1->Font->Color = clBlack;
Panel1->Caption = "Button1";
Panel1->Color = clRed;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1KeyUp(TObject *Sender, WORD &Key,
TShiftState Shift)
{
//
Panel1->BevelInner = bvRaised;
Panel1->BevelOuter = bvRaised;
Panel1->Font->Color = clWhite;
Panel1->Caption = " Button1";
Panel1->Color = clPurple;
}
//-------------------------------------------------------
void __fastcall TForm1::BitBtn1KeyDown(TObject *Sender, WORD &Key,
TShiftState Shift)
{
//
Timer1->Enabled = false;
Panel2->BevelInner = bvLowered;
Panel2->BevelOuter = bvLowered;
Panel2->Font->Color = clBlack;
Panel2->Caption = "BitBtn1";
Panel2->Color = clRed;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn1KeyPress(TObject *Sender, char &Key)
{
//
Timer1->Enabled = false;
Panel2->BevelInner = bvLowered;
Panel2->BevelOuter = bvLowered;
Panel2->Font->Color = clBlack;
Panel2->Caption = "BitBtn1";
Panel2->Color = clRed;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn1KeyUp(TObject *Sender, WORD &Key,
TShiftState Shift)
{
//
Timer1->Enabled = true;
Panel2->BevelInner = bvRaised;
Panel2->BevelOuter = bvRaised;
Panel2->Font->Color = clWhite;
Panel2->Caption = " BitBtn1";
Panel2->Color = clPurple;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
// Timer to refresh Panels whose Parent is a
// BitBtn Button.
// This timer is not necessary for Panels whose Parent
// is a TButton.
for( int i=0; i < this->ControlCount; i++ )
{
TWinControl* pControl = dynamic_cast <TWinControl*>(this->Controls[i]);
if ( pControl )
{
if ( Controls[i]->ClassNameIs( "TBitBtn" ) )
{
TBitBtn* p = (TBitBtn*)pControl;
if ( p->Focused() )
{
TWinControl* pChild = dynamic_cast <TWinControl*>
( p->Controls[0] );
TPanel* pPanel = (TPanel*)pChild;
pPanel->BevelInner = bvRaised;
pPanel->BevelOuter = bvRaised;
pPanel->Font->Color = clWhite;
pPanel->Caption = " BitBtn1";
pPanel->Color = clPurple;
}
}
}
}
}
//---------------------------------------------------------------------------
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 ) );
}
#ifndef ColouredButtonsH
#define ColouredButtonsH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <Buttons.hpp>
#include <ExtCtrls.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
//Panel1 - Button1
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);
//------------------------------------------
//Panel2 - BitBtn1
void __fastcall BitBtn1Click(TObject *Sender);
void __fastcall Panel2Click(TObject *Sender);
void __fastcall Panel2MouseUp(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y);
void __fastcall Panel2MouseDown(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y);
void __fastcall BitBtn1MouseMove( TObject *Sender,
TShiftState Shift,
int X, int Y);
void __fastcall Panel2MouseMove( TObject *Sender,
TShiftState Shift,
int X, int Y );
void __fastcall FormClose(TObject *Sender, TCloseAction &Action);
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 BitBtn1KeyDown(TObject *Sender, WORD &Key,
TShiftState Shift);
void __fastcall BitBtn1KeyPress(TObject *Sender, char &Key);
void __fastcall BitBtn1KeyUp(TObject *Sender, WORD &Key,
TShiftState Shift);
void __fastcall Timer1Timer(TObject *Sender);
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
TPanel* Panels[10];
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif