var
fDragging: Boolean;
fRect: TRect;
function NormalizeRect (ARect: TRect): TRect;
var
tmp: Integer;
begin
if ARect.Bottom < ARect.Top then
begin
tmp := ARect.Bottom;
ARect.Bottom := ARect.Top;
ARect.Top := tmp;
end;
if ARect.Right < ARect.Left then
begin
tmp := ARect.Right;
ARect.Right := ARect.Left;
ARect.Left := tmp;
end;
Result := ARect;
end;
procedure TShapesForm.Image1MouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
if Button = mbLeft then
begin
fDragging := True;
SetCapture (Handle);
fRect.Left := X;
fRect.Top := Y;
fRect.BottomRight := fRect.TopLeft;
Canvas.DrawFocusRect (fRect);
end;
end;
procedure TShapesForm.Image1MouseMove(Sender: TObject;
Shift: TShiftState; X, Y: Integer);
var
ARect: TRect;
begin
Caption := Format ('ShapeBmp (x=%d, y=%d)', [X, Y]);
if fDragging then
begin
ARect := NormalizeRect (fRect);
Canvas.DrawFocusRect (ARect);
fRect.Right := X;
fRect.Bottom := Y;
ARect := NormalizeRect (fRect);
Canvas.DrawFocusRect (ARect);
end
else
if ssShift in Shift then
Image1.Canvas.Pixels [X, Y] := clRed;
end;
procedure TShapesForm.Image1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if fDragging then
begin
ReleaseCapture;
fDragging := False;
Image1.Canvas.Rectangle (fRect.Left, fRect.Top,
fRect.Right, fRect.Bottom);
end;
end;