Question

I need to move an image with along X-axis and, when it reaches defined borders, stop moving (I'm making my own trackbar). I can't find out how to define borders. With my code when it reaches border, it stucks there and unable to move. Here's the code

var 
  PinCurrentPosition,PinStartingPosition:integer; 
  move:boolean;

procedure TForm1.FormCreate(Sender: TObject);
begin
DoubleBuffered := True;
Image5.Picture.LoadFromFile('Untitled2.bmp');
PinStartingPosition:=Image5.Left;
end;

procedure TForm1.Image5MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
if (button <> mbLeft) then move:=false
  else
  begin
    move:=true;
    PinCurrentPosition:=x;
  end;
end;

procedure TForm1.Image5MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
 if move and ((PinStartingPosition-75)<Image5.Left) 
 and ((PinStartingPosition+75)>Image5.Left) then
 Image5.Left:=Image5.Left+x-PinCurrentPosition;
end;

procedure TForm1.Image5MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
move:=false;
end;
Was it helpful?

Solution

You should add an Else to Image5MouseMove procedure to correct image position if it is outside of movable area:

procedure TForm1.Image5MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
    if 
      move and 
      (Image5.Left>(PinStartingPosition-75)) and
      (Image5.Left<(PinStartingPosition+75)) 
    then
      Image5.Left:=Image5.Left+x-PinCurrentPosition;
    else if Image5.Left<=(PinStartingPosition-75) then
      Image5.Left:= PinStartingPosition-75+1
    else if Image5.Left>=(PinStartingPosition+75) then
      Image5.Left:= PinStartingPosition+75-1; 
end;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top