{ Component to Slice a picture to cells and display the needed slice Picture : the picture to slice Col & Row : slice the pictrue to col & row Slice : the displayed slice } unit slice; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs; type TSlice = class(TGraphicControl) private { Private declarations } FPicture : TBitmap; FCol : integer; FRow : integer; FSlice :integer; procedure SetPicture ( p:TBitmap); procedure SetCol ( c:integer); procedure SetRow ( r:integer); procedure SetSlice ( s:integer); procedure UpdatePicture ( s:TObject); protected { Protected declarations } procedure Paint ; override; public { Public declarations } constructor Create (AOwner:TComponent); override; destructor Destroy ; override; published { Published declarations } property Picture: TBitmap read FPicture write SetPicture; property Col: integer read FCol write SetCol default 1; property Row: integer read FRow write SetRow default 1; property Slice: integer read FSlice write SetSlice default 1; end; procedure Register; implementation procedure Register; begin RegisterComponents('Samples', [TSlice]); end; constructor TSlice.Create (AOwner:TComponent); begin inherited Create(AOwner); FPicture:=TBitmap.Create; FPicture.OnChange:= UpdatePicture; width:=100; height:=100; fcol:=1; frow:=1; FSlice:=1; end; destructor TSlice.Destroy ; begin Fpicture.Free; inherited destroy; end; procedure TSlice.SetPicture ( p:TBitmap); begin FPicture.assign(p); end; procedure TSlice.UpdatePicture ( s:TObject); begin Invalidate; end; procedure TSlice.SetCol ( c:integer); begin FCol:= c; Invalidate; end; procedure TSlice.SetRow ( r:integer); begin FRow:= r; Invalidate; end; procedure TSlice.SetSlice ( s:integer); begin if (s < 1) or (s > row*col) then MessageDlg('Slice Number Out of range!', mtError, [mbOk] ,0) else begin FSlice:= s; Invalidate; end; end; procedure TSlice.Paint ; var dst,src: TRect; x,y: integer; begin if not picture.empty then begin width:= FPicture.width div col; height:= FPicture.height div row; x:= (slice-1) mod col; y:= (slice-1) div col; dst:= rect (0, 0, width, height); src:= rect ( x*width , y*height ,(x+1)*width ,(y+1)*height ); inherited canvas.CopyRect (dst, Picture.canvas ,src); {inherited canvas.brush.style:=bsclear; inherited canvas.Rectangle (0,0,width,height);} end; if csDesigning in ComponentState then with inherited canvas do begin pen.style:=psDash; brush.style:=bsClear; rectangle(0,0,width,height); end; end; end.
Make your own free website on Tripod.com