delphi动态创建了一组tspeedbutton按钮如何给这些按钮写上OnMouseDown,OnMouseLeave,onClick等事件呢?

解决方案 »

  1.   

    如下:
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, Buttons;type
      TForm1 = class(TForm)
        Button1: TButton;
        Memo1: TMemo;
        procedure Button1Click(Sender: TObject);
        procedure FormClose(Sender: TObject; var Action: TCloseAction);
      private
        { Private declarations }
        ASpeedBtn: TSpeedButton;
        procedure ASpeedBtnClick(Sender: TObject);
        procedure ASpeedBtnMouseDown(Sender: TObject; Button: TMouseButton;
          Shift: TShiftState; X, Y: Integer);
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.ASpeedBtnClick(Sender: TObject);
    begin
      Memo1.Lines.Add('ASpeedBtnClick');
    end;procedure TForm1.ASpeedBtnMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    begin
      Memo1.Lines.Add('ASpeedBtnMouseDown');
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
      if not Assigned(ASpeedBtn) then
      begin
        ASpeedBtn := TSpeedButton.Create(Self);
        ASpeedBtn.Parent := Self;
        ASpeedBtn.SetBounds(100, 100, 100, 30);
        ASpeedBtn.Caption := '123';
        ASpeedBtn.OnMouseDown := ASpeedBtnMouseDown;
        //ASpeedBtn.OnMouseLeave := ; //SpeedButton没有这个事件
        ASpeedBtn.OnClick := ASpeedBtnClick;
      end;
    end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
    begin
      if Assigned(ASpeedBtn) then
        FreeAndNil(ASpeedBtn);
    end;end.
      

  2.   

    高版本的delphi,button 都有OnMouseLeave,speedbutton也不例外。好像d2007(2007都不应该叫高版本了,发布也都7年历史了)开始就有了。
    反正就那个意思,LZ俺LS的方法照猫画虎就行了
      

  3.   

    事件动态赋值ASpeedBtn.OnMouseDown := ASpeedBtnMouseDown就可以,具体参考2楼的代码,很详细。