在鼠标上输入TShape

时间:2013-01-28 11:22:07

标签: delphi delphi-xe2

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

1 个答案:

答案 0 :(得分: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;