Files
2026-06-13 00:05:19 +08:00

206 lines
5.4 KiB
C++

//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "PNGButton.h"
//#pragma package(smart_init)
//---------------------------------------------------------------------------
// ValidCtrCheck is used to assure that the components created do not have
// any pure virtual functions.
//
//static inline void ValidCtrCheck(TPNGButton *)
//{
// new TPNGButton(NULL);
//}
////---------------------------------------------------------------------------
__fastcall CBTPNButton::CBTPNButton(TComponent* Owner)
: TGraphicControl(Owner)
{
Height = 25; //a default Height
Width = 75; //a default Width
FStateCount = 4; //default state count
FPngImg = new Vcl::Imaging::Pngimage::TPngImage(); //we create our png
FDown = false;
}
//---------------------------------------------------------------------------
__fastcall CBTPNButton::~CBTPNButton(void)
{
if(FPngImg)
delete FPngImg; //destroy our png
}
//---------------------------------------------------------------------------
void __fastcall CBTPNButton::SetStateCount( int count )
{
if( count>4 ) count = 4;
if( count<2 ) count = 2;
FStateCount = count;
}
//---------------------------------------------------------------------------
void __fastcall CBTPNButton::SetPngImg(Vcl::Imaging::Pngimage::TPngImage *value)
{
FPngImg->Assign(value);
//Now setting the component width and height by the png image used
Height = FPngImg->Height;
Width = FPngImg->Width / FStateCount;
Constraints->MaxHeight = Height;
Constraints->MinHeight = Height;
Constraints->MaxWidth = Width;
Constraints->MinWidth = Width;
EtatBtn = 0; //reset the component state to normal
}
//---------------------------------------------------------------------------
void __fastcall CBTPNButton::SetDown(bool value)
{
if( FStateCount<3 )
value = false;
FDown = value;
if( FDown==False )
EtatBtn = 0; //is the mouse down is false then we reset the component state to normal
Paint();
// Refresh(); //we repaint the componet)
}
//---------------------------------------------------------------------------
void __fastcall CBTPNButton::SetColor( TColor value )
{
FBkColor = value;
}
//---------------------------------------------------------------------------
void __fastcall CBTPNButton::Paint(void)
{
TRect rect = GetClientRect();
Canvas->Brush->Color = FBkColor;
Canvas->FillRect( rect );
if( FPngImg ){
switch( EtatBtn ){
case 0:
FPngImg->Draw( Canvas, Rect(0, 0, FStateCount*Width, Height) ); // Normal
break;
case 1:
FPngImg->Draw( Canvas, Rect(-Width, 0, (FStateCount-1)*Width, Height) ); // Mouse Entered
break;
case 2:
FPngImg->Draw( Canvas, Rect(-(Width*2), 0, (FStateCount-2)*Width, Height) ); // Pressed
break;
case 3:
FPngImg->Draw( Canvas, Rect(-(Width*3), 0, (FStateCount-3)*Width, Height) ); // Disabled
break;
}
}
}
//---------------------------------------------------------------------------
void __fastcall CBTPNButton::SetEnabled(bool Value)
{
if(FStateCount<4) //can't be disable
Value = true;
TGraphicControl::SetEnabled(Value);
if( Enabled )
EtatBtn = 0;
else
EtatBtn = 3;
Paint();
}
//---------------------------------------------------------------------------
void __fastcall CBTPNButton::MouseDown(System::Uitypes::TMouseButton Button,
System::Classes::TShiftState Shift, int X, int Y)
{
TGraphicControl::MouseDown( Button, Shift, X, Y );
if( Button==mbLeft && Enabled && FStateCount>2){
EtatBtn = 2; // Pressed
Paint();
// Refresh();
}
}
//---------------------------------------------------------------------------
void __fastcall CBTPNButton::MouseUp(System::Uitypes::TMouseButton Button,
System::Classes::TShiftState Shift, int X, int Y)
{
TGraphicControl::MouseUp( Button, Shift, X, Y );
if( Button==mbLeft && Enabled ){
if( X>=0 && X<Width && Y>=0 && Y<Height )
EtatBtn = 1; //Mouse Entered
else
EtatBtn = 0; //normal;
Paint();
// Refresh(); //repaint component
}
}
//---------------------------------------------------------------------------
void __fastcall CBTPNButton::CMMouseEnter(Winapi::Messages::TMessage &Msg)
{
if( Enabled ){//&& !ComponentState.Contains(csDesigning) ){
EtatBtn = 1; // Mouse Entered
Paint();
// Refresh(); //repainting the component
}
if( FOnMouseEnter )
FOnMouseEnter(this); //if user set a mouse enter procedure then we execute it
}
//---------------------------------------------------------------------------
void __fastcall CBTPNButton::CMMouseLeave(Winapi::Messages::TMessage &Msg)
{
if( Enabled ){//&& !ComponentState.Contains(csDesigning) ) {
EtatBtn = 0; //Normal
Paint();
// Refresh(); //repainting the component
}
if( FOnMouseLeave )
FOnMouseLeave(this); //if user set a mouse leave procedure then we execute it
}
//---------------------------------------------------------------------------
void __fastcall CBTPNButton::Click(void)
{
TCustomForm * parentForm = GetParentForm( this );
if( parentForm ){
parentForm->ModalResult = FModalResult;
parentForm->Close();
}
TGraphicControl::Click();
}
//---------------------------------------------------------------------------
//namespace Pngbutton
//{
// void __fastcall PACKAGE Register()
// {
// TComponentClass classes[1] = {__classid(TPNGButton)};
// RegisterComponents(L"Samples", classes, 0);
// }
//}
////---------------------------------------------------------------------------