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