通过下面的函数生成了一组Button控件:var ButtonCount:Integer;
var tb:Array [0..100] of TButton;function TForm1.CreateButton(strCaption:String;x,y:Integer):TButton;
var temp:TButton;
begin
    temp:=Tbutton.Create (ScrollBox1);
    temp.Parent := ScrollBox1;
    temp.Caption := strCaption;
    temp.Font.Color := clWhite;
    temp.Left := x;
    temp.Top := y;
end;procedure TForm1.ScrollBox1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
if ButtonCount=99 then begin
     showmessage ('无法创建新的节点!');
     exit;
    end;
if Button=mbLeft then begin
    tb[ButtonCount]:=CreateButton('按钮'+IntToStr(ButtonCount),x,y);
     Inc(ButtonCount);
    end;
end;现在要求在每个动态生成的Button控件中响应各自的MouseDown MouseUp MouseMove事件,该如何做呢?

解决方案 »

  1.   

    在Form的on create事件中
    Btn:TButton;
    begin
      Btn:=TButton.create(self);
      Btn.onmousedown:=BtnMouseDown;
    end;
    procedure TForm1.BtnMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    begin
    end;
      

  2.   

    在你的CreateButton函数中加上:
    temp.OnMouseDown:=BMouseDown;
    temp.OnMouseUp:=BMouseUp;
    temp.OnMouseMove:=BMouseMove;其中BMouseDown,BMouseUp,BMouseMove为自定义的事件函数。
      

  3.   

    注意:
    procedure  BtnMouseDown/MouseUp/MouseMove(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    需在Type中声明!