我有一个TMachine类,也就是TShape类

unit MachineShape;


interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, extctrls,myDataModule,Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type

TMachine = class(TShape)
  private
    { Private declarations }
  public
    { Public declarations }
    procedure PlaceShape(sizeW,sizeH :integer; name, order,asset : string);
  end;
implementation



    Procedure TMachine.PlaceShape(sizeW,sizeH :integer; name, order,asset : string);
    begin
       self.width :=  sizeW;
       self.height := sizeH;
       self.top := 136;
       self.left := MyDataModule.fDB.LastX +2;//set left
       MyDataModule.fDB.lastx := left + sizeW;
    end;



end.


我将如何为此添加onmouseenter代码?因此,在运行时添加形状时,鼠标输入代码将具有自己的形状。像这样的东西,我知道这行不通..但是也许它将告诉你我想要做什么?因此,当我创建TMachine时,我会将名称和编号传递给此过程,这将使onmouseenter过程使用我发送的名称/编号进行更新。

Procedure TMachine.EditMouseEnter(name,number :string);
begin
   ....onmouseenter(Label2.Caption := name AND label3.caption := Number)...
end

最佳答案

添加一个OnMouseEnter事件:

type
  TMachineEvent = procedure(Sender: TMachine) of object;

  TMachine = class(TShape)
  private
    FOnMouseEnter: TMachineEvent;
    ...
    procedure CMMouseEnter(var Message: TMessage); message CM_MOUSEENTER;
  protected
    procedure DoMouseEnter; virtual;
  published
    property OnMouseEnter: TMachineEvent read FOnMouseEnter write FOnMouseEnter;
    ...
  end;

implementation

{ TMachine }

procedure TMachine.CMMouseenter(var Message: TMessage);
begin
  DoMouseEnter;
  inherited;
end;

procedure TMachine.DoMouseEnter;
begin
  if Assigned(FOnMouseEnter) then
    FOnMouseEnter(Self);
end;


并在运行时分配该事件:

procedure TForm1.CreateMachine;
var
  Machine: TMachine;
begin
  Machine := TMachine.Create(Self);
  Machine.SetBounds(...);
  Machine.OnMouseEnter := MachineMouseEnter;
  Machine.Parent := Self;
end;

procedure TForm1.MachineMouseEnter(Sender: TMachine);
begin
  Label2.Caption := Sender.Name;
  Label3.Caption := Sender.Number;
end;

10-05 22:15