unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls;
type
{ TForm1 }
TForm1 = class(TForm)
Image1: TImage;
Image2: TImage;
Label1: TLabel;
lbeGoto: TLabeledEdit;
lbeResultMax: TLabeledEdit;
lbeResultMin: TLabeledEdit;
procedure FormCreate(Sender: TObject);
procedure Image1Click(Sender: TObject);
procedure Image2MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer
);
procedure lbeGotoEditingDone(Sender: TObject);
procedure lbeResultMaxEditingDone(Sender: TObject);
procedure lbeResultMinEditingDone(Sender: TObject);
private
FResultMin: Integer;
FResultMax: Integer;
procedure CalculatePosition;
procedure GotoPosition(Pos: Integer);
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.lbeResultMaxEditingDone(Sender: TObject);
var
NewValue: Integer;
begin
if not(TryStrToInt(lbeResultMax.Text, NewValue)) then
begin
lbeResultMax.Text := IntToStr(FResultMax);
Exit;
end;
if NewValue <= FResultMin then
begin
lbeResultMax.Text := IntToStr(FResultMax);
Exit;
end;
FResultMax := NewValue;
CalculatePosition;
end;
procedure TForm1.lbeResultMinEditingDone(Sender: TObject);
var
NewValue: Integer;
begin
if not(TryStrToInt(lbeResultMin.Text, NewValue)) then
begin
lbeResultMin.Text := IntToStr(FResultMin);
Exit;
end;
if NewValue >= FResultMax then
begin
lbeResultMin.Text := IntToStr(FResultMin);
Exit;
end;
FResultMin := NewValue;
CalculatePosition;
end;
procedure TForm1.lbeGotoEditingDone(Sender: TObject);
var
NewValue: Integer;
begin
if not(TryStrToInt(lbeGoto.Text, NewValue)) then Exit;
if (NewValue < FResultMin) or (NewValue > FResultMax) then Exit;
lbeGoto.Text := '';
GotoPosition(NewValue);
end;
procedure TForm1.Image1Click(Sender: TObject);
var
NewPos:TPoint;
begin
NewPos := ScreenToClient(Mouse.CursorPos);
Image2.Left := NewPos.X - (Image2.Width div 2);
CalculatePosition;
end;
procedure TForm1.Image2MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
var
AllowedMin,
AllowedMax : Integer;
begin
if not(ssLeft in Shift) then Exit;
AllowedMin := Image1.Left;
AllowedMax := Image1.Left + Image1.Width - Image2.Width;
Image2.Left := Image2.Left + X - (Image2.Width div 2);
if Image2.Left < AllowedMin then Image2.Left := AllowedMin;
if Image2.Left > AllowedMax then Image2.Left := AllowedMax;
CalculatePosition;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
FResultMin := StrToInt(lbeResultMin.Text);
FResultMax := StrToInt(lbeResultMax.Text);
end;
procedure TForm1.CalculatePosition;
var
Result: Integer;
begin
Result := ((FResultMax-FResultMin) *
(Image2.Left-Image1.Left) div
(Image1.Width-Image2.Width)) + FResultMin;
Label1.Caption := Result.ToString;
end;
procedure TForm1.GotoPosition(Pos: Integer);
begin
Image2.Left := Round((Pos-FResultMin) * (Image1.Width-Image2.Width) /
(FResultMax-FResultMin) + Image1.Left);
Label1.Caption := Pos.ToString;
end;
end.