牵引对象数组

时间:2017-03-20 06:58:38

标签: delphi

有一个包含2个动态对象的数组:圆形(SP)和对象内的编号铭文(LblPhoto)。

问题:如何确定移动SP [n]数组中的哪个对象?

(SP [CurPhoto] .OnTrack)

LblPhoto [n] .position.y:= SP [n] position.x

var 
SP: array [0..10] of TSelectionPoint; // Circle
LblPhoto: array [0..10] of TLabel; // Text in Circle

// Add new circle
Procedure AddObjectPoint(X,Y:Single);
begin
  //
if CurPhoto>10 then
  begin
   ShowMessage('Maximum number exceeded ');
   exit;
  end;


        with dyn_frm do
         begin
             SP[CurPhoto]:=TSelectionPoint.Create(ImageViewer1); //
             SP[CurPhoto].Parent:=ImageViewer1;

             LblPhoto[CurPhoto]:=TLabel.Create(ImageViewer1); //
             LblPhoto[CurPhoto].Parent:=ImageViewer1;


            SP[CurPhoto].position.y:=y-120;
            SP[CurPhoto].position.x:= x;
            SP[CurPhoto].Width:=80;
            SP[CurPhoto].Scale.X:=6;
            SP[CurPhoto].Scale.Y:=6;
            SP[CurPhoto].OnClick:=PhotoDialogClick;
            SP[CurPhoto].OnTrack:=SelectionPoint1Track; /Fasten the object's handler


            LblPhoto[CurPhoto].Position.Y:=SP[CurPhoto].position.y-10;
            LblPhoto[CurPhoto].Position.X:=SP[CurPhoto].position.x-10;
            LblPhoto[CurPhoto].Text:=IntToStr(CurPhoto+1);


            Inc(CurPhoto);
         end;
end;



// Handler for moving the circle and moving the inscription inside
procedure dyn_frm.SelectionPoint1Track(Sender: TObject;
  var X, Y: Single);
begin
 LblPhoto[?].Position.X:=X-10;
 LblPhoto[?].Position.Y:=Y-10;
end;

1 个答案:

答案 0 :(得分:0)

回顾这个问题,以下可能是更好的解决方案:

使用Tag的{​​{1}}属性存储数组的索引

TSelectionPoint

然后SP[CurPhoto].tag := CurPhoto; 处理程序变为

OnTrack