Dynamically create controls and control events in DELPHI

In DELPHI we often have to dynamically create controls and control events. For example, we might want to create a dynamic program according to need some Tshape components to create a graphic and make it possible to complete certain operations after moving the mouse up. This generally requires three steps:

  1. Generating a class instance of the component to develop
  2. Examples pay for the initial value
  3. After use, the release of resources

    The following will be shown by an example, an example of the function is to create an instance of a dynamic MyShape a Tshape, and after MyShape title bar in the form of mouse movement, mouse, display coordinates.

The first step: the establishment of a new project, put a button on the main form BitBtn1 engineering, and add ExtCtrls uses the code in the file.

Step Two: Write in the OnClick event of the button

TForm1.BitBtn1Click Procedure (Sender: TObject);
var
  myShape: TShape;
the begin
   myShape: = TShape.Create (nil);
   with myShape do
   the begin
     the Parent: = Form1; // If there is no sentence, myShape newly created will not be displayed on the form
     Left: = 100; // position information
     Top: = 100;
     the Width: = 200 is; // size information of
     the Height: = 200 is;
   End;
End;

The third step: this time the compiler and executed, click BitBtn1, you will see the results in a new Shape, size (200 × 200) on the form, when the mouse is moved to the Shape, and not in the window body in the title bar to display any information. Very simple, because we do not process it to create a dynamic event. This is certainly not what we need. We know that if it is in the design period, the door just need to write a code in its onMouseMove event on it, that this dynamically created controls how do we do it? Statement observe TShape class, we will find it is a property of onMouseMove events, it is a type TMouseMoveEvent properties. We know that property is the interface to access the internal components of the client storage space, and the event property is a method pointer. Careful observation statement following OnMousemove property

property OnMouseMove: TMouseMoveEvent read FOnMouseMove write FOnMouseMove;

Then observe TMouseMoveEvent type, we will find it defined as follows:

TMouseMoveEvent = procedure(Sender: TObject; Shift: TShiftState; X, Y: Integer) of object;

See here I think you probably already know, we only need to create a type and TMouseMoveEvent a kind of process, and the process is assigned to onMouseMove property of a control on it.

Note: The number of arguments creation process, as well as the type and order must be declared in the same type!

Step four: just add new features to the program, modify the code. First, add the procedure declaration MyMouseMove the type definition of Form1;

type
  TForm1 = class(TForm)
    BitBtn1: TBitBtn;
    procedure BitBtn1Click(Sender: TObject);
  private
    procedure MyMouseMove(Sender: TObject; Shift: TShiftState;
    X, Y: Integer);
  public

  end;

Then complete the development of MyMouseMove write functional code, here is the title bar of the window display coordinate information of the mouse;

procedure TForm1.MyMouseMove(Sender: TObject; Shift: TShiftState;X, Y: Integer);
begin
   Form1.Caption := Format('x:%d;y:%d',[x,y]);
end;

Last Modified BitBtn1Click, the event handler assigned to the newly created onMouseMove event properties of myShape

OnMouseMove := MyMouseMove;

Step five: the compiler and run, click on the button to create the component, and then move the mouse to the newly created component, you will find that the title bar displays the current mouse coordinate information.

Note: This article was written content is relatively simple, but I hope to give beginners DELPHI people play a valuable role.

Full source code program is as follows:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls, Buttons;
type
  TForm1 = class(TForm)
    BitBtn1: TBitBtn;
    procedure BitBtn1Click(Sender: TObject);
  private
    procedure MyMouseMove(Sender: TObject; Shift: TShiftState;
    X, Y: Integer);
  public

  end;

was
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.MyMouseMove(Sender: TObject; Shift: TShiftState;X, Y: Integer);
begin
   Form1.Caption := Format('x:%d;y:%d',[x,y]);
end;

procedure TForm1.BitBtn1Click(Sender: TObject);
var
  myShape : TShape;
begin
   myShape := TShape.Create(nil);
   with myShape do
   begin
     Parent := Form1;
     Left := 100;
     Top := 100;
     Width := 200;
     Height := 200;
     OnMouseMove := MyMouseMove;
   end;
end;

end.

Guess you like

Origin www.cnblogs.com/jijm123/p/11305325.html