菜鸟请教《delphi4编程技术内幕》书中的一个问题 ( 积分: 50 )

  • 主题发起人 主题发起人 追月
  • 开始时间 开始时间

追月

Unregistered / Unconfirmed
GUEST, unregistred user!
原代码如下:

///////////////////////////////////////
// Name: Main.pas
// Project: DockableForm
// Author: Copyright (c) 1998 by Charlie Calvert
//
unit Main;

{------------------------------------------------------------------------------
This example is designed to show how you can dock
forms inside a page control.
------------------------------------------------------------------------------}
interface

uses
Windows, Messages, SysUtils,
Classes, Graphics, Controls,
Forms, Dialogs, StdCtrls,
StdActns, ActnList, Menus,
ComCtrls, ToolWin, ExtCtrls,
Gauges;

const
MAX = 3;

type
TMyGauge = class(TGauge)
private
FTimer: TTimer;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Timer1Timer(Sender: TObject);
end;

TForm1 = class(TForm)
PageControl1: TPageControl;
MainMenu1: TMainMenu;
File1: TMenuItem;
ShowForms1: TMenuItem;
N1: TMenuItem;
Exit1: TMenuItem;
Help1: TMenuItem;
procedure Exit1Click(Sender: TObject);
procedure ShowForms1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Help1Click(Sender: TObject);
private
{ Private declarations }
FGauge: array [0..MAX] of TMyGauge;
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

uses
DockWindow1, DockWindow2;

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
var
i: Integer;
begin
Randomize;
for i := 0 to MAX do begin
FGauge := TMyGauge.Create(Self);
FGauge.Parent := Self;
FGauge.Visible := True;
FGauge.Left := (Width div 6) * (i + 1);
FGauge.Top := 8;
FGauge.Color := RGB(Random(255), Random(255), Random(255));
FGauge.ForeColor := RGB(Random(255), Random(255), Random(255));
end;
PageControl1.Top := FGauge[0].Height + 5;
PageControl1.Align := alBottom;
end;

procedure TForm1.FormDestroy(Sender: TObject);
var
i: Integer;
begin
for i := 0 to MAX do
FGauge.Free;
end;

procedure TForm1.Exit1Click(Sender: TObject);
begin
Close;
end;

procedure TForm1.ShowForms1Click(Sender: TObject);
begin
Form2.Show;
Form3.Show;
end;

{ TMyGauge }

constructor TMyGauge.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FTimer := TTimer.Create(Self);
FTimer.OnTimer := Timer1Timer;
FTimer.Interval := 10;
Width := 75;
Height := 75;
Kind := gkPie;
end;

destructor TMyGauge.Destroy;
begin
FTimer.Enabled := False;
FTimer.Free;
inherited Destroy;
end;

procedure TMyGauge.Timer1Timer(Sender: TObject);
var
i: Integer;
begin
Progress := Progress + 1;
if Progress >= 100 then begin
Progress := 0;
for i := 0 to MAX do begin
Color := RGB(Random(255), Random(255), Random(255));
ForeColor := RGB(Random(255), Random(255), Random(255));
end;

{这里的for i:=0 to max和上面的有什么不同,我把这一段语句删除了,好象程序也能正常运行。还有就是为什么要用const max=3,直接赋值给语句不就行了吗?
如:for i:=0 to 3 do }

end;
end;

procedure TForm1.Help1Click(Sender: TObject);
const
S = 'Press the menu item to create two forms. Then ' +
'dock the forms in the page control displayed at the bottom ' +
'of the main form.';
begin
ShowMessage(S);
end;

end.
 
原代码如下:

///////////////////////////////////////
// Name: Main.pas
// Project: DockableForm
// Author: Copyright (c) 1998 by Charlie Calvert
//
unit Main;

{------------------------------------------------------------------------------
This example is designed to show how you can dock
forms inside a page control.
------------------------------------------------------------------------------}
interface

uses
Windows, Messages, SysUtils,
Classes, Graphics, Controls,
Forms, Dialogs, StdCtrls,
StdActns, ActnList, Menus,
ComCtrls, ToolWin, ExtCtrls,
Gauges;

const
MAX = 3;

type
TMyGauge = class(TGauge)
private
FTimer: TTimer;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Timer1Timer(Sender: TObject);
end;

TForm1 = class(TForm)
PageControl1: TPageControl;
MainMenu1: TMainMenu;
File1: TMenuItem;
ShowForms1: TMenuItem;
N1: TMenuItem;
Exit1: TMenuItem;
Help1: TMenuItem;
procedure Exit1Click(Sender: TObject);
procedure ShowForms1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Help1Click(Sender: TObject);
private
{ Private declarations }
FGauge: array [0..MAX] of TMyGauge;
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

uses
DockWindow1, DockWindow2;

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
var
i: Integer;
begin
Randomize;
for i := 0 to MAX do begin
FGauge := TMyGauge.Create(Self);
FGauge.Parent := Self;
FGauge.Visible := True;
FGauge.Left := (Width div 6) * (i + 1);
FGauge.Top := 8;
FGauge.Color := RGB(Random(255), Random(255), Random(255));
FGauge.ForeColor := RGB(Random(255), Random(255), Random(255));
end;
PageControl1.Top := FGauge[0].Height + 5;
PageControl1.Align := alBottom;
end;

procedure TForm1.FormDestroy(Sender: TObject);
var
i: Integer;
begin
for i := 0 to MAX do
FGauge.Free;
end;

procedure TForm1.Exit1Click(Sender: TObject);
begin
Close;
end;

procedure TForm1.ShowForms1Click(Sender: TObject);
begin
Form2.Show;
Form3.Show;
end;

{ TMyGauge }

constructor TMyGauge.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FTimer := TTimer.Create(Self);
FTimer.OnTimer := Timer1Timer;
FTimer.Interval := 10;
Width := 75;
Height := 75;
Kind := gkPie;
end;

destructor TMyGauge.Destroy;
begin
FTimer.Enabled := False;
FTimer.Free;
inherited Destroy;
end;

procedure TMyGauge.Timer1Timer(Sender: TObject);
var
i: Integer;
begin
Progress := Progress + 1;
if Progress >= 100 then begin
Progress := 0;
for i := 0 to MAX do begin
Color := RGB(Random(255), Random(255), Random(255));
ForeColor := RGB(Random(255), Random(255), Random(255));
end;

{这里的for i:=0 to max和上面的有什么不同,我把这一段语句删除了,好象程序也能正常运行。还有就是为什么要用const max=3,直接赋值给语句不就行了吗?
如:for i:=0 to 3 do }

end;
end;

procedure TForm1.Help1Click(Sender: TObject);
const
S = 'Press the menu item to create two forms. Then ' +
'dock the forms in the page control displayed at the bottom ' +
'of the main form.';
begin
ShowMessage(S);
end;

end.
 
定义常量
const MAX=3;
是为了便于维护,如果写成“硬码”,将来需要更改的时候,可能导致更改不全或更改错误的问题。
 
谢谢,但第二个语句循环for i:=0 to Max和上面的功能有什么不同,我把这一段语句删除了,好象程序也能正常运行。
 
for i := 0 to MAX do begin
Color := RGB(Random(255), Random(255), Random(255));
ForeColor := RGB(Random(255), Random(255), Random(255));
end;
是把前景色和背景色随机产生三次并去最后一次的结果,没有什么太特别的意义
 
接受答案了
 
这是本好书啊!!好好研究
 
后退
顶部