//--------------------------------------------------------------------------- #include #pragma hdrstop #include "TPStretchImage.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(TPStretchImage *) { new TPStretchImage(NULL); } //--------------------------------------------------------------------------- __fastcall TPStretchImage::TPStretchImage(TComponent* Owner) : TGraphicControl(Owner) { ControlStyle = ControlStyle << csReplicatable << csOpaque; Height = 105; Width = 105; FBitmap = new TTexpiaBitmap; FInsideBitmap = true; FZoomIn = FZoomOut = 1; } //--------------------------------------------------------------------------- __fastcall TPStretchImage::~TPStretchImage() { if (FInsideBitmap) delete FBitmap; } //--------------------------------------------------------------------------- // Private Function //--------------------------------------------------------------------------- TCanvas *__fastcall TPStretchImage::GetCanvas() { return TGraphicControl::Canvas; } //--------------------------------------------------------------------------- void __fastcall TPStretchImage::SetBitmap(TTexpiaBitmap *Value) { if (FBitmap!=Value) { if (Value) { if (FInsideBitmap) delete FBitmap; FBitmap = Value; FInsideBitmap = false; } else if (!FInsideBitmap) { FBitmap = new TTexpiaBitmap; FInsideBitmap = true; } Paint(); Invalidate(); } } //--------------------------------------------------------------------------- void __fastcall TPStretchImage::SetZoom(int zi, int zo) { if (FZoomIn != zi || FZoomOut != zo) { FZoomIn = zi; FZoomOut = zo; Paint(); Invalidate(); } } //--------------------------------------------------------------------------- // Protected Function //--------------------------------------------------------------------------- void __fastcall TPStretchImage::Paint() { BITMAPHANDLE *bh=NULL; HPALETTE hpaintpal = NULL, holdpal = NULL; HDC dc=NULL; RECT Src, Dst; if (ComponentState.Contains(csDesigning)) { TGraphicControl::Canvas->Pen->Style = psDash; TGraphicControl::Canvas->Brush->Style = bsClear; TGraphicControl::Canvas->Rectangle(0, 0, Width, Height); } else { if (Width<=0 || Height<=0) return; if (FOnPaint) { FOnPaint(TGraphicControl::Canvas->Handle, FZoomIn, FZoomOut); if (FOnPaintEnd) FOnPaintEnd(this); } else { if (FBitmap->Width<=0 || FBitmap->Height<=0) { TGraphicControl::Canvas->Brush->Color = clBtnFace; TGraphicControl::Canvas->FillRect(Rect(0, 0, Width, Height)); } else { bh = FBitmap->Handle; if (bh) { dc = TGraphicControl::Canvas->Handle; if (FBitmap->BitsPerPixel==8) { hpaintpal = L_CreatePaintPalette(dc, bh); if (hpaintpal) holdpal = SelectPalette(dc, hpaintpal, false); } Src.left = 0; Src.top = 0; Src.right = bh->Width; Src.bottom = bh->Height; Dst.left = 0; Dst.top = 0; Dst.right = Width; Dst.bottom = Height; L_PaintDC(dc, bh, &Src, NULL, &Dst, NULL, SRCCOPY); if (hpaintpal) { if (holdpal) SelectPalette(dc, holdpal, false); DeleteObject(hpaintpal); } if (FOnPaintEnd) FOnPaintEnd(this); } } } } } //--------------------------------------------------------------------------- namespace Tpstretchimage { void __fastcall PACKAGE Register() { TComponentClass classes[1] = {__classid(TPStretchImage)}; RegisterComponents("Texpia", classes, 0); } } //---------------------------------------------------------------------------