窗体上显示200个正方形(shape),每个正方形都能够响应OnMouseUp事件,比如在OnMouseUp事件中变成圆形(shape)
该怎么实现,请高手给点思路或算法。目前我用数组创建了200个正方形,a : array[1..200] of Tshape,用a[i]表示。

解决方案 »

  1.   


    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, ExtCtrls, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
        procedure ShapeMouseUp(Sender: TObject; Button: TMouseButton;
          Shift: TShiftState; X, Y: Integer);
        function CreateShape(Left, Top, Width, Height: Integer): TShape;
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}function TForm1.CreateShape(Left, Top, Width, Height: Integer): TShape;
    var
      Shape: TShape;
    begin
      Shape := TShape.Create(Self);
      Shape.Left := Left;
      Shape.Top := Top;
      Shape.Width := Width;
      Shape.Height := Height;
      Shape.OnMouseUp := ShapeMouseUp;
      Shape.Parent := Self;
    end;procedure TForm1.ShapeMouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    begin
      with TShape(Sender) do
      if Shape = stRectangle then
        Shape := stEllipse
      else
        Shape := stRectangle;
    end;
    //这里只建立了6个对象,你可以任意扩充
    procedure TForm1.Button1Click(Sender: TObject);
    var
      a: array[0..5] of Tshape;
      i: Integer;
    begin
      for i := 0 to 5 do
        a[i] := CreateShape(i * 50, 10, 50, 50);
    end;end.
      

  2.   

    如果对动态对象不再操作,a: array[0..5] of Tshape可以不定义,直接给定对象位置和大小就行了:
      for i := 0 to 5 do
        CreateShape(i * 50, 10, 50, 50);
      

  3.   

    补充:函数CreateShape最后的返回语句漏掉了:
      Result := Shape;
      

  4.   

    谢谢 maozefa但语句 Shape.OnMouseUp := ShapeMouseUp 编译通不过,
    提示Undeclared identifier: 'ShapeMouseUp'我用的DELPHI7还请赐教
      

  5.   

    搞定了, maozefa ,能遇到你真是太幸运了!!!!非常感谢!!!