1

我有一个 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
4

1 回答 1

5

添加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;
于 2013-01-28T11:33:56.650 回答