procedure TForm1.FormCreate(Sender: TObject); begin ImageBox1 := TImageBox.Create(Self); with ImageBox1 do begin Parent := Self; Align := alClient; OutImage := Image1; end; ScrollBox1.Color := clWhite; ScrollBox1.DoubleBuffered := True; KeyPreview := True; List := TList.Create; end;
procedure TForm1.FormDestroy(Sender: TObject); var i: Integer; begin for i := 0 to List.Count - 1 do TMoveImage(List[i]).Free; List.Free; end;
procedure TForm1.FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState); var i: Integer; begin if Key = VK_DELETE then for i := List.Count - 1 downto 0 do if TMoveImage(List[i]).Selected then begin TMoveImage(List[i]).Free; List.Delete(i); end; end;
procedure TForm1.Image1MouseEnter(Sender: TObject); var mi: TMoveImage; begin Image1.Visible := False; mi := TMoveImage.Create(ScrollBox1); with mi do begin Parent := ScrollBox1; Left := Image1.Left; Top := Image1.Top; Width := Image1.Width; Height := Image1.Height; Picture.Bitmap.Assign(Image1.Picture.Bitmap); end; List.Add(mi); end;
procedure TForm1.ScrollBox1Click(Sender: TObject); var i: Integer; begin for i := 0 to List.Count - 1 do TMoveImage(List[i]).Selected := False; end;
procedure TForm1.ScrollBox1DblClick(Sender: TObject); var i: Integer; begin with TSavePictureDialog.Create(nil) do if Execute then begin with TBitmap.Create do begin Width := ScrollBox1.HorzScrollBar.Range + 20; Height := ScrollBox1.VertScrollBar.Range + 20; for i := 0 to List.Count - 1 do begin TMoveImage(List[i]).Selected := False; Canvas.Draw(TMoveImage(List[i]).Left, TMoveImage(List[i]).Top, TMoveImage(List[i]).Picture.Bitmap); end; SaveToFile(FileName); Free; end; Free; end; end;
procedure TImageBox.ImageBoxDblClick(Sender: TObject); begin FFlag := False; with TOpenPictureDialog.Create(nil) do if Execute then begin FImage.Picture.LoadFromFile(FileName); Free; end; end;
destructor TImageBox.Destroy; begin FImage.Free; FShape.Free; FBitmap.Free; inherited; end;
procedure TImageBox.ImageBoxMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); var cx,cy: Integer; begin FFlag := True; cx := X - HorzScrollBar.Position; cy := Y - VertScrollBar.Position; FShape.BoundsRect := Rect(cx, cy, cx, cy); end;
procedure TImageBox.ImageBoxMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer); var cx,cy: Integer; begin if FFlag then begin cx := X - HorzScrollBar.Position; cy := Y - VertScrollBar.Position; if FFlag then FShape.BoundsRect := Rect(FShape.Left, FShape.Top, cx, cy); end else FShape.BoundsRect := Rect(0, 0, 0, 0); end;
procedure TImageBox.ImageBoxMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); var R: TRect; begin if not FFlag then Exit; FFlag := False; if FShape.Width * FShape.Height < 100 then Exit;
if FShape.Width < 0 then begin FShape.Left := FShape.Left + FShape.Width; FShape.Width := -FShape.Width; end; if FShape.Height < 0 then begin FShape.Top := FShape.Top + FShape.Height; FShape.Height := -FShape.Height; end; FBitmap.Width := FShape.Width; FBitmap.Height := FShape.Height; R := FShape.BoundsRect; OffsetRect(R, HorzScrollBar.Position, VertScrollBar.Position); FBitmap.Canvas.CopyRect(FShape.ClientRect, FImage.Canvas, R); if Assigned(FOutImage) then with FOutImage do begin AutoSize := True; Picture.Bitmap.Assign(FBitmap); Left := (Parent.ClientWidth - FOutImage.Width) div 2; Top := (Parent.ClientHeight - Height) div 2; Visible := True; end; end;
procedure TImageBox.SetOutImage(const Value: TImage); begin FOutImage := Value; end;
constructor TMoveImage.Create(AOwner: TComponent); begin inherited; Parent := TWinControl(AOwner); Left := (TWinControl(AOwner).ClientWidth - Width) div 2; Top := (TWinControl(AOwner).ClientHeight - Height) div 2; end;
procedure TMoveImage.MouseMove(Shift: TShiftState; X, Y: Integer); var i: Integer; begin inherited; if FFlag then begin Left := Left + X - FX; Top := Top + Y - FY; for i := 0 to List.Count - 1 do if (TMoveImage(List[i]) <> Self) and (TMoveImage(List[i]).Selected) then begin TMoveImage(List[i]).Left := TMoveImage(List[i]).Left + X - FX; TMoveImage(List[i]).Top := TMoveImage(List[i]).Top + Y - FY; end; end; end;
procedure TMoveImage.MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); begin inherited; FFlag := False; if not (ssCtrl in Shift) then Selected := False; end;
procedure TMoveImage.SetSelected(const Value: Boolean); var bit: TBitmap; begin if Value <> FSelected then begin FSelected := Value; bit := TBitmap.Create; bit.Width := Width; bit.Height := Height; BitBlt(Canvas.Handle, 0, 0, Width, Height, bit.Canvas.Handle, 0, 0, SRCINVERT); Repaint; bit.Free; end; end;