//--------------------------------------------------------------------------- #include #pragma hdrstop #include "TPTestKnitImage.h" #pragma link "TPImage" #pragma package(smart_init) //--------------------------------------------------------------------------- //TPTestKnitImage //--------------------------------------------------------------------------- // ValidCtrCheck is used to assure that the components created do not have // any pure virtual functions. // static inline void ValidCtrCheck(TPTestKnitImage *) { new TPTestKnitImage(NULL); } //--------------------------------------------------------------------------- __fastcall TPTestKnitImage::TPTestKnitImage(TComponent* Owner) : TPImage(Owner) { FBitmap = new TTexpiaBitmap; FYarnBitmap = NULL;//new TTexpiaBitmap; FMarkBitmap = NULL;//new TTexpiaBitmap; FKnitBitmap = NULL;//new TTexpiaBitmap; FBitmap->OnPropertyChange = PropertyChange; FBitmap->CoordinateSystem = csBottomLeft; } //--------------------------------------------------------------------------- __fastcall TPTestKnitImage::~TPTestKnitImage() { if (FBitmap) delete FBitmap; } //--------------------------------------------------------------------------- // Private Function //--------------------------------------------------------------------------- #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; \ TGraphicControl::Canvas->Brush->Color = FColor; \ if (vwFillRect(Rect(vw, Height-vh, Width, Height)); \ if (vhFillRect(Rect(0, 0, Width, Height-vh)); //--------------------------------------------------------------------------- #define PAINT_GRIDLINE \ 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 (pCreateDC(); \ BitBlt(dcMemV, 0, 0, pw*16, ph*16, dcMark, sx*16, sy*16, SRCINVERT); \ FMarkBitmap->DeleteDC(dcMark); \ } //--------------------------------------------------------------------------- // by playzzang for CrossLine #define PAINT_CROSSLINE \ if (FCrossLine) { \ hOldPen = SelectObject(dcMemV, GetStockObject(WHITE_PEN)); \ SetROP2(dcMemV, R2_NOT); \ MoveToEx(dcMemV, FCrossPos.x, 0, NULL); \ LineTo(dcMemV, FCrossPos.x, vh); \ MoveToEx(dcMemV, 0, FCrossPos.y, NULL); \ LineTo(dcMemV, vw, FCrossPos.y); \ SelectObject(dcMemV, hOldPen); \ } //--------------------------------------------------------------------------- void __fastcall TPTestKnitImage::PaintBottom(BITMAPHANDLE *bh) { HPALETTE hpaintpal = NULL, holdpal = NULL; int pw, ph, vw, vh, n, w, x, y; double sx, sy, syy, gx, gy, gzx, gzy, p; HDC dcSrc = NULL, dcDst, dcMemV = NULL; HBITMAP bmMemV = NULL; HPEN hPen = NULL, hOldPen; BYTE *bp; PAINT_INIT; 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); FBitmap->DeleteDC(dcSrc); if (FYarnBitmap) { if ((dcSrc = FYarnBitmap->CreateDC()) == NULL) goto fail; SetStretchBltMode(dcMemV, COLORONCOLOR); StretchBlt(dcMemV, (bmx-sx)*FZoomIn/FZoomOut, (bmy-sy)*FZoomIn/FZoomOut, bmw*FZoomIn/FZoomOut, bmh*FZoomIn/FZoomOut, dcSrc, 0, 0, FYarnBitmap->Width, FYarnBitmap->Height, SRCCOPY); FYarnBitmap->DeleteDC(dcSrc); } if (FMarkBitmap) { if ((dcSrc = FMarkBitmap->CreateDC()) == NULL) goto fail; SetStretchBltMode(dcMemV, COLORONCOLOR); StretchBlt(dcMemV, (bmx-sx)*FZoomIn/FZoomOut, (bmy-sy)*FZoomIn/FZoomOut, bmw*FZoomIn/FZoomOut, bmh*FZoomIn/FZoomOut, dcSrc, 0, 0, FMarkBitmap->Width, FMarkBitmap->Height, SRCINVERT); FMarkBitmap->DeleteDC(dcSrc); } DrawPresentKo(dcMemV); if (FOnPaintKo) FOnPaintKo(this, dcMemV); if (FOnPaintZoom) FOnPaintZoom(this, dcMemV); BitBlt(dcDst, 0, FMarginY, vw, vh, dcMemV, 0, 0, SRCCOPY); DeleteDC(dcMemV); DeleteObject(bmMemV); 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); } } if (hpaintpal) { if (holdpal) SelectPalette(dcDst, holdpal, false); DeleteObject(hpaintpal); } } //--------------------------------------------------------------------------- bool __fastcall TPTestKnitImage::PositionChange() { int m = FMarginY, r = FRealPosY; if (FBitmap->Handle) { int ps = Height*FZoomOut/FZoomIn; 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; } if (m != FMarginY || r != FRealPosY) { FMarginY = m; FRealPosY = r; return true; } } return false; } //--------------------------------------------------------------------------- void __fastcall TPTestKnitImage::PropertyChange() { if (PositionChange()) { if (FOnPositionChange) FOnPositionChange(this); } Invalidate(); } //--------------------------------------------------------------------------- void __fastcall TPTestKnitImage::DrawPresentKo(HDC dc) { POINT pt[4]; HPEN hPen = NULL, hOldPen; pt[0].x=30; pt[0].y=50; pt[1].x=30; pt[1].y=70; pt[2].x=50; pt[2].y=70; pt[3].x=50; pt[3].y=50; if (dc) { SetROP2(dc, R2_COPYPEN); hPen = CreatePen(psSolid, 1, PALETTEINDEX(2)); hOldPen = SelectObject(dc, hPen); MoveToEx(dc, pt[0].x, pt[0].y, NULL); LineTo(dc, pt[1].x, pt[1].y); LineTo(dc, pt[2].x, pt[2].y); LineTo(dc, pt[3].x, pt[3].y); LineTo(dc, pt[0].x, pt[0].y); SelectObject(dc, hOldPen); DeleteObject(hPen); hPen = NULL; } } //--------------------------------------------------------------------------- //--------------------------------------------------------------------------- // Protected Function //--------------------------------------------------------------------------- void __fastcall TPTestKnitImage::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) { // PaintBottomMask(bh); // } else { PaintBottom(bh); // } } } } //--------------------------------------------------------------------- void __fastcall TPTestKnitImage::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 TPTestKnitImage::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 TPTestKnitImage::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 TPTestKnitImage::Line(int sx, int sy, int ex, int ey, int thick, COLORREF c) { HDC dcSrc=NULL; HPEN hPen=NULL, hOldPen; dcSrc = FBitmap->CreateDC(); SetROP2(dcSrc, R2_COPYPEN); hPen = CreatePen(psSolid, thick, c); hOldPen = SelectObject(dcSrc, hPen); MoveToEx(dcSrc, sx, sy, NULL); LineTo(dcSrc, ex, ey); SelectObject(dcSrc, hOldPen); FBitmap->DeleteDC(dcSrc); DeleteObject(hPen); hPen = NULL; } //--------------------------------------------------------------------------- bool __fastcall TPTestKnitImage::Create(int width, int height, int bpp, RGBQUAD *rgb) { FBitmap->CoordinateSystem = csBottomLeft; if (!FBitmap->Create(width, height, bpp, rgb)) return false; FKnitBitmap = (TTexpiaBitmap *)FBitmap; return true; } //--------------------------------------------------------------------------- void __fastcall TPTestKnitImage::OutlineBitmapRgn(bool bComplex) { 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; } if (bComplex) { L_FrameBitmapRgn(TGraphicControl::Canvas->Handle, bh, &XForm, nFrameRgn); } else { L_GetBitmapRgnHandle(bh, &XForm, &rgn); if ((hBrush = CreatePatternBrush(bmFrameRgn[nFrameRgn])) != NULL) { FrameRgn(TGraphicControl::Canvas->Handle, rgn, hBrush, 1, 1); DeleteObject(hBrush); } DeleteObject(rgn); } } } } //--------------------------------------------------------------------------- void __fastcall TPTestKnitImage::RectPaint(RECT Src) { ; } //--------------------------------------------------------------------------- void __fastcall TPTestKnitImage::RectEditPaint(RECT Src, TRectEditPaint func, void *pData) { ; } //--------------------------------------------------------------------------- int __fastcall TPTestKnitImage::BitmapToCanvasX(int X) { return (X-FPositionX)*FZoomIn/FZoomOut; } //--------------------------------------------------------------------------- int __fastcall TPTestKnitImage::BitmapToCanvasY(int Y) { return FMarginY+(Y-FRealPosY)*FZoomIn/FZoomOut; } //--------------------------------------------------------------------------- int __fastcall TPTestKnitImage::CanvasToBitmapX(int X) { return FPositionX+X*FZoomOut/FZoomIn; } //--------------------------------------------------------------------------- int __fastcall TPTestKnitImage::CanvasToBitmapY(int Y) { return FRealPosY+(Y-FMarginY)*FZoomOut/FZoomIn; } //--------------------------------------------------------------------------- void __fastcall TPTestKnitImage::FullViewPaint(HDC dcDst, int zi, int zo) { HPALETTE hpaintpal = NULL, holdpal = NULL; int pw, ph, vw, vh; HDC dcSrc = NULL; vw = FBitmap->Width *zi / zo; vh = FBitmap->Height *zi / zo; pw = vw * zo / zi; ph = vh * zo / zi; if (FBitmap->BitsPerPixel==8) { hpaintpal = L_CreatePaintPalette(dcDst, FBitmap->Handle); if (hpaintpal) holdpal = SelectPalette(dcDst, hpaintpal, false); } if ((dcSrc = FBitmap->CreateDC()) == NULL) goto fail; SetStretchBltMode(dcDst, COLORONCOLOR); StretchBlt(dcDst, 0, 0, vw, vh, dcSrc, 0, 0, pw, ph, SRCCOPY); FBitmap->DeleteDC(dcSrc); if (hpaintpal) { if (holdpal) SelectPalette(dcDst, holdpal, false); DeleteObject(hpaintpal); } return; fail: if (dcSrc) FBitmap->DeleteDC(dcSrc); if (hpaintpal) { if (holdpal) SelectPalette(dcDst, holdpal, false); DeleteObject(hpaintpal); } } //--------------------------------------------------------------------------- void __fastcall TPTestKnitImage::ChangeRGBColors(RGBQUAD *rgb) { FBitmap->PutColors(0, 256, rgb); if (FSubBitmap) FSubBitmap->PutColors(0, 256, rgb); if (FSubMask) FSubMask->PutColors(0, 256, rgb); } //--------------------------------------------------------------------------- bool __fastcall TPTestKnitImage::CreateMark() { return true; } //--------------------------------------------------------------------------- bool __fastcall TPTestKnitImage::SetMark() { return true; } //--------------------------------------------------------------------------- bool __fastcall TPTestKnitImage::SetMark(RECT rect) { return true; } //--------------------------------------------------------------------------- bool __fastcall TPTestKnitImage::DrawMark(int x, int y, int n) { return true; } //--------------------------------------------------------------------------- bool __fastcall TPTestKnitImage::ClearMark(int x, int y, int w, int h) { return true; } //--------------------------------------------------------------------------- void __fastcall TPTestKnitImage::SetCrossPos(POINT Pos) { FCrossPos.x = BitmapToCanvasX(Pos.x); FCrossPos.y = BitmapToCanvasY(Pos.y); } //--------------------------------------------------------------------------- namespace Tptestknitimage { void __fastcall PACKAGE Register() { TComponentClass classes[1] = {__classid(TPTestKnitImage)}; RegisterComponents("Texpia", classes, 0); } } //---------------------------------------------------------------------------