Delphi 7: Attach Image to Mouse

I want the derived TImage to follow the cursor when it is clicked and stop following when it is clicked again. To do this, I created a pointer called "Attached" that points to a TImage or derivation.

var Attached: ^TImage; 

I also set up a derivative of Timage to call the ChangeAttachState procedure when I clicked it.

Now, in the ChangeAttachState procedure, I want to change the pointer that it points to the pressed image, or indicate zero when the image was already attached. In code:

 procedure TForm1.ChangeAttachState(Sender:TObject); begin if Attached = nil then Attached := @Sender else Attached := nil; end; 

However, the string "Attached: = @Sender" does not work, causing an access violation when I want to use the pointer to move the image to the right.

I think the pointer points to the wrong location. How can I make a pointer point in the correct saved address or make the image click with the mouse in other ways?

(I hope I used the correct technical terms as English is not my first language)

+6
source share
1 answer

The object is already a pointer, declares Attached a TImage (unlike ^TImage ), and you can assign it as Attached := Sender as TImage in 'ChangeAttachedState' (unlike Attached := @Sender ).

Then you can attach the mouse movement handler in the form as follows:

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

All Articles