Delphi 7:Attach Image to Mouse

delphi, image, pointers, sender

Solution

An object is already a pointer, declare your `Attached` a `TImage` (as opposed to `^TImage`) and you can assign to it like `Attached := Sender as TImage` in 'ChangeAttachedState' (as opposed to `Attached := @Sender`).

You can then attach a mouse move handler on the form like so:

procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  if Assigned(Attached) then begin
    Attached.Left := X;
    Attached.Top := Y;
  end;
end;

Problem

I want a derivate of TImage follow the Cursor when it has been clicked and stop following when it gets clicked again. For this, I created a pointer, named 'Attached', that points to a TImage or a derivate. ``` var Attached: ^TImage; ``` I also set the derivate of Timage to call the procedure ChangeAttachState when its clicked. Now, in the ChangeAttachState procedure I want to change the pointer that it points on the clicked Image or point to nil when an Image was already attached. In Code: ``` procedure TForm1.ChangeAttachState(Sender:TObject); begin if Attached = nil then Attached := @Sender else Attached := nil; end; ``` However, the line 'Attached := @Sender' does not seem to work, causing an Access violation when I want to use the pointer to i.e. move the Image to the Right. I think the pointer points at a wrong location. How can I make the pointer point at the correct save adress or make the clicked Image follow the mouse with other methods? (I hope I used the right technical terms, as English is not my native language)

Original source