Sei sulla pagina 1di 36

-

Delphi

-
2001

0101,5101,0104,0105 1208 - , .

Windows-,

, , .
.

Delphi

,
2001
..
..

Delphi
Delphi .
Delphi
. ,
Delphi. Delphi , 300 .
Delphi .
.
, . , .
Windows . Windows
.
Delphi . C Delphi (RAD rapid application
development), . VCL ,
, , , , , DDE OLE.
Delphi.
,
, . Delphi
,
. ,
,
.
(Form Designer) ()
.
, 3

, .
,
. .
.
(Code Editor)
, . - Object
Pascal . .
(Component Palette)
.
(
) ,
. , .
(Object Inspector)
Properties Events.

Events , Delphi
,
:
procedure TForm1.(Sender: TObject);

begin
< Object Pascal>

end;
. .dfm .pas,
, . Delphi,
, .pas ,
.
4


.

Windows ,
.
1. SpinEdit (
Samples) : SpinEdit1
SpinEdit2.
2. StatiText (
Additional) .
3. Label ( Standard) (Label1) (Label2).
4. Button ( Standard) .
5. Object inspector
Properties, .
Caption () Label, Button
StaticText ( ) .
.
, ShowHint True ( ) Hint ( )
SpinEdit, Label Button.
6. . Object inspector
Events .
: Begin End.
SpinEdit OnChange (
). Begin End :
f1:=SpinEdit1.Value; ( SpinEdit2 1 2).
Label2.Caption:= ; ( );
5

Button1 OnClick (
) :
Sum:=f1+f2; Label2.Caption:==+ IntToStr(Sum);
( ).
Var
: f1,f2, Sum:integer.
7. . . , . . .


.
, .
1. Timer ( System)
(Timer1).
2. Progress Bar ( Win32) .
3. Label ( Standard)
(Label1).
4. Object inspector
Properties, :
Caption ()
Label1;
( Font Label1);
Interval Timer1 100, OnTimer 0,1 .
5. i : integer
Var Unit1.
6. :
OnCreate Form1. Begin End : Timer1timer(Timer1);i:=0;
6

OnTimer (10 0,1 ) :


if (i mod 100) = 0 then Label1.Caption:=TimeToStr(Now);
ProgressBar1.Position:=i;
i:=i+10; if i>100 then i:=0;
TimeToStr(Now) .
7. . .
. Gauge ( Samples), . .
2


. .
Math
, .
1. (Form1) (Button),
( MaskEdit)
(Label).
2. MaskEdit :
EditMask Input Mask Editor;
Input Mask 000.00,
, ;
Character For Blanks #,
.
3. :
OnClick( 10 ):
if i<11 then
begin
if i<10 then
Label1.Caption:=+ IntToStr(i+1)+-
else begin Label1.Caption:= ;
7

Button1.Enabled:=false; Button1.Visible:=false
end;
val(MaskEdit1.EditText,m[i],cod);
inc(i);
Label2.Caption:= Label2.Caption + + MaskEdit1.EditText;
MaskEdit1.SetFocus;end;
OnClick(
):
Label4.Caption:= floatToStr(MaxValue(m));.
OnClick:
Label4.Caption:= floatToStr(Mean(m));
4. OnCreate ,
:
i:=1;
Label1.Caption:= + IntToStr(i)+- ;
5. Form1
Math:
Uses Math;
6. Var:
i,cod:integer; m:array [1..10] of Double;
7. . .
.
. ,
. .

.
Printers
, ,
. , 2, (Button5), (PrintDialog1).
1. Uses
Printers.
2. (Button5):
8

var f:Textfile; j:byte;s:string; // ,


//
begin
if PrintDialog1.Execute then //
begin
AssignPrn(f); //
Try //
Rewrite(f); //
Writeln(f, ); //
Writeln(f,);
for j:=1 to 10 do
s:= s + +floatToStr(M[j]);
Writeln(f,s);
Writeln(f,);
s:= floatToStr(MaxValue(m));
Writeln(f,s)
finally
CloseFile(f); //
end;
end;
end;
3. ( Enabled false) .
4. : Button5.Enabled:=true;
.
5. . .
. . .
3

.

Windows, .
1. Memo ( Standartd)
(Memo1).
9

2. Button ( Standard)
(Button1)
(Button2 ).
3. OpenDialog ( Dialogs)
(OpenDialog1).
,
.
.
4. Object inspector
Properties:
Caption () Button ( ) . ;
, ShowHint True ( )
Hint ( ) .
5. Memo
Lines. String list editor. .
6. Object inspector Events.
Button1 OnClick (
) Lines Memo:
If OpenDialog1.Execute then
Memo1.Lines.LoadFromFile(OpenDialog1.FileName); (
). ( Ctrl+J, Enter) (
Ctrl+).
Button2 OnClick Lines Memo :
Memo1.Lines.SaveToFile(OpenDialog1.FileName);.
Form1 ,
OnDestroy: Close.
10

7. . , . . . .
. .
1.
OpenDialog1Close :
Caption:= +ExtractFileName(OpenDialog1.FileName);
2. Memo .
Align alClient ( alNone).
Memo.
3. Bevel ( Additional)
Align alTop.
4. Memo ,
ScrollBars ssBoth.
5.
. SaveDialog.
OnClick:
if SaveDialog1.Execute then
begin
Memo1.Lines.SaveToFile(SaveDialog1.FileName);
Caption:= +
ExtractFileName(SaveDialog1.FileName);
end;
. .
6. .

FontDialog. OnClick :
if FontDialog1.Execute then
Memo1.Font:=FontDialog1.Font;
OnApplay FontDialog
Memo1.Font:=FontDialog1.Font;
11

.
. : . ,
.
CloseQuery, :
canclose:=messagedlg (?,mtConfirmation,[mbYes,mbNo]
,0)=mrNo;
Memo Change, . . .



, ,
. : . : ,
, , ... .
: , , .
1. MainMenu ( Standard)
(MainMenu1). . , .
2. Items [Menu]
MainMenu1 [] ( ) , .
3.
.
(Hot key) &. ,
, N ( )
N1 N11 (
Object inspector).
12

4. Object inspector , ( , ..)


Events ,
OnClick.
5. :
: Memo1.CopyToClipboard;
: Memo1.CutToClipboard;
: Memo1.PasteFromClipboard;
OnClick :
n8.Enabled:=Memo1.SelLength>0; ( )
n9.Enabled:=n8.Enabled; ( )
n11.Enabled:=n8.Enabled; ( )
n10.Enabled:=clipboard.hasformat(cf_text); ( )
Enable - False.
6. Uses
. - ClipBrd.
7. . . . Memo . , . .
. . ,
Memo.
.
. .
, , . Image. :
;

.
13

, , SpeedButton , Gluph Program


Files\Common Files\Borland Shared\Images\Buttons.
OpenPictureDialog
SavePictureDialog.
Image.
. .
. .


Memo
3, ,
Memo.
1. : (Button) .
2. PrintDialog
PrintSetupDialog.
3. Printers,
Uses.
4. :
:
PrinterSetupDialog1.Execute;
:
var f:Textfile; j:integer;
begin
if PrintDialog1.Execute then
begin
AssignPrn(f);
try
Rewrite(f);
printer.canvas.Font:=Memo1.Font;
for j:=0 to Memo1.Lines.Count -1 do
Writeln(f,Memo1.Lines[j]);
finally
14

CloseFile(f);
end;
end;
end;
. .
. .
. 3 , . .



( Application Wizard)
Delphi
Application Wizard,
, .
Application Wizard File->New
New Items Projects,
Application Wizard OK. 3.
, (File Edit) Next.

( ). :
*.txt ;
*.* - .

.
Insert. Spae.
, , . Finish , . ,
.txt, Application
15

Wizard ,
.
. Memo. . . .
, Projects Win95/98 Logo Application.
, RichEdit, , Memo.
4

.

Windows, , .
1. Memo ( Standard)
(Memo1).
2. Button ( Standard) (Button1).
3. ListBox ( Standard)
(ListBox1).
4. Object inspector
Properties.
Caption ()
( Form1)
.
, ShowHint True ( ) Hint ( ) .
Memo1
Lines. String list editor
.
5. :
New New
16

Unit. Code editor Unit2, . , ,


Project -> Add to project
6. Unit2 ,
:
interface
//
// ,
//
// -
Function GetSubStr3(st:string; { }
n:integer) { }
:string; { }
implementation //
Function GetSubStr3(st:string;n:integer):string;
var
p:integer; i:integer;
begin
For i:=1 to n-1 do
begin
p:=pos( ,st);
if (p>0)
then
st:=copy(st,p+1,Length(st)-p)
else st:=;
// ,
while (pos( ,st)=1)and (length(st)>0) do
delete(st,1,1);
end;
p:=pos( ,st);
if p <> 0
then result:=copy(st,1,p-1)
else result:=st ;
end;
end.

17

7. OnClick:
var i:integer;s:string;
begin
for i:=1 to 10000 do begin
s:=Getsubstr3(Memo1.text,i);
if s= then Break
else
ListBox1.Items.Add(s);
end;
8. . .
. , : , .. , ( , ).
, ,
. .
5

o .

,
: , .
.
1. : PaintBox ( System),
RadioGroup c RadioButton ( Standard) , Label ( Standard) FG ; BG , SpinEdit
ColorGrid (
Samples). (ForeGround FG)
, (BackGround BG) .
2. SpinEdit MaxValue=20 MinValue=1, (1 20 ).
- Value 1.
18

3. Color PaintBox
clWhite .
4. Var : nt:Tpoint - ; i: integer (0 , 1 - , 2 ); f
.
5. :
OnActivate

f:=true;
OnClick c i : i:=0; i:=1; i:=2;
PaintBox: OnPaint

if f then
with SENDER AS TPaintBox, Canvas do
begin
Brush.Color:=Color;
FillRect(ClientRect);
f:=false;
end;
OnMouseDown
(
, ):
nt.x:=x; nt.y:=y;
with SENDER AS TPaintBox, Canvas do
PaintBox1.Canvas.MoveTo(x,y);
OnMouseUp
:
with SENDER AS TPaintBox, Canvas do begin
case i of
0 :PaintBox1.Canvas.LineTo(x,y) ;
1 :PaintBox1.Canvas.Ellipse(nt.x,nt.y,x,y);
2: PaintBox1.Canvas.Rectangle(nt.x,nt.y,x,y);
end;
end;
19

ColorGrid OnClick :
PaintBox1.Canvas.Pen.Color:=ColorGrid1.ForegroundColor;
PaintBox1.Canvas.Brush.Color:=ColorGrid1.BackgroundColor;
, ,
.
SpinEdit
OnChange:
PaintBox1.Canvas.Pen.Width:= SpinEdit1.Value;
6. . .
.
. PaintBox Image . Image Canvas,
, PaintBox.
PaintBox . .
6

o
, .
1. : Chart ( Additional)
Button ( Standard) .
2. .
3.
Chart Edit Chart.
Editing Chart1 Series Add Series1
TeeChartGallery Line. Title ,
( Legends ). Series1, Series2, Series3
.
4. :
OnClick , i
Var
20

With Series1 do
Begin
i:=1;While i<= 12 do begin
Add( i, IntToStr(i) , clBlue); inc(i);
Add( i-5,IntToStr(i) , clBlue );inc(i) ;
Add( 2*i,IntToStr(i) ,clBlue ) ;inc(i);
end;
end;
With Series2 do
Begin
for i:=1 to 10 do
Add( i*i, IntToStr(i) , clRed ) ;
end;
With Series3 do
Begin
for i:=1 to 10 do
Add( 100-10*i, IntToStr(i) , clGreen ) ;
end;
,
, i2 100-10*i. Add Series.
, ,
,
.
5. . .
. Chart,
. , .
.

7
,
.


,
( ).
21

1. : Animate TrackBar ( Win32), Button ( Standard) , Label (


Standard) , , .
2. TrackBar
Max=20 Min=1, Position . Frequency =1
.
3. Animate FileName:
c:\Program Files\Delphi5\Demos\Coolstuf\cool.avi
, Delphi5 (
Windows, CommonAVI c aviNone
FileName).
4. , .
5. .
File->New
File Items Dialogs, Standard Dialog.
Copy ( ),
.
OKBottomDlg Unit2.pas.
Unit1
Uses Unit2 (

Unit1).
SpinEdit (
Samples)
StaticText ( Additional) .
6. .
6.1. ( Unit2):
OnClick:
ModalResult := mrOK;
Cansel OnClick:
22

ModalResult := mrCancel;
.
6.2. ( Unit1):
OnClick:
Animate1.Repetitions:=TrackBar1.Position;
Animate1.Active:=true;
TrackBar OnChange
:
Label3.Caption:= + IntToStr(TrackBar1.Position);
, , ,
, OnClick:
If OkBottomDlg.ShowModal = mrOK then begin
TrackBar1.Min:=OkBottomDlg.SpinEdit1.Value;
TrackBar1.Max:=OkBottomDlg.SpinEdit2.Value;
Label1.Caption:=IntToStr(TrackBar1.Min);
Label2.Caption:=IntToStr(TrackBar1.Max);
Label3.Caption:= + IntToStr(TrackBar1.Position);end;
ShowModal
.
7. . .
.
. ,
. . . .
8



Windows, ,
. :
.. .
.
1. Timer ( System)
: Timer1.
23

2.
Object inspector Properties.
3. x y: integer
Var Unit1.
4. .
FormActivate
Form1. Begin End
: x:=0; y:=100;
form1.color:=clNavy{ };
timer1.Interval:=50;{ , ,
}
OnTimer :
ris(x,y,form1.color); { , }
x:=x+5;{ }
ris(x,y,clWhite); { }
if x > ClientWidth then x:=0; { }
5. implementation
procedure ris(x,y: integer; { }
color: TColor); { }
var buf: TColor;
begin
with form1.canvas do
begin
buf:=pen.Color; { }
pen.Color:=color; { }
{ ...} {}
MoveTo(x,y);{ }
LineTo(x+50,y);{ }
LineTo(x+55,y-10);
LineTo(x,y-10);
LineTo(x,y);
{ }
Rectangle(x+15,y-15,x+45,y-10);{ }
24

{ }
Rectangle(x+40,y-20,x+45,y-15);
{ }
Rectangle(x+25,y-25,x+30,y-15);
{ }
Ellipse(x+42,y-6,x+47,y-1);{() }
{ }
MoveTo(x+40,y-20);
LineTo(x+40,y-35);
pen.Color:=buf; { }
end;
end;
6. . .
.
. ,
. .
9



Windows, ,
bmp (
film.bmp ). bmp .
. :
.. . .
1. Timer : Timer1 Button1 ( Standard).
2. Object inspector
Properties:
, ;
;
Interval 100 ( ; , ).
25

3. :
const
N_KADR=5; //
4. Var Unit1:
Film: TBitMap;
// -
Kadr: TBitMap;
//
WKadr,HKadr: integer; //
CKadr: integer;
//
RectKadr: TRect;
//
Back: TBitmap;//
WorkBitMap: Tbitmap;//
5. .
FormActivate Form1:
Film:=TBitMap.Create;
Film.LoadFromFile(film.bmp);
WKadr:=Round(Film.Width/N_Kadr);
HKadr:=Film.Height;
Kadr:=TBitMap.Create;
Kadr.Transparent:=True;
Kadr.Width:=WKadr;
Kadr.Height:=HKadr;
Back:=TBitMap.Create;
Back.Width:=WKadr;
Back.Height:=HKadr;
WorkBitMap:=TBitMap.Create;
WorkBitMap.Width:=WKadr;
WorkBitMap.Height:=HKadr;
Ckadr:=0;
OnTimer :
DrawKadr;
OnClick :
Back.Canvas.CopyRect
(Bounds(0,0,WKadr,HKadr),Form1.canvas,Bounds(0,0,WKadr,HKadr));
Form1.Timer1.Enabled:=True;
6. implementation
:
procedure DrawKadr;
begin
26

WorkBitmap.Canvas.Draw(0,0,Back);//
//
RectKadr:=Bounds(WKadr*CKadr,0,WKadr,HKadr);
//
Kadr.Canvas.CopyRect(Rect(0,0,WKadr,HKadr),Film.Canvas,RectKadr);
//
WorkBitmap.Canvas.Draw(0,0,Kadr);
//
Form1.Canvas.Draw(10,10,WorkBitmap);
//
CKadr:=CKadr+1;
if CKadr = N_KADR
then CKadr:=0;
end;
7. . .
.
. Paint
. .
10

,
.

, ,
, .
: TabControl ( Win32),
StringGrid ( Additional) . OpenDialog SaveDialog ( Dialogs)
.
1. TabControl
Tabs
String List Editor ( Tabs).
2. .
3. Align alClient,
.
27

4. StringGrid :
ColCount ( ) = 4 RowCount ( ) =7;
Options (
(+) goEditing True (
), goColSizing True ( , ).
5. Type
r=array [0..6,1..7,1..3] of string[40]; , ( TabIndex
TabControl, 0),
(1 , 2 , 3 ).
6. Var :
rasp:r f: file of r ;.
7. .
7.1. :

OnCreate:
with StringGrid1 do begin
Cells[1,0]:=;
Cells[2,0]:=;
Cells[3,0]:=;
Cells[0,1]:=8-30';
Cells[0,2]:=10-10';
.
Cells[0,7]:=18-30';
end;
OnActivate,
,
rasp:
var i,j:integer;
begin
SaveDialog1.FileName:=.ras;
if MessageDlg
( ?,mtConfirmation,[mbYes,mbNo],0)=mrYes
then
begin
28

if OpenDialog1.Execute then begin


SaveDialog1.FileName:=OpenDialog1.FileName;
AssignFile(f,OpenDialog1.FileName);
Reset(F);
read(f,rasp);
CloseFile(f);
with StringGrid1 do begin
for i := 1 to 7 do
for j := 1 to 3 do
cells[j,i]:=rasp[TabControl1.TabIndex,i,j];
end;
end;
end;
end;
OnCloseQuery:
var i,j:integer;
begin
if MessageDlg
( ?, mtConfirmation, [mbYes,
mbNo], 0)=mrYes
then
begin
with StringGrid1 do begin
for i := 1 to 7 do
for j := 1 to 3 do
rasp[TabControl1.TabIndex,i,j]:=cells[j,i];
end;
if SaveDialog1.Execute then begin
AssignFile(f,SaveDialog1.FileName);
Rewrite(F);
write(f,rasp);
end;
end;
end;
7.2. TabControl:

OnChanging:
29

var i,j:integer;
begin
with StringGrid1 do begin
for i := 1 to 7 do
for j := 1 to 3 do
rasp[TabControl1.TabIndex,i,j]:=cells[j,i];
end;
end;
rasp OnChange:
var i,j:integer;
begin
with StringGrid1 do begin
for i := 1 to 7 do
for j := 1 to 3 do
cells[j,i] :=rasp[TabControl1.TabIndex,i,j];
end;
end;
8. . .
. TabControl StringGrid.
. .
11

. ,
,

Windows, .
. .
1. RichEdit ( Win32)
(RichEdit1).
2. Label ( Standard) : (Label1) (Label2 ).
3. ComboBox ( Standard)
(ComboBox1).
30

4. SpinEdit( Samples)
(SpinEdit1).
5. Object inspector
Properties.
Caption () - ( Form1) .
, ShowHint True ( ) Hint ( ) .
RichEdit1 Lines. String list editor : , . Aline alTop,
.
SpinEdit1 : 8,
72, 20 ( MinValue, MaxValue
Value).
6. Object inspector Events.
OnCreate Form1:
ComboBox1.Items := Screen.Fonts;
RichEdit1.Font.size:=20;
OnClick
Combobox1:
RichEdit1.Font.Name := ComboBox1.Items[ComboBox1.ItemIndex];
RichEdit1.Font.size:=SpinEdit1.Value;

OnChange SpinEdit1:
RichEdit1.Font.size:=SpinEdit1.Value;
7. .. .
. .
. .

31



, 11, ,
. .

1. StatusBar ( Win32)
(StatusBar1).
2. Button1 ( Standard) 11.
3. Object inspector
Properties.
StatusBar1,
:
Panels, Editing
StatusBar1.Panels, Add New
0 1 TSatusPanel.
0 Width () 250 ( 50, ).
4. ,
, .
OnCreate:
StatusBar1.Panels[1].Text:= ;
OnClick Combobox1:
StatusBar1.Panels[0].Text:= +
RichEdit1.Font.Name;
Button1 OnClick ,
:
StatusBar1.Panels[1].Text:= ;
StatusBar1.Panels[1].Text:= ;
32

5. . .
. . .
12

MDI,
.

, bmp.
.
1. Delphi MDI.
New Delphi
New Items Projects MDI Application.
OK ,
.
, . : Main
, hildwin About
.
.
2. ( )
Image ( Additional), .
3. OpenDialog
OpenPictureDialog ( Dialogs) .
4. Image o e Stretch true, .
5. Filter
OpenPictureDialog, Filter Editor (
)
, *.bmp, *.ico *.wmf. Image .
6. About Label, . Image ,
33

. Pictures, Picture Editor


. Picture Editor Load

OK.
Image ,
Pictures.
7. , .
8. . . Program Files\Common
Files\BorlandShared\Images.
. , .
. .
13

SDI,
. Paradox

, country.db.
1. .
2. (Form3)
Database->Form
Wizard. (
Ttable) . Next.
Drive or
Alias name DBDEMOS Table name
country.db. Next. Name, Area Population
( >). ( ) . Next. (Form
34

and Data Module) Main form. Finish.


: DataModule2 Unit2 Form3 Unit3.
3. (Form4) , .
4.
(Form1), :
Form3.Show Form4.Show.
5. Form1
:
Uses Unit3, Unit4.
6. . .
.
14


,
. Internet.
Pascal
Delphi.
1. Form1 ,
. Code Editor (
Project->View source). :
Uses Forms;
Application.Initialize;
Application.Run;
2. begin end Pascal:
Writeln(This program has not windowinterface);
3. : {$APPTYPE CONSOLE}.
4. Generate console application
( Project options, Linker, EXE and DLL options).
5. .
MS-DOS.
35

. .
, . .

1. ., ., Delphi 5 . .: BHV, 1999.


2. ., . Delphi 6. .: BHV, 2001.

36

Potrebbero piacerti anche