Hướng dẫn lập trình Delphi - Quyển 2: Những ví dụ trong lập trình Delphi
Bạn cần phải đọc quyển I trước khi đọc những ví dụtrong quyển này. Quyển sách này giành
cho bạn, hãy bấm và bấm, bạn sẽthông thạo Delphi.
Hướng dẫn này sẽgiúp bạn điều khiển Delphi. Có vài chương trình ví dụcùng với những
nhiệm vụtương tự. Bạn hãy thửlàm các nhiệm vụ đó dựa vào các ví dụ.
Tất cảcác bài làm của các ví dụ, bạn nên bỏvào thưmục của mình.
D:\Examples\ME\
Nếu bạn muốn “phẻ” thì trên Website của tui có các chương trình ví dụnày (bao gồm tất cả
các file cần thiết)
uch as
a messageBeep(0) or a picture being drawn. }
unit Unit1;
interface
uses
SysUtils, WinTypes,
WinProcs, Messages,
Classes, Graphics,
Controls, Forms,
Dialogs, StdCtrls,
ExtCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Timer1: TTimer;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
count : Integer;
procedure doSomething;
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.doSomething;
begin
label1.caption := 'Hello'
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
count := 5;
Những ví dụ về lập trình bằng Delphi; Biên dịch bởi Lê Khắc Như; website:
2/21/04
Le Khac Nhu --- Website : Page 20 2/21/2004
Reviewed by NGUYEN, Ba Thanh
Distributed by Diễn đàn Tin học –
label1.caption := ''
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
count := 0;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
if count 0 then
begin
count := count - 1;
if count = 0 then
begin
doSomething
end
end;
label2.caption := intToStr(count);
if label3.caption = 'Tick' then
label3.caption := 'Tock'
else
label3.caption := 'Tick'
end;
end.
Những ví dụ về lập trình bằng Delphi; Biên dịch bởi Lê Khắc Như; website:
2/21/04
Le Khac Nhu --- Website : Page 21 2/21/2004
Reviewed by NGUYEN, Ba Thanh
Distributed by Diễn đàn Tin học –
12) Tạo chiếc máy tính đơn giản
{ EXAMPLE Đây là ví dụ về chiếc máy tính đơn giản.
Nó tính được Cộng và tính Trừ.
TASK Học chương trình để hiểu nó và sau đó
Thêm vào máy tính thành phần Times, Divide,
Square root và nếu muốn bạn làm từ X tới Y
Và các hàm khó hơn nữa.
}
{ ================================================================ }
unit Mainform;
interface
uses
SysUtils, WinTypes, WinProcs,
Messages, Classes, Graphics,
Controls, Forms, Dialogs,
StdCtrls;
{ ================================================================ }
type
modeType = (none, plus, minus); { Add more modes here. }
TForm1 = class(TForm)
EditDisplay: TEdit;
ButtonPlus: TButton;
ButtonMinus: TButton;
ButtonEquals: TButton;
ButtonClear: TButton;
procedure FormCreate(Sender: TObject);
procedure ButtonClearClick(Sender: TObject);
procedure ButtonPlusClick(Sender: TObject);
procedure ButtonEqualsClick(Sender: TObject);
procedure ButtonMinusClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations } { You type these in. }
mode : modeType;
resultSoFar : Real;
procedure calculate;
end;
{ ================================================================ }
var
Form1: TForm1;
implementation
{$R *.DFM}
{ ================================================================ }
Những ví dụ về lập trình bằng Delphi; Biên dịch bởi Lê Khắc Như; website:
2/21/04
Le Khac Nhu --- Website : Page 22 2/21/2004
Reviewed by NGUYEN, Ba Thanh
Distributed by Diễn đàn Tin học –
{ ================================================================ }
procedure TForm1.Calculate;
begin
{ Do a calcualtion based on the mode. }
if mode = none then
begin
resultSoFar := strToFloat(editDisplay.text);
end
else if mode = plus then
begin
resultSoFar := resultSoFar + strToFloat(editDisplay.text);
end
else if mode = minus then
begin
resultSoFar := resultSoFar - strToFloat(editDisplay.text);
end;
{ Refresh the answer / input display. }
editDisplay.text := floatTOStr(resultSoFar);
editDisplay.setFocus;
editDisplay.selectAll;
end;
{ ================================================================ }
{ Ensure that the initial values are OK. }
{ This happens when the form is created. }
procedure TForm1.FormCreate(Sender: TObject);
begin
resultSoFar := 0.0;
mode := none;
end;
{ ================================================================ }
{ CLEAR BUTTON }
{ Clear the displays. }
{ Set the result so far to zero. }
procedure TForm1.ButtonClearClick(Sender: TObject);
begin
mode := none;
resultSoFar := 0.0;
editDisplay.setFocus;
editDisplay.clear;
end;
{ ================================================================ }
{ PLUS BUTTON }
{ Perform the calculation so far and set the mode to PLUS. }
procedure TForm1.ButtonPlusClick(Sender: TObject);
begin
calculate;
mode := plus;
end;
{ ================================================================ }
Những ví dụ về lập trình bằng Delphi; Biên dịch bởi Lê Khắc Như; website:
2/21/04
Le Khac Nhu --- Website : Page 23 2/21/2004
Reviewed by NGUYEN, Ba Thanh
Distributed by Diễn đàn Tin học –
{ =================================================================}
{ EQUALS BUTTON }
{ Perform the calculation so far and set the mode to NONE. }
procedure TForm1.ButtonEqualsClick(Sender: TObject);
begin
calculate;
mode := none;
end;
{ ================================================================ }
{ MINUS BUTTON }
{ Perform the calculation so far and set the mode to MINUS. }
procedure TForm1.ButtonMinusClick(Sender: TObject);
begin
calculate;
mode := minus;
end;
{ ================================================================ }
{ ADD BUTTONS AND PROCEDURES FOR Times, Divide, Square root, Etc. }
end.
{ ================================================================ }
Những ví dụ về lập trình bằng Delphi; Biên dịch bởi Lê Khắc Như; website:
2/21/04
Le Khac Nhu --- Website : Page 24 2/21/2004
Reviewed by NGUYEN, Ba Thanh
Distributed by Diễn đàn Tin học –
13) Ví dụ về đồ họa Fractals
Fractals là một kiểu đồ họa phức tạp không giống với bất kì kiểu đồ họa
nào. That means you can zoom in on the picture and the small scale
looks the same as the large scale picture. This program produces a
simple fractal. Nice fern or tree shapes can be produced by modifying
the program below. By introducing a small degree of randomness, the
shapes can look surprisingly like real trees or ferns.
{
EXAMPLE
A much more
complex fractal
graphic.
TASK
Tinker with the
maths to get
different shaped
fractals.
}
unit Unit1;
interface
uses
SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics,
Controls, Forms, Dialogs, StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
PaintBox1: TPaintBox;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
procedure fractal(X, Y, Size : Integer);
end;
var
Form1: TForm1;
Những ví dụ về lập trình bằng Delphi; Biên dịch bởi Lê Khắc Như; website:
2/21/04
Le Khac Nhu --- Website : Page 25 2/21/2004
Reviewed by NGUYEN, Ba Thanh
Distributed by Diễn đàn Tin học –
implementation
{$R *.DFM}
{ Thủ tục này là ĐỆ QUY. Nó có nghĩa là sử dụng lại chính nó đề làm
công việc đó. Những thủ tục Đệ Quy có thể chạy ra khỏi điều khiển
nếu thiết kế sai. Trước những thủ tục của chính nó, nó phải kiểm
tra xem có đúng không rồi mới tiếp tục. Trong ví dụ này,
những hình dạng nhỏ và nhỏ hơn cho đến khi kích thước nó là hai điểm.
Đó là các điểm nhỏ bởi vì khi trình bày ra màn hình, nó không
hiển thị các bức ảnh được. }
procedure TForm1.fractal(X, Y, Size : Integer);
begin
paintBox1.Canvas.MoveTo(X, Y);
paintBox1.Canvas.LineTo(X, Y - Size);
paintBox1.Canvas.MoveTo(X - Size div 2, Y - Size div 2);
paintBox1.Canvas.LineTo(X + Size div 2, Y - Size div 2);
if Size > 2 then
begin
fractal(X - Size div 2, Y - Size div 2, Size div 2);
fractal(X + Size div 2, Y - Size div 2, Size div 2)
end
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
fractal(200, 200, 150)
end;
end.
Những ví dụ về lập trình bằng Delphi; Biên dịch bởi Lê Khắc Như; website:
2/21/04
Le Khac Nhu --- Website : Page 26 2/21/2004
Reviewed by NGUYEN, Ba Thanh
Distributed by Diễn đàn Tin học –
Ví dụ vẽ Cricle, Spiral (Vòng tròn và Xoắn óc)
{ EXAMPLE Đồ họa thú vị nào đó được trình bày ở đây.
Những vòng tròn và các đường xoắn óc được vẽ bằng các
điểm được hiển thị trên màn hình.
TASK Vẽ những hình học khác như sóng sin và đồ thị bình phương,
lập phương và các hàm khác. }
unit Unit1;
interface
uses
SysUtils, WinTypes, WinProcs,
Messages, Classes, Graphics,
Controls, Forms, Dialogs,
StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
PaintBox1: TPaintBox;
Panel1: TPanel;
ButtonCircle: TButton;
ButtonSpiral: TButton;
procedure ButtonCircleClick(Sender: TObject);
procedure ButtonSpiralClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.ButtonCircleClick(Sender: TObject);
var X, Y, theta : Integer;
begin
for theta := 0 to 359 do { Bước độ một }
begin
X := 110 + round(100 * sin(2 * PI * theta / 360));
Y := 110 + round(100 * cos(2 * PI * theta / 360));
paintBox1.Canvas.Pixels[X, Y] := clRed;
end
end;
Những ví dụ về lập trình bằng Delphi; Biên dịch bởi Lê Khắc Như; website:
2/21/04
Le Khac Nhu --- Website : Page 27 2/21/2004
Reviewed by NGUYEN, Ba Thanh
Distributed by Diễn đàn Tin học –
procedure TForm1.ButtonSpiralClick(Sender: TObject);
var X, Y, theta : Integer;
begin
for theta := 0 to 3600 do { Bước độ một }
begin
X := 110 + round((theta div 36) * sin(2 * PI * theta / 360));
Y := 110 + round((theta div 36) * cos(2 * PI * theta / 360));
paintBox1.Canvas.Pixels[X, Y] := clYellow;
end
end;
end.
Tác giả : Lê Khắc Như ; Ngày 24 tháng 1 năm 2004
Email: laptrinh04@yahoo.ca
Website:
File đính kèm:
Hướng dẫn lập trình Delphi - Quyển 2_Những ví dụ trong lập trình Delphi.pdf

