Delphi 7:Attach Image to Mouse
Asked Answered
F

1

6

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)

Frog answered 23/10, 2012 at 17:6 Comment(0)
G
6

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;
Gerigerianna answered 23/10, 2012 at 17:38 Comment(1)
That works perfectly, thank you! Didn't know of that possibility.Frog

© 2022 - 2024 — McMap. All rights reserved.