//--------------------------------------------------------------------------- #include #pragma hdrstop #include "TPSelectImage.h" #pragma link "TPImage" #pragma package(smart_init) //--------------------------------------------------------------------------- // ValidCtrCheck is used to assure that the components created do not have // any pure virtual functions. // static inline void ValidCtrCheck(TPSelectImage *) { new TPSelectImage(NULL); } //--------------------------------------------------------------------------- __fastcall TPSelectImage::TPSelectImage(TComponent* Owner) : TPImage(Owner) { FBitmap = new TTexpiaBitmap; FBitmap->OnPropertyChange = PropertyChange; FColorEnable = false; FRealPosY = FPositionY; } //--------------------------------------------------------------------------- __fastcall TPSelectImage::~TPSelectImage() { if (FBitmap) delete FBitmap; } //--------------------------------------------------------------------------- // Private Function //--------------------------------------------------------------------------- bool __fastcall TPSelectImage::PositionChange() { int m = FMarginY, r = FRealPosY; if (FBitmap->Handle) { int ps = Height*FZoomOut/FZoomIn; if (FBitmap->CoordinateSystem==csBottomLeft) { if (ps>FBitmap->Height) { m = Height-FBitmap->Height*FZoomIn/FZoomOut; r = 0; } else { m = FZoomIn>1 ? Height-ps*FZoomIn/FZoomOut : 0; r = FBitmap->Height-FPositionY-ps; } } else { m = 0; r = FPositionY; } if (m != FMarginY || r != FRealPosY) { FMarginY = m; FRealPosY = r; return true; } } return false; } //--------------------------------------------------------------------------- #define PAINT_INIT \ sx = FPositionX; \ sy = FRealPosY; \ dcDst = TGraphicControl::Canvas->Handle; \ if (FBitmap->BitsPerPixel==8) { \ hpaintpal = L_CreatePaintPalette(dcDst, bh); \ if (hpaintpal) holdpal = SelectPalette(dcDst, hpaintpal, false); \ } \ if (FParentImage) { \ FGrid = FParentImage->Grid; \ FZoomIn = FParentImage->ZoomIn; \ FZoomOut = FParentImage->ZoomOut; \ } \ pw = Width*FZoomOut/FZoomIn; \ ph = Height*FZoomOut/FZoomIn; \ if (pw>FBitmap->Width-sx) pw = FBitmap->Width-sx; \ if (ph>FBitmap->Height-sy) ph = FBitmap->Height-sy; \ if (FZoomOut>1) { \ pw -= pw%FZoomOut; \ ph -= ph%FZoomOut; \ } \ vw = pw*FZoomIn/FZoomOut; \ vh = ph*FZoomIn/FZoomOut; //--------------------------------------------------------------------------- #define PAINT_GRIDLINE_TOP \ SetROP2(dcMemV, R2_COPYPEN); \ if ((hPen = CreatePen(psSolid, 1, FGridColor)) == NULL) goto fail; \ hOldPen = SelectObject(dcMemV, hPen); \ p = sx-Floor(sx/gx)*gx; \ p = p>0 ? (gx-p)*FZoomIn/FZoomOut : 0; \ while (p0 ? (gy-p)*FZoomIn/FZoomOut : 0; \ while (p0) { \ gx = FGridGapX*FGridMark; \ gy = FGridGapY*FGridMark; \ gzx = gx*FZoomIn/FZoomOut; \ gzy = gy*FZoomIn/FZoomOut; \ if ((hPen = CreatePen(psSolid, 1, FGridMarkColor)) == NULL) goto fail; \ hOldPen = SelectObject(dcMemV, hPen); \ p = sx-Floor(sx/gx)*gx; \ p = p>0 ? (gx-p)*FZoomIn/FZoomOut : 0; \ while (p0 ? (gy-p)*FZoomIn/FZoomOut : 0; \ while (pBrush->Color = FColor; if (vwFillRect(Rect(vw, 0, Width, vh)); if (vhFillRect(Rect(0, vh, Width, Height)); } if ((dcSrc = FBitmap->CreateDC()) == NULL) goto fail; if ((bmMemV = CreateCompatibleBitmap(dcSrc, vw, vh)) == NULL) goto fail; if ((dcMemV = CreateCompatibleDC(dcSrc)) == NULL) goto fail; SelectObject(dcMemV, bmMemV); if (hpaintpal) SelectPalette(dcMemV, hpaintpal, false); SetStretchBltMode(dcMemV, COLORONCOLOR); StretchBlt(dcMemV, 0, 0, vw, vh, dcSrc, sx, sy, pw, ph, SRCCOPY); if (FGrid) { gx = FGridGapX; gy = FGridGapY; gzx = gx*FZoomIn/FZoomOut; gzy = gy*FZoomIn/FZoomOut; if (gzx>=5 && gzy>=5) { if (FParentImage) { FGridColor = FParentImage->GridColor; FGridGapX = FParentImage->GridGapX; FGridGapY = FParentImage->GridGapY; FGridMark = FParentImage->GridMark; FGridMarkColor = FParentImage->GridMarkColor; sx = FParentImage->PositionX+(Left-FParentImage->Left)*FZoomOut/FZoomIn; sy = FParentImage->PositionY+(Top-FParentImage->Top)*FZoomOut/FZoomIn; } PAINT_GRIDLINE_TOP; } } if (FOnPaintZoom) FOnPaintZoom(this, dcMemV); if (FCross) PAINT_CROSSLINE; BitBlt(dcDst, 0, 0, vw, vh, dcMemV, 0, 0, SRCCOPY); DeleteDC(dcMemV); DeleteObject(bmMemV); FBitmap->DeleteDC(dcSrc); if (hpaintpal) { if (holdpal) SelectPalette(dcDst, holdpal, false); DeleteObject(hpaintpal); } if (FOnPaint) FOnPaint(this); return; fail: if (dcSrc) { if (bmMemV) { if (dcMemV) { if (hPen) DeleteObject(hPen); DeleteDC(dcMemV); } DeleteObject(bmMemV); } FBitmap->DeleteDC(dcSrc); } if (hpaintpal) { if (holdpal) SelectPalette(dcDst, holdpal, false); DeleteObject(hpaintpal); } } //--------------------------------------------------------------------------- void __fastcall TPSelectImage::PaintTopMask(BITMAPHANDLE *bh) { BITMAPHANDLE *mh; HPALETTE hpaintpal = NULL, holdpal = NULL; int pw, ph, vw, vh; double sx, sy, x, y, gx, gy, gzx, gzy, p; HDC dcSrc = NULL, dcDst, dcMask = NULL, dcMemV = NULL, dcMSrc = NULL; HBITMAP bmMemV = NULL; HPEN hPen = NULL, hOldPen; mh = FMask->Handle; if (mh) { PAINT_INIT; if (FColorEnable) { TGraphicControl::Canvas->Brush->Color = FColor; if (vwFillRect(Rect(vw, 0, Width, vh)); if (vhFillRect(Rect(0, vh, Width, Height)); } if ((dcMSrc = FMask->CreateDC()) == NULL) goto fail; if ((dcSrc = FBitmap->CreateDC()) == NULL) goto fail; if ((bmMemV = CreateCompatibleBitmap(dcSrc, vw, vh)) == NULL) goto fail; if ((dcMemV = CreateCompatibleDC(dcSrc)) == NULL) goto fail; SelectObject(dcMemV, bmMemV); if (hpaintpal) SelectPalette(dcMemV, hpaintpal, false); SetStretchBltMode(dcMemV, COLORONCOLOR); StretchBlt(dcMemV, 0, 0, vw, vh, dcSrc, sx, sy, pw, ph, SRCCOPY); StretchBlt(dcMemV, 0, 0, vw, vh, dcMSrc, sx, sy, pw, ph, SRCINVERT); if (FGrid) { gx = FGridGapX; gy = FGridGapY; gzx = gx*FZoomIn/FZoomOut; gzy = gy*FZoomIn/FZoomOut; if (gzx>=5 && gzy>=5) { if (FParentImage) { FGridColor = FParentImage->GridColor; FGridGapX = FParentImage->GridGapX; FGridGapY = FParentImage->GridGapY; FGridMark = FParentImage->GridMark; FGridMarkColor = FParentImage->GridMarkColor; sx = FParentImage->PositionX+(Left-FParentImage->Left)*FZoomOut/FZoomIn; sy = FParentImage->PositionY+(Top-FParentImage->Top)*FZoomOut/FZoomIn; } PAINT_GRIDLINE_TOP; } } if (FOnPaintZoom) FOnPaintZoom(this, dcMemV); if (FCross) PAINT_CROSSLINE; BitBlt(dcDst, 0, 0, vw, vh, dcMemV, 0, 0, SRCCOPY); DeleteDC(dcMemV); DeleteObject(bmMemV); FBitmap->DeleteDC(dcSrc); FMask->DeleteDC(dcMSrc); if (hpaintpal) { if (holdpal) SelectPalette(dcDst, holdpal, false); DeleteObject(hpaintpal); } if (FOnPaint) FOnPaint(this); } return; fail: if (dcMSrc) { if (dcSrc) { if (bmMemV) { if (dcMemV) { if (hPen) DeleteObject(hPen); DeleteDC(dcMemV); } DeleteObject(bmMemV); } FBitmap->DeleteDC(dcSrc); } FMask->DeleteDC(dcMSrc); } if (hpaintpal) { if (holdpal) SelectPalette(dcDst, holdpal, false); DeleteObject(hpaintpal); } } //--------------------------------------------------------------------------- #define PAINT_GRIDLINE_BOTTOM \ SetROP2(dcMemV, R2_COPYPEN); \ if ((hPen = CreatePen(psSolid, 1, FGridColor)) == NULL) goto fail; \ hOldPen = SelectObject(dcMemV, hPen); \ p = sx-Floor(sx/gx)*gx; \ p = p>0 ? (gx-p)*FZoomIn/FZoomOut : 0; \ while (p0 ? (gy-p)*FZoomIn/FZoomOut : 0; \ while (p0) { \ gx = FGridGapX*FGridMark; \ gy = FGridGapY*FGridMark; \ gzx = gx*FZoomIn/FZoomOut; \ gzy = gy*FZoomIn/FZoomOut; \ if ((hPen = CreatePen(psSolid, 1, FGridMarkColor)) == NULL) goto fail; \ hOldPen = SelectObject(dcMemV, hPen); \ p = sx-Floor(sx/gx)*gx; \ p = p>0 ? (gx-p)*FZoomIn/FZoomOut : 0; \ while (p0 ? (gy-p)*FZoomIn/FZoomOut : 0; \ while (pBrush->Color = FColor; if (vwFillRect(Rect(vw, Height-vh, Width, Height)); if (vhFillRect(Rect(0, 0, Width, Height-vh)); } if ((dcSrc = FBitmap->CreateDC()) == NULL) goto fail; if ((bmMemV = CreateCompatibleBitmap(dcSrc, vw, vh)) == NULL) goto fail; if ((dcMemV = CreateCompatibleDC(dcSrc)) == NULL) goto fail; SelectObject(dcMemV, bmMemV); if (hpaintpal) SelectPalette(dcMemV, hpaintpal, false); SetStretchBltMode(dcMemV, COLORONCOLOR); StretchBlt(dcMemV, 0, 0, vw, vh, dcSrc, sx, sy, pw, ph, SRCCOPY); if (FGrid) { gx = FGridGapX; gy = FGridGapY; gzx = gx*FZoomIn/FZoomOut; gzy = gy*FZoomIn/FZoomOut; if (gzx>=5 && gzy>=5) { if (FParentImage) { FGridColor = FParentImage->GridColor; FGridGapX = FParentImage->GridGapX; FGridGapY = FParentImage->GridGapY; FGridMark = FParentImage->GridMark; FGridMarkColor = FParentImage->GridMarkColor; sx = FParentImage->PositionX+(Left-FParentImage->Left)*FZoomOut/FZoomIn; sy = FParentImage->Bitmap->Height-FParentImage->PositionY- FParentImage->Height*FZoomOut/FZoomIn+(Top-FParentImage->Top)*FZoomOut/FZoomIn; if (Top < FParentImage->Top) sy--; syy = FParentImage->Bitmap->Height-ph-sy; } else { syy = FBitmap->Height-ph-sy; } PAINT_GRIDLINE_BOTTOM; } } if (FOnPaintZoom) FOnPaintZoom(this, dcMemV); BitBlt(dcDst, 0, FMarginY, vw, vh, dcMemV, 0, 0, SRCCOPY); DeleteDC(dcMemV); DeleteObject(bmMemV); FBitmap->DeleteDC(dcSrc); if (hpaintpal) { if (holdpal) SelectPalette(dcDst, holdpal, false); DeleteObject(hpaintpal); } if (FOnPaint) FOnPaint(this); return; fail: if (dcSrc) { if (bmMemV) { if (dcMemV) { if (hPen) DeleteObject(hPen); DeleteDC(dcMemV); } DeleteObject(bmMemV); } FBitmap->DeleteDC(dcSrc); } if (hpaintpal) { if (holdpal) SelectPalette(dcDst, holdpal, false); DeleteObject(hpaintpal); } } //--------------------------------------------------------------------------- void __fastcall TPSelectImage::PaintBottomMask(BITMAPHANDLE *bh) { BITMAPHANDLE *mh; HPALETTE hpaintpal = NULL, holdpal = NULL; int pw, ph, vw, vh; double sx, sy, syy, x, y, gx, gy, gzx, gzy, p; HDC dcSrc = NULL, dcDst, dcMemV = NULL, dcMSrc = NULL; HBITMAP bmMemV =NULL; HPEN hPen = NULL, hOldPen; mh = FMask->Handle; if (mh) { PAINT_INIT; if (FColorEnable) { TGraphicControl::Canvas->Brush->Color = FColor; if (vwFillRect(Rect(vw, Height-vh, Width, Height)); if (vhFillRect(Rect(0, 0, Width, Height-vh)); } if ((dcMSrc = FMask->CreateDC()) == NULL) goto fail; if ((dcSrc = FBitmap->CreateDC()) == NULL) goto fail; if ((bmMemV = CreateCompatibleBitmap(dcSrc, vw, vh)) == NULL) goto fail; if ((dcMemV = CreateCompatibleDC(dcSrc)) == NULL) goto fail; SelectObject(dcMemV, bmMemV); if (hpaintpal) SelectPalette(dcMemV, hpaintpal, false); SetStretchBltMode(dcMemV, COLORONCOLOR); StretchBlt(dcMemV, 0, 0, vw, vh, dcSrc, sx, sy, pw, ph, SRCCOPY); StretchBlt(dcMemV, 0, 0, vw, vh, dcMSrc, sx, sy, pw, ph, SRCCOPY); if (FGrid) { gx = FGridGapX; gy = FGridGapY; gzx = gx*FZoomIn/FZoomOut; gzy = gy*FZoomIn/FZoomOut; if (gzx>=5 && gzy>=5) { if (FParentImage) { FGridColor = FParentImage->GridColor; FGridGapX = FParentImage->GridGapX; FGridGapY = FParentImage->GridGapY; FGridMark = FParentImage->GridMark; FGridMarkColor = FParentImage->GridMarkColor; sx = FParentImage->PositionX+(Left-FParentImage->Left)*FZoomOut/FZoomIn; sy = FParentImage->Bitmap->Height-FParentImage->PositionY- FParentImage->Height*FZoomOut/FZoomIn+(Top-FParentImage->Top)*FZoomOut/FZoomIn; if (Top < FParentImage->Top) sy--; syy = FParentImage->Bitmap->Height-ph-sy; } else { syy = FBitmap->Height-ph-sy; } PAINT_GRIDLINE_BOTTOM; } } if (FOnPaintZoom) FOnPaintZoom(this, dcMemV); BitBlt(dcDst, 0, FMarginY, vw, vh, dcMemV, 0, 0, SRCCOPY); DeleteDC(dcMemV); DeleteObject(bmMemV); FBitmap->DeleteDC(dcSrc); FMask->DeleteDC(dcMSrc); if (hpaintpal) { if (holdpal) SelectPalette(dcDst, holdpal, false); DeleteObject(hpaintpal); } if (FOnPaint) FOnPaint(this); } return; fail: if (dcMSrc) { if (dcSrc) { if (bmMemV) { if (dcMemV) { if (hPen) DeleteObject(hPen); DeleteDC(dcMemV); } DeleteObject(bmMemV); } FBitmap->DeleteDC(dcSrc); } FMask->DeleteDC(dcMSrc); } if (hpaintpal) { if (holdpal) SelectPalette(dcDst, holdpal, false); DeleteObject(hpaintpal); } } //--------------------------------------------------------------------------- void __fastcall TPSelectImage::PropertyChange() { if (PositionChange()) { if (FOnPositionChange) FOnPositionChange(this); } Invalidate(); } //--------------------------------------------------------------------------- void __fastcall TPSelectImage::SetColorEnable(bool Value) { if (FColorEnable!=Value) { FColorEnable = Value; Paint(); Invalidate(); } } //--------------------------------------------------------------------------- // Protected Function //--------------------------------------------------------------------------- void __fastcall TPSelectImage::Paint() { BITMAPHANDLE *bh; if (ComponentState.Contains(csDesigning)) { TGraphicControl::Canvas->Pen->Style = psDash; TGraphicControl::Canvas->Brush->Style = bsClear; TGraphicControl::Canvas->Rectangle(0, 0, Width, Height); } else { bh = FBitmap->Handle; if (bh) { if (Width<=0 || Height<=0) return; if (FMaskEnabled) { if (FBitmap->CoordinateSystem==csTopLeft) PaintTopMask(bh); else PaintBottomMask(bh); } else { if (FBitmap->CoordinateSystem==csTopLeft) PaintTop(bh); else PaintBottom(bh); } } } } //--------------------------------------------------------------------- void __fastcall TPSelectImage::MouseDown(TMouseButton Button, TShiftState Shift, int X, int Y) { int px, py; if (FCoordinationTransfer) { if (FBitmap->Handle) { px = FPositionX+X*FZoomOut/FZoomIn; py = FRealPosY+(Y-FMarginY)*FZoomOut/FZoomIn; TControl::MouseDown(Button, Shift, px, py); } else { TControl::MouseDown(Button, Shift, X, Y); } } else { TControl::MouseDown(Button, Shift, X, Y); } } //--------------------------------------------------------------------- void __fastcall TPSelectImage::MouseMove(TShiftState Shift, int X, int Y) { int px, py; if (FCoordinationTransfer) { if (FBitmap->Handle) { px = FPositionX+X*FZoomOut/FZoomIn; py = FRealPosY+(Y-FMarginY)*FZoomOut/FZoomIn; TControl::MouseMove(Shift, px, py); } else { TControl::MouseMove(Shift, X, Y); } } else { TControl::MouseMove(Shift, X, Y); } } //--------------------------------------------------------------------- void __fastcall TPSelectImage::MouseUp(TMouseButton Button, TShiftState Shift, int X, int Y) { int px, py; if (FCoordinationTransfer) { if (FBitmap->Handle) { px = FPositionX+X*FZoomOut/FZoomIn; py = FRealPosY+(Y-FMarginY)*FZoomOut/FZoomIn; TControl::MouseUp(Button, Shift, px, py); } else { TControl::MouseUp(Button, Shift, X, Y); } } else { TControl::MouseUp(Button, Shift, X, Y); } } //--------------------------------------------------------------------- // Public Function //--------------------------------------------------------------------------- void __fastcall TPSelectImage::OutlineBitmapRgn() //added by qe ÀÛ¾÷±¸¿ªÀâ±â ±â´É ÈÖÇØ { pBITMAPHANDLE bh; RGNXFORM XForm; HRGN rgn; HBRUSH hBrush; if (FBitmap->IsUse) return; bh = FBitmap->Handle; if (bh) { if (L_BitmapHasRgn(bh)) { if (FParentImage) { FZoomIn = FParentImage->ZoomIn; FZoomOut = FParentImage->ZoomOut; } nFrameRgn = (nFrameRgn+1)&7; XForm.uViewPerspective = TOP_LEFT; XForm.nXScalarNum = FZoomIn; XForm.nXScalarDen = FZoomOut; XForm.nYScalarNum = FZoomIn; XForm.nYScalarDen = FZoomOut; XForm.nXOffset = -FPositionX*FZoomIn/FZoomOut; if (ZoomIn>1) { XForm.nYOffset = FMarginY-(FRealPosY-1)*FZoomIn/FZoomOut-FZoomIn; } else { XForm.nYOffset = FMarginY-(FRealPosY-1)*FZoomIn/FZoomOut; } L_FrameBitmapRgn(TGraphicControl::Canvas->Handle, bh, &XForm, nFrameRgn); } } } //--------------------------------------------------------------------------- int __fastcall TPSelectImage::BitmapToCanvasX(int X) { return (X-FPositionX)*ZoomIn/ZoomOut; } //--------------------------------------------------------------------------- int __fastcall TPSelectImage::BitmapToCanvasY(int Y) { return FMarginY+(Y-FRealPosY)*ZoomIn/ZoomOut; } //--------------------------------------------------------------------------- int __fastcall TPSelectImage::CanvasToBitmapX(int X) { return FPositionX+X*FZoomOut/FZoomIn; } //--------------------------------------------------------------------------- int __fastcall TPSelectImage::CanvasToBitmapY(int Y) { return FRealPosY+(Y-FMarginY)*FZoomOut/FZoomIn; } //--------------------------------------------------------------------------- void __fastcall TPSelectImage::SetCrossPos(POINT Pos) { FCrossPos.x = Pos.x; FCrossPos.y = Pos.y; } //--------------------------------------------------------------------------- namespace Tpselectimage { void __fastcall PACKAGE Register() { TComponentClass classes[1] = {__classid(TPSelectImage)}; RegisterComponents("Texpia", classes, 0); } } //---------------------------------------------------------------------------