Giáo trình Delphi

Một k ỹ n ăn g cơ bản m à n g ười lập t rìn h ph ải làm .Đây là n h iệm vụ củ a bài 2

n h ưn g ch ún g

t a ph ải t h ay đổi m àu t ừ f orm , n ó ch ọn 1 m àu m ới t h ay t h ế m àu cũ .

Xây d ựn g lại ch ươn g t rìn h t ron g bài h ọc 2 làm ch o f orm m àu đỏ. Ch o

ch ươn g t rìn h làm việc. Th ấy rằn g bạn có t h ể làm n ó m à k h ôn g cần sử dụ n g

h ướn g dẫn n ày . rồi t h ay đổi các sự k iện n h ư sau .V iệc n ày t iêu biểu để sử dụ n g

I f State m e n t (lện h I F).

pdf63 trang | Chuyên mục: Delphi | Chia sẻ: dkS00TYs | Lượt xem: 2759 | Lượt tải: 1download
Tóm tắt nội dung Giáo trình Delphi, để xem tài liệu hoàn chỉnh bạn click vào nút "TẢI VỀ" ở trên
om(49) + 1)) end;
{ -------------------------------------------------
--------------- } 
procedure TForm1.Button1Click(Sender: TObject);
Var Counter : Integer; begin For Counter := 1 to 7
Do { For all seven lottery numbers DO } Begin
MyShow(Counter) { Display the ball } End end;
{ -------------------------------------------------
--------------- } 
procedure TForm1.Timer1Timer(Sender: TObject); {
This procedure runs once every ten seconds. It
displays the word Lottery. It also changes the
colour of the text. } begin Image1.Canvas.Pen.Color
:= clLime; Image1.Canvas.Brush.Color := clLime;
if Image1.Canvas.Font.Color = clRed then begin
Image1.Canvas.Font.Color := clYellow end else
begin
Image1.Canvas.Font.Color := clRed end;
Image1.Canvas.Font.Size := 24; 
Image1.Canvas.TextOut(Image1.Width div 2, 100, '
Lottery ') 
end ; { ----------------------------------------
------------------------ } procedure
TForm1.FormCreate(Sender: TObject);
{ This procedure runs when the program first
starts up. Randomize causes the random number
sequence to start on a different random number on
each run. } begin randomize end;
{ ----------------------------------------------
------------------ } end. { -----------------------
----------------------------------------- }
Le Khac Nhu --- Website :  Page 18 2/21/2004 
11) Ví dụ về Time Delay (Trì hoãn thời gian)
{ EXAMPLE This program shows how to do a time
delay using a Delphi System timer. When you press
the button, after five seconds, the word Hello
appears. TASK Write a program that counts from one
to ten at half second time intervals and displays
the count. When it reaches ten, make something
happen such 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;
Le Khac Nhu --- Website :  Page 19 2/21/2004 
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.
Le Khac Nhu --- Website :  Page 20 2/21/2004 
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); p r i v a t e {
Private declarations } public { Public declarations
} { You type these in. }
mode : modeType; resultSoFar : Real;
procedure calculate; 
end ; {
================================================================
} var
Form1: TForm1; implementation {$R *.DFM} {
================================================================
}
Le Khac Nhu --- Website :  Page 21 2/21/2004 
{
================================================================
} 
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;
{
================================================================
}
Le Khac Nhu --- Website :  Page 22 2/21/2004 
{
=================================================================}
{ 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. {
================================================================
}
Le Khac Nhu --- Website :  Page 23 2/21/2004 
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;
Le Khac Nhu --- Website :  Page 24 2/21/2004 
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.
Le Khac Nhu --- Website :  Page 25 2/21/2004 
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;
Le Khac Nhu --- Website :  Page 26 2/21/2004 
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: 
Le Khac Nhu --- Website :  Page 27 2/21/2004 

File đính kèm:

  • pdfGiáo trình Delphi.pdf
Tài liệu liên quan