Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi 7:Attach Image to Mouse

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)

like image 812
restcoser Avatar asked Oct 23 '12 17:10

restcoser


1 Answers

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;
like image 152
Sertac Akyuz Avatar answered Oct 02 '22 00:10

Sertac Akyuz