2

私は、Delphi をよりよく理解するための演習を考え出しました。私がどうしても知りたいことがすべて含まれています。Graphics32 コンポーネント (TRect、TPaintBox32 など) と Borland Delphi 7 を使用しています。

エクササイズ。プログラムのメイン フォームに四角形 (色、サイズ、画面上の位置などのパラメーターを事前に設定) を描画できるクラス Square (できれば、プログラムのメイン フォームとは異なる .pas ファイル) を記述します。いくつかの正方形をダブルクリックすると、その色がランダムな色に変わります。いくつかの正方形をクリックしたままにすると、クリックを離すまでマウスでこの正方形を移動できるはずです。

私の見方: プログラムのメイン フォームでは、Square の配列を作成し、残りは Square クラスのメソッドによって行われます。しかし、これが可能かどうかはわかりませんか?正方形の描画、クリックの処理は非常に問題があるように思えます。Square クラスには別のフォーム (.dfm ファイル) が必要ですか?

大変助かりました。

編集: 正方形の中心とその境界線は異なる色にする必要があります。また、枠の色で正方形の真ん中に横線を入れるといいでしょう。

EDIT2:あなたのヒントを私のプログラムに適用する方法がわかりません。たぶん、いくつかのコードで私を助けるのがより簡単になるでしょう。

ここでは、茶色の動きをシミュレートできる正方形を表すクラス Box を用意しています。

unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, GR32, GR32_Image, ExtCtrls, StdCtrls;

type
  Box = class

  private
    speed:TTimer;
    liveTime:TTimer;
    isAlive:boolean;
    rect:TRect;
    live:integer;
  public
    //procedure PaintBox321PaintBuffer(Sender: TObject);
    procedure liveTimeTimer(Sender: TObject);
    procedure speedTimer(Sender: TObject);
    function color():TColor32;
    constructor Create();
  end;

implementation

  constructor Box.Create();
    var x,y:integer;
  begin
    x:=random(900); y:=random(420);
    rect:=MakeRect(x,y,x+30,y+30);
   isAlive:=true; live:=random(26)+5;

    liveTime := TTimer.Create(nil);
    speed := TTimer.Create(nil);
    liveTime.interval:=1000;
    speed.interval:=live*100;
    liveTime.OnTimer := liveTimeTimer;
    speed.OnTimer := speedTimer;
  end;

  {
  procedure Box.PaintBox321PaintBuffer(Sender: TObject);
  begin
    if isAlive then begin
      PaintBox321.Buffer.Clear(Color32(255,255,255,125));
      PaintBox321.Buffer.FillRectS(rect, color());
    end;
  end;
  }

  procedure Box.liveTimeTimer(Sender: TObject);
  begin
    if isAlive then begin
      live:=live-1;
      if live=0 then isAlive:=false;
    end;
  end;

  procedure Box.speedTimer(Sender: TObject);
  begin
    if isAlive then begin
      OffsetRect(rect, 3*(random(3)-1), 3*(random(3)-1));
      speed.interval:=live*100;
      //PaintBox321.Repaint;
    end;
  end;

  function Box.color():TColor32;
  begin
    color:=Color32(255-live*5,255-live*5,255-live*5,125);
  end;
end.

そして、メイン フォーム コード: unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, GR32, GR32_Image, ExtCtrls, StdCtrls, Unit2;

type
  TForm1 = class(TForm)
    PaintBox321: TPaintBox32;
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure PaintBox321PaintBuffer(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1:TForm1;
  Boxes:array of Box;
  BoxesNumber:integer;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  randomize;
  BoxesNumber:=-1;
end;

procedure TForm1.PaintBox321PaintBuffer(Sender: TObject);  
begin
  PaintBox321.Buffer.Clear(Color32(255,255,255,125));
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  BoxesNumber:=BoxesNumber+1;
  SetLength(Boxes, BoxesNumber+1);
  Boxes[BoxesNumber]:=Box.Create();
end;

end.

読んでください、とても簡単です。コーディングの仕方がわからない、描画を担当するフラグメントにコメントしました。ここでクリックと描画ボックスを処理する方法を本当に知りたいです。

4

2 に答える 2

6

開始するためのいくつかの提案:

  • 正方形に必要なコントロールの種類を自問してください。回答には VCL の知識が少し必要ですが、最も明白なコントロールを検討し、エディターで ctrl を押しながらクリックして、一致する先祖を見つけてください。(ヒント:TShapeすでに絵を描いていますが、私は使いません。)
  • あなたは正しいSquare の配列を使用し、残りは Square クラスのメソッドによって行われます
  • いいえ、TSquareクラスはフォームを意識する必要はありませんし、フォームであるべきでもありません。そのような正方形の親を割り当てるとうまくいきます。
  • すべてのコントロール (つまり、TControl クラスの祖先) は、マウス イベント/処理をサポートします。(ヒント: オーバーライドMouseMoveDblClick.)
  • Graphics32 ライブラリは必ずしも必要ではありません。
  • TSquareクラスをコンポーネントとして登録する必要はありませんが、Delphi ヘルプの「Component Writer's Guide」を(部分的に)読むことを強くお勧めします
于 2012-05-02T16:54:30.977 に答える
1

わかりました、あなたは初心者のようです、これは長方形(正方形)を描く方法です

http://docwiki.embarcadero.com/CodeSamples/en/Rectangle_%28Delphi%29

それを移動する方法

http://docwiki.embarcadero.com/CodeSamples/en/OnMouseMove_%28Delphi%29

これが始まりです。これを行うことができるクラスがある場合、車輪を再発明する必要はないと思います。代わりに、すでに定義されているこれらのクラスの適合性を調べることができます。

Delphi のグラフィックスから始めます - http://delphi.about.com/od/graphics/Delphi_Graphics_Programming.htm

また、基本 (dfm ファイルとは何かなど) を理解するために、Embarcadero wiki やその他の初心者用資料を参照することをお勧めします。

あなたの大きな問題はイベントを作成する方法であるように思われるため、この質問は Delphi イベント処理、独自のイベントを作成する方法に役立つ場合があります

于 2012-05-02T17:33:09.507 に答える