//--------------------------------------------------------------------------- #include #pragma hdrstop #include "TPImage.h" #pragma package(smart_init) #pragma resource "TPImage.res" //--------------------------------------------------------------------------- int TPImage::FInstance = 0; HBITMAP TPImage::bmFrameRgn[8]; //--------------------------------------------------------------------------- __fastcall TPImage::TPImage(TComponent* Owner) : TGraphicControl(Owner) { ControlStyle = ControlStyle << csReplicatable << csOpaque; Height = 105; Width = 105; FBitmap = NULL; FColor = clBtnFace; FCoordinationTransfer = false; FGrid = false; FCross = false; FCrossLine = false; FGridColor = clBlack; FGridGapX = FGridGapY = 1.0; FGridMark = 0; FGridMarkColor = clBlack; // FInsideBitmap = true; FMarginY = 0; FMask = NULL; FMaskEnabled = false; FParentImage = NULL; FZoomIn = FZoomOut = 1; FSubBitmap = FSubMask = FBackGround = NULL; FSubEnabled = FSubVisible = FSubMethod = false; FSubRange.left = 0; FSubRange.right = 0; FSubRange.top = 0; FSubRange.bottom = 0; if (!FInstance) { for (int i=0; i<8; i++) { bmFrameRgn[i] = LoadBitmap(HInstance, Format("FRAMERGN%d", OPENARRAY(TVarRec, (i))).c_str()); } } nFrameRgn = 0; FInstance++; } //--------------------------------------------------------------------------- __fastcall TPImage::~TPImage() { FInstance--; if (!FInstance) { for (int i=0; i<8; i++) { if (bmFrameRgn[i]) DeleteObject(bmFrameRgn[i]); } } if (FMask) delete FMask; if (FSubBitmap) delete FSubBitmap; if (FSubMask) delete FSubMask; if (FBackGround) delete FBackGround; } //--------------------------------------------------------------------------- // Private Methods //--------------------------------------------------------------------------- void __fastcall TPImage::CMMouseEnter(TMessage &msg) { if (FOnMouseEnter) FOnMouseEnter(this); } //--------------------------------------------------------------------------- void __fastcall TPImage::CMMouseLeave(TMessage &msg) { if (FOnMouseLeave) FOnMouseLeave(this); } //--------------------------------------------------------------------------- TCanvas *__fastcall TPImage::GetCanvas() { return TGraphicControl::Canvas; } //--------------------------------------------------------------------------- int __fastcall TPImage::GetZoomIn() { if (FParentImage) FZoomIn = FParentImage->ZoomIn; return FZoomIn; } //--------------------------------------------------------------------------- int __fastcall TPImage::GetZoomOut() { if (FParentImage) FZoomOut = FParentImage->ZoomOut; return FZoomOut; } /*--------------------------------------------------------------------------- void __fastcall TPImage::SetBitmap(TTexpiaBitmap *Value) { if (FBitmap!=Value) { if (Value) { if (FInsideBitmap) { delete FBitmap; FBitmap = NULL; } FBitmap = Value; FBitmap->OnPropertyChange = PropertyChange; FInsideBitmap = false; } else if (!FInsideBitmap) { FBitmap = new TTexpiaBitmap; FBitmap->OnPropertyChange = PropertyChange; FInsideBitmap = true; } Paint(); Invalidate(); } } */ //--------------------------------------------------------------------------- void __fastcall TPImage::SetGrid(bool Value) { if (FParentImage) return; if (FGrid!=Value) { FGrid = Value; Paint(); Invalidate(); } } //--------------------------------------------------------------------------- void __fastcall TPImage::SetGridColor(TColor Value) { if (FParentImage) return; if (FGridColor!=Value) { FGridColor = Value; Paint(); Invalidate(); } } //--------------------------------------------------------------------------- void __fastcall TPImage::SetGridGapX(double Value) { if (FParentImage) return; if (FGridGapX!=Value) { FGridGapX = Value; Paint(); Invalidate(); } } //--------------------------------------------------------------------------- void __fastcall TPImage::SetGridGapY(double Value) { if (FParentImage) return; if (FGridGapY!=Value) { FGridGapY = Value; Paint(); Invalidate(); } } //--------------------------------------------------------------------------- void __fastcall TPImage::SetGridMark(int Value) { if (FParentImage) return; if (FGridMark!=Value) { FGridMark = Value; Paint(); Invalidate(); } } //--------------------------------------------------------------------------- void __fastcall TPImage::SetGridMarkColor(TColor Value) { if (FParentImage) return; if (FGridMarkColor!=Value) { FGridMarkColor = Value; Paint(); Invalidate(); } } //--------------------------------------------------------------------------- void __fastcall TPImage::SetMask(TTexpiaBitmap *Value) { if (FMask!=Value) { ::doDestroy(FMask); if (Value) FMask = Value; else FMaskEnabled = false; Paint(); Invalidate(); } } //--------------------------------------------------------------------------- void __fastcall TPImage::SetMaskEnabled(bool Value) { if (FMaskEnabled!=Value) { FMaskEnabled = FMask ? Value : false; Paint(); Invalidate(); } } //--------------------------------------------------------------------------- void __fastcall TPImage::SetSubEnabled(bool Value) { if (FSubEnabled != Value) { FSubEnabled = Value; if (FSubBitmap) { delete FSubBitmap; FSubBitmap = NULL; } if (FSubMask) { delete FSubMask; FSubMask = NULL; } if (FSubEnabled) { FSubBitmap = new TTexpiaBitmap; FSubMask = new TTexpiaBitmap; } } } //--------------------------------------------------------------------------- void __fastcall TPImage::SetSubVisible(bool Value) { if (FSubVisible != Value) { FSubVisible = Value; Invalidate(); } } //--------------------------------------------------------------------------- // Protected Methods //--------------------------------------------------------------------------- void __fastcall TPImage::SetPositionX(int Value) { int v; if (FParentImage) { FZoomIn = FParentImage->ZoomIn; FZoomOut = FParentImage->ZoomOut; } else { v = Width*FZoomOut/FZoomIn; if (v>FBitmap->Width) v = FBitmap->Width; v = FBitmap->Width-v; if (Value<0) Value = 0; else if (Value>v) Value = v; } if (FZoomOut>1) Value -= Value%FZoomOut; if (FPositionX != Value) { FPositionX = Value; if (FOnPositionChange) FOnPositionChange(this); Paint(); Invalidate(); } } //--------------------------------------------------------------------------- void __fastcall TPImage::SetPositionY(int Value) { int v; if (FParentImage) { FZoomIn = FParentImage->ZoomIn; FZoomOut = FParentImage->ZoomOut; } else { v = Height*FZoomOut/FZoomIn; if (v>FBitmap->Height) v = FBitmap->Height; v = FBitmap->Height-v; if (Value<0) Value = 0; else if (Value>v) Value = v; } if (FZoomOut>1) Value -= Value%FZoomOut; if (FPositionY != Value) { FPositionY = Value; PositionChange(); if (FOnPositionChange) FOnPositionChange(this); Paint(); Invalidate(); } else { if (PositionChange()) { if (FOnPositionChange) FOnPositionChange(this); Paint(); Invalidate(); } } } //--------------------------------------------------------------------------- void __fastcall TPImage::SetCrossLine(bool Value) { if (FCrossLine != Value) { FCrossLine = Value; if (FCrossLine == false) Invalidate(); } } //--------------------------------------------------------------------------- // Public Methods //--------------------------------------------------------------------- POINT __fastcall TPImage::GetCrossPos(void){ return FCrossPos; } //--------------------------------------------------------------------- bool __fastcall TPImage::LoadFromFile(AnsiString fn) { RGBQUAD rgb[256]; FBitmap->LoadFromFile(fn); if (FBitmap->BitsPerPixel==8) { FBitmap->GetColors(0, 256, rgb); SetDIBColorTable(TGraphicControl::Canvas->Handle, 0, 256, rgb); } if (PositionChange()) { if (FOnPositionChange) FOnPositionChange(this); Invalidate(); } return true; } //--------------------------------------------------------------------------- void __fastcall TPImage::SetPosition() { int ps; ps = Width*FZoomOut/FZoomIn; if (ps>FBitmap->Width) { if (FPositionX>0) FPositionX = 0; } else { if (FPositionX>FBitmap->Width-ps) FPositionX = FBitmap->Width-ps; } ps = Height*FZoomOut/FZoomIn; if (ps>FBitmap->Height) { if (FPositionY>0) FPositionY = 0; } else { if (FPositionY>FBitmap->Height-ps) FPositionY = FBitmap->Height-ps; } if (PositionChange()) { if (FOnPositionChange) FOnPositionChange(this); Invalidate(); } } //--------------------------------------------------------------------------- void __fastcall TPImage::SetPosition(int xValue, int yValue) { int v; if (FParentImage) { FZoomIn = FParentImage->ZoomIn; FZoomOut = FParentImage->ZoomOut; } else { v = Width*FZoomOut/FZoomIn; if (v>FBitmap->Width) v = FBitmap->Width; v = FBitmap->Width-v; if (xValue<0) xValue = 0; else if (xValue>v) xValue = v; v = Height*FZoomOut/FZoomIn; if (v>FBitmap->Height) v = FBitmap->Height; v = FBitmap->Height-v; if (yValue<0) yValue = 0; else if (yValue>v) yValue = v; } if (FZoomOut>1) { xValue -= xValue%FZoomOut; yValue -= yValue%FZoomOut; } if (FPositionY != yValue) { FPositionY = yValue; if (FPositionX != xValue) FPositionX = xValue; PositionChange(); if (FOnPositionChange) FOnPositionChange(this); Paint(); Invalidate(); } else { if (FPositionX != xValue) { FPositionX = xValue; if (FOnPositionChange) FOnPositionChange(this); Paint(); Invalidate(); } else { if (PositionChange()) { if (FOnPositionChange) FOnPositionChange(this); Paint(); Invalidate(); } } } } //--------------------------------------------------------------------------- void __fastcall TPImage::SetZoom(int InValue, int OutValue, POINT *ptPosition) { SIZE ps; int ri, ro; if (FParentImage) return; if (InValue!=FZoomIn || OutValue!=FZoomOut) { if (ptPosition) { ps.cx = ptPosition->x*FZoomOut/FZoomIn; ps.cy = ptPosition->y*FZoomOut/FZoomIn; } else { ps.cx = (Width>>1)*FZoomOut/FZoomIn; ps.cy = (Height>>1)*FZoomOut/FZoomIn; } if (ps.cx>FBitmap->Width) ps.cx = FBitmap->Width; if (ps.cy>FBitmap->Height) ps.cy = FBitmap->Height; ri = FZoomIn*OutValue; ro = FZoomOut*InValue; if (riFBitmap->Width-ps.cx) FPositionX = FBitmap->Width-ps.cx; if (FPositionX<0) FPositionX = 0; ps.cy = Height*FZoomOut/FZoomIn; if (FPositionY>FBitmap->Height-ps.cy) FPositionY = FBitmap->Height-ps.cy; if (FPositionY<0) FPositionY = 0; if (FZoomOut>1) { FPositionX -= FPositionX%FZoomOut; FPositionY -= FPositionY%FZoomOut; } if (PositionChange()) { if (FOnPositionChange) FOnPositionChange(this); } Paint(); Invalidate(); } } //---------------------------------------------------------------------------