| Then begin | | 32 | Interface | | 34 | 36 | Implementation | |

© um.co.ua -

Implementation

  1. Implementation
  2. Implementation

usesControlUnit;

{& R * .DFM}

rackBar , . , .

procedureTLifeForm.FillBtnClick (Sender: TObject);
begin
RandomCells;
DrawCells;
end;

procedureTLifeForm.SpeedTrkBarChange (Sender: TObject); { }
begin
MainForm.LifeTimer.Interval: = SpeedTrkBar.Position;
end;
procedureTLifeForm.EditSpBtnClick (Sender: TObject); { / }
begin
MainForm.LifeTimer.Enabled: = not EditSpBtn.Down;
end;

, OnMouseDown LifeImg.

procedureTMainForm.LifeImgMouseDown (Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
ifLifeForm.EditSpBtn.Downthen
A [X div10, Y div10]: = not A [X div10, Y div10];
DrawCells;
end;

uses LifeUnit.

.

83


.

16. ', . , .

. , file of integer - integer; file of TLifeCells - TLifeCells. . .

AssignFile(Var F; FileName: string) - ' F ' . .

Reset(F) - , ' F, - .

Rewrite(F) - , F.

Read(F, V) - , ' F, V. , ( integer 4 ).

Write(F, V) - , ' F, V. .

Seek(F, N: LongInt) - N, N = 0.

CloseFile(Var F) - F.

TFileCells:

{Private declaration}

Public

{Public declaration}

End;

Const

XSize = 40;

YSize = 25;

Type

TFileCells = array[0 .. XSize - 1, 0 .. YSize - 1] ofboolean;

Var

MainForm: TMainForm;

A: TFileCells;

OnClick LifeOpen . , ' , A, MainUnit .

procedureTLifeForm.LifeOpenBtnClick (Sender: TObject);
var
F: file ofTLifeCells;
begin
ifLifeOpenDlg.Execute then
ifFileExists (LifeOpenDlg.FileName)then
begin
AssignFile (F, LifeOpenDlg.FileName);
Reset (F);
Read (F, A);
CloseFile (F);
DrawCells;
end;
end;

, A.

, , . , ; , , . , ; , . .


procedureTLifeForm.LifeSaveBtnClick (Sender: TObject);
var
F:file ofTLifeCells;
begin
ifLifeSaveDlg.Execute then
begin
AssignFile (F, LifeSaveDlg.FileName);
if notFileExists (LifeSaveDlg.FileName) then
Rewrite (F)
else ifMessageDlg ( '?', MtWarning, [mbYes, mbNo], 0) = mrYes then
Reset (F)
else begin
CloseFile (F);
exit;
end;
Write (F, A);
CloseFile (F);
end;
end;

. ³ , var F: file; ( of ...), - Reset (F, 1) - ( 128), BlockRead, BlockWirite(Var F: File; var Buf; Count: Integer), Buf - , ( ) , Count - ( Reset). (, a: integer; b: boolean; c: TLifeCells; d: string [20]), .

:

Rewrite (F, 1);
BlockWrite (F, a, SizeOf (a));
BlockWrite (F, b, SizeOf (b));
BlockWrite (F, c, SizeOf (c));
BlockWrite (F, d, SizeOf (d));
CloseFile (F);

, :

Reset (F, 1);
BlockRead (F, a, SizeOf (a));
BlockRead (F, b, SizeOf (b));
BlockRead (F, c, SizeOf (c));
BlockRead (F, d, SizeOf (d));
CloseFile (F);

17. , , . .

- , , , ShowModal . , , , . . ModalResult mrNone.

, ColorUnit. Shape (CellShape: TShape, FieldShape: TShape), (ModalResult = mrOk), (ModalResult = mrCancel).

84


. , .

ColorForm: TColorForm

BorderStyle = bsToolWindoiw

Caption =

Position = poScreenCenter

Shape . .

ColorUnit - FieldShape CellShape:

procedureTColorForm.FieldShapeMouseDown (Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
MainForm.FieldColorDlg.Execute;
FieldShape.Brush.Color: = MainForm.FieldColorDlg.Color;
end;

procedureTColorForm.CellShapeMouseDown (Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
MainForm.CellColorDlg.Execute;
CellShape.Brush.Color: = MainForm.CellColorDlg.Color;
end;

" " .

LifeUnit SetColorBtn:

procedureTLifeForm.ColorBtnClick (Sender: TObject);
var
C1, C2: TColor;
begin
C1: = MainForm.FieldColorDlg.Color;
C2: = MainForm.CellColorDlg.Color;
ColorForm.FieldShape.Brush.Color: = C1;
ColorForm.CellShape.Brush.Color: = C2;
ifColorForm.ShowModal = mrCancelthen
begin
MainForm.FieldColorDlg.Color: = C1;
MainForm.CellColorDlg.Color: = C2;
end;
end;

ColorForm - . "", . C1 C2.

18. . , .

. , . - .

procedureTControlForm.FormCloseQuery (Sender: TObject; var CanClose: Boolean);
begin
ifMessageDlg ( ' ?', MtWarning,
[MbYes, mbNo], 0) = mrYes then MainForm.Close
elseCanClose: = false;
end;

ֳ . , .

.

85


19. ϳ' . :

  1. .
  2. , , interface, , , uses.
  3. ' ignotum per ignotius. .
  4. OnCreate , .
  5. , . .
  6. ij: ShowModal, - ModalResult .
  7. Ctrl-F12 Shift-F12. - - F12.

1



Implementation | , Delphi