Files
JdSoft/ActImg/Copia de actimg.cpp
2021-09-12 22:11:31 +02:00

151 lines
4.6 KiB
C++

//---------------------------------------------------------------------------
#include <vcl\vcl.h>
#pragma hdrstop
#include "actimg.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------
// ValidCtrCheck is used to assure that the components created do not have
// any pure virtual functions.
//
static inline TActiveImage *ValidCtrCheck()
{
return new TActiveImage(NULL);
}
//---------------------------------------------------------------------------
__fastcall TActiveImage::TActiveImage(TComponent* Owner)
// : TGraphicControl(Owner)
: TButton(Owner)
{
mcaptured = false;
FGlyph = new Graphics::TCanvas;
FGlyph->Brush->Color = clNone;
FGlyph->Brush->Style = bsClear;
FGlyph->CopyMode = cmSrcCopy;
GlyphNormal = new Graphics::TPicture;
GlyphOver = new Graphics::TPicture;
GlyphPress = new Graphics::TPicture;
vAutoSize = false;
}
//---------------------------------------------------------------------------
namespace Actimg
{
void __fastcall PACKAGE Register()
{
TComponentClass classes[1] = {__classid(TActiveImage)};
RegisterComponents("JD Soft.", classes, 0);
}
}
//---------------------------------------------------------------------------
void __fastcall TActiveImage::CreateParams(TCreateParams &Params)
{
TButton::CreateParams(Params); // La clase ascendente se ocupará de establecer los parámetros generales
// Params.ExStyle = Params.ExStyle | WS_EX_TRANSPARENT;
Params.Style = Params.Style | BS_OWNERDRAW;
TButton::ControlStyle = TControlStyle();
}
//---------------------------------------------------------------------------
void __fastcall TActiveImage::CNMeasureItem(TWMMeasureItem& Mensaje)
{
Mensaje.MeasureItemStruct->itemWidth = Width;
Mensaje.MeasureItemStruct->itemHeight = Height;
}
//---------------------------------------------------------------------------
// Cada vez que haya que dibujar el control se llamará a este método
void __fastcall TActiveImage::CNDrawItem(TWMDrawItem& Mensaje)
{
Graphics::TBitmap *Bitmap;
Bitmap = GlyphNormal->Bitmap;
if (Mensaje.DrawItemStruct->itemState & ODS_SELECTED) // El botón está pulsado
Bitmap = GlyphPress->Bitmap;
else
// Si el botón está desactivado
if (! Enabled )
Bitmap = GlyphPress->Bitmap;
else
if ( mcaptured )
Bitmap = GlyphOver->Bitmap;
FGlyph->Handle = Mensaje.DrawItemStruct->hDC;
FGlyph->BrushCopy( ClientRect, Bitmap, Rect( 0, 0, Bitmap->Width-1, Bitmap->Height-1 ), Bitmap->TransparentColor );
// El Handle de FCanvas será el Hdc enviado en Mensaje.DrawItemStruct,
// lo que nos permitirá dibujar en la superficie del botón
FGlyph->Handle = 0;
}
//---------------------------------------------------------------------------
// Al cambiar el estado del botón
void __fastcall TActiveImage::SetButtonStyle(bool ADefault)
{
Refresh(); // simplemente redibujar
}
//---------------------------------------------------------------------------
__fastcall TActiveImage::~TActiveImage()
{
delete FGlyph;
delete GlyphNormal;
delete GlyphOver;
delete GlyphPress;
}
//---------------------------------------------------------------------------
void __fastcall TActiveImage::MouseMove( Classes::TShiftState Shift, int X, int Y )
{
if( ! mcaptured )
{
SetCapture( Handle );
mcaptured = true;
Refresh();
}
if( (X<0) || (Y<0) || (X>Width) || (Y>Height))
{
ReleaseCapture();
mcaptured = false;
Refresh();
}else{
TButton::MouseMove( Shift, X, Y );
}
}
//---------------------------------------------------------------------------
void __fastcall TActiveImage::Click( void )
{
TButton::Click();
ReleaseCapture();
mcaptured = false;
}
//---------------------------------------------------------------------------
void __fastcall TActiveImage::SetGlyphOver( Graphics::TPicture *val )
{
GlyphOver -> Assign( val );
}
//---------------------------------------------------------------------------
void __fastcall TActiveImage::SetGlyphPress( Graphics::TPicture *val )
{
GlyphPress -> Assign( val );
}
//---------------------------------------------------------------------------
void __fastcall TActiveImage::SetGlyphNormal( Graphics::TPicture *val )
{
GlyphNormal -> Assign( val );
}
//---------------------------------------------------------------------------
void __fastcall TActiveImage::SetAutoSize( bool val )
{
vAutoSize = val;
// Facilitamos la anchura y altura del control
if ( vAutoSize && ! GlyphNormal->Graphic->Empty )
{
Width = GlyphNormal->Width;
Height = GlyphNormal->Height;
}
}
//---------------------------------------------------------------------------