Glen Morse
Glen Morse

Reputation: 2593

On Mouse Enter TShape

I have a TMachine class, that is a TShape class

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.

How would i add onmouseenter code for this? So when the shape is added during run time it will have its own on mouse enter code. Something like this, I know this wont work.. but maybe it will show you what i am looking to do? So when i create a TMachine, i would pass the name and number to this procedure and it would make the onmouseenter procedure update with the name/number i sent it.

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

Upvotes: 1

Views: 1400

Answers (1)

NGLN
NGLN

Reputation: 43649

Add an OnMouseEnter event:

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;

And assign that event at runtime:

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;

Upvotes: 5

Related Questions