简体   繁体   English

Delphi 7:将图像附加到鼠标

[英]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. 我希望TImage的衍生版本在单击时跟随Cursor,并在再次单击时停止跟随。 For this, I created a pointer, named 'Attached', that points to a TImage or a derivate. 为此,我创建了一个名为'Attached'的指针,指向TImage或衍生物。

var Attached: ^TImage;

I also set the derivate of Timage to call the procedure ChangeAttachState when its clicked. 我还设置了Timage的派生,在单击时调用过程ChangeAttachState。

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. 现在,在ChangeAttachState过程中,我想更改它指向所单击图像的指针,或者在已附加图像时指向nil。 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 ie move the Image to the Right. 但是,“Attached:= @Sender”这一行似乎不起作用,当我想使用指针即将图像向右移动时,会导致访问冲突。

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) (我希望我使用正确的技术术语,因为英语不是我的母语)

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 ). 一个对象已经是一个指针,声明你的Attached了一个TImage (而不是^TImage ),你可以像在'ChangeAttachedState'中那样将Attached := Sender as TImage (与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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM