y:=mx+b
If A=B then Writeln ('A and B are equal')
Const
TaxRate = 0.0075
program IfDemo;
uses Forms;
var UserInput : Integer;
begin
Write('How old are you ? ');
ReadLn(UserInput);
If (UseerInput < 1) or (UserInput > 130) Then
WriteLn('You are not telling the truth.')
Else
WriteLn('Being ', UserInput, 'yers old is great!');
WriteLn('Press Enter to Exit program');
ReadLn {To keep the window from closing until you press Enter}
end {InDemo}
program CaseDemo;
Uses Forms;
var UserIn : Char;
begin
Write('Type in character followed dy the Enter key: ');
ReadLn(UserIn);
Case UserIn of
'a' :WriteLn('That is a Small a');
'z','Z':WriteLn('That is a small or capital z')
else
WriteLn('That is a character other than an a, z, or Z.')
end;
WriteLn('Press Enter to exit program');
ReadLn {To keep the window from closing until you press Enter}
end {CaseDemo}
یک فایل ورد زیپ شده با حجم 400 کیلو.
آموزش نصب کامپوننت در دلفی
type
TDay = (Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday)
var
Name : String; {256 Bytes}
X, Y : Integer; {4 + 4 = 8 Bytes}
List : array [0..10] of Double; {8 * 11 = 88 Bytes}
Today : TDay; {1 Byte}
var
;Number : ^Integer
;Name : ^String
var
;Something : Pointer
begin
New(Something);
Dispose(Something);
end;
type
IntPtr = ^Integer;
var
Something : Pointer;
begin
Something := New(IntPtr);
Integer(Something^) := 10;
Dispose(Something);
end;
var
Something : Pointer
begin
GetMem(Something, 100);
FreeMem(Something, 100);
end;
var
Something : Pointer;
MyString : PChar; // type PChar = ^Char;
begin
GetMem(Something, 100);
MyString := Something;
StrCopy(Something, 'Hello World');
FreeMem(Something, 100);
end;
var
array[indexType1, ..., indexTypen] of baseType;
d(.i.):= 3 + i; // Equivalent d:= 3 + i;
var
A : array [Boolean] of integer;
begin
A[True] := 50;
A[False] := 100;
end;
for I := 0 to High(X) do S := S + X;
type
TDay = (Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday);
const
DayNames : array [TDay] of String[9] = ('Sunday', 'Monday', 'Tuesday',
'Wednesday', 'Thursday',
'Friday', 'Saturday');
var
Today : TDay;
begin
Today := TDay(DayOfWeek(Date) - 1);
ShowMessage('Today is ' + DayNames[Today] + '!');
end;
var MyFlexibleArray: array of Real;
SetLength (MyFlexibleArray, 2; (
MyFlexibleArray := nil;
type TNumbers = packed array[1..100] of Real;
type
TMessageGrid = array of array of string;
var
Msgs: TMessageGrid;
SetLength(Msgs, I, J);
var
Ints: array of array of Integer;
SetLength(Ints, 10);
SetLength(Ints[2], 5);
type
TMyDBGrid = class(TDBGrid)
published
property OnMouseMove;
end;
procedure WMMouseMove(var Message : TWMMouse); message WM_MOUSEMOVE;
MouseRow : integer;
MouseCol : integer;
procedure TMyDBGrid.WMMouseMove(var Message : TWMMouse);
var
t : TGridCoord;
begin
t := MouseCoord(Message.XPos, Message.YPos);
MouseCol := t.x;
MouseRow := t.y;
inherited;
end;
procedure TForm2.MyDBGrid1MouseMove(Sender: TObject; Shift: TShiftState; X, Y:
Integer);
begin
if (((dgIndicator in MyDBGrid1.Options) and (MyDBGrid1.MouseCol > 0)) or
((not (dgIndicator in MyDBGrid1.Options)) and (MyDBGrid1.MouseCol <> -1)))
and (MyDBGrid1.MouseCol <> OldMouseCol) then begin
OldMouseCol := MyDBGrid1.MouseCol;
if dgIndicator in MyDBGrid1.Options then
MYDBGrid1.Hint := MyDBGrid1.Columns[MyDBGrid1.MouseCol - 1].FieldName
else
MYDBGrid1.Hint := MyDBGrid1.Columns[MyDBGrid1.MouseCol].FieldName;
end;
end;
HintMouseMessage(Control : TControl; var Message : TMessage)
var
r : TMessage;
begin
Application.HintMouseMessage(self, r);
TWMMouse(r).XPos := X;
TWMMouse(r).YPos := Y;
Application.HintMouseMessage(MyDBGrid1, r);
end;
procedure TForm1.CopyFileWithProgressBar1(Source, Destination: string);
var
FromF, ToF: file of byte;
Buffer: array[0..4096] of char;
NumRead: integer;
FileLength: longint;
begin
AssignFile(FromF, Source);
reset(FromF);
AssignFile(ToF, Destination);
rewrite(ToF);
FileLength := FileSize(FromF);
with Progressbar1 do
begin
Min := 0;
Max := FileLength;
while FileLength > 0 do
begin
BlockRead(FromF, Buffer[0], SizeOf(Buffer), NumRead);
FileLength := FileLength - NumRead;
BlockWrite(ToF, Buffer[0], NumRead);
Position := Position + NumRead;
end;
CloseFile(FromF);
CloseFile(ToF);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
CopyFileWithProgressBar1('c:\Welcome.exe', 'c:\temp\Welcome.exe');
end;
procedure TForm1.CopyFileWithProgressBar1(Source, Destination: string);
var
FromF, ToF: file of byte;
Buffer: array[0..4096] of char;
NumRead: integer;
FileLength: longint;
t1, t2: DWORD;
maxi: integer;
begin
AssignFile(FromF, Source);
reset(FromF);
AssignFile(ToF, Destination);
rewrite(ToF);
FileLength := FileSize(FromF);
with Progressbar1 do
begin
Min := 0;
Max := FileLength;
t1 := TimeGetTime;
maxi := Max div 4096;
while FileLength > 0 do
begin
BlockRead(FromF, Buffer[0], SizeOf(Buffer), NumRead);
FileLength := FileLength - NumRead;
BlockWrite(ToF, Buffer[0], NumRead);
t2 := TimeGetTime;
Min := Min + 1;
// Show the time in Label1
label1.Caption := FormatFloat('0.00', ((t2 - t1) / min * maxi - t2 + t1) / 100);
Application.ProcessMessages;
Position := Position + NumRead;
end;
CloseFile(FromF);
CloseFile(ToF);
end;
end;
((t2 - t1) / min * maxi - t2 + t1) / 100
ProgressBar1: TprogressBar;
var
ProgressBarStyle: LongInt;
begin
{create a run progress bar in the status bar}
ProgressBar1 := TProgressBar.Create(StatusBar1);
ProgressBar1.Parent := StatusBar1;
{remove progress bar border}
ProgressBarStyle := GetWindowLong(ProgressBar1.Handle, GWL_EXSTYLE);
ProgressBarStyle := ProgressBarStyle - WS_EX_STATICEDGE;
SetWindowLong(ProgressBar1.Handle, GWL_EXSTYLE, ProgressBarStyle);
{set progress bar position and size - put in Panel[2]}
ProgressBar1.Left := StatusBar1.Panels.Items[0].Width +
StatusBar1.Panels.Items[1].Width + 4;
ProgressBar1.Top := 4;
ProgressBar1.Height := StatusBar1.Height - 6;
ProgressBar1.Width := StatusBar1.Panels.Items[2].Width - 6;
{set range and initial state}
ProgressBar1.Min := 0;
ProgressBar1.Max := 100;
ProgressBar1.Step := 1;
ProgressBar1.Position := 0;
end;
ProgressBar1.free;
library demodll;
{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }
uses
SysUtils,
Classes,
UDemo in 'UDemo.pas' {Demo};
{$R *.res}
procedure ShowdemoForm;stdcall;
begin
Demo :=Tdemo.Create(nil);
demo.Show;
end;
function ShowdemoFormModal:integer;stdcall;
begin
demo :=Tdemo.Create(nil);
Result := demo.ShowModal;
end;
Exports
ShowDemoForm,
ShowdemoFormModal;
begin
end.
unit UMain;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
type
TShowdemoFormModal= Function :integer;
.
.
.
var
hndDLLHandle:THandle;
ShowdemoFormModal:TShowdemoFormModal;
procedure TFMain.Button1Click(Sender: TObject);
begin
try
hndDLLHandle:=LoadLibrary('Demodll.dll');
if hndDLLHandle <> 0 then begin
@ShowdemoFormModal:=getProcAddress(hndDLLHandle,'ShowdemoFormModal');
if addr(ShowdemoFormModal) <> nil then begin
ShowdemoFormModal;
end
else
showmessage ('function not exists ...');
end
else
showMessage('Dll Not Found!');
finally
freelibrary(hndDLLHandle);
end;
end;
unit MinMax;
interface
uses
SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
Forms, Dialogs;
type
TForm1 = class(TForm)
private
{ Private declarations }
procedure WMGetMinMaxInfo(var MSG: Tmessage); message WM_GetMinMaxInfo;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.WMGetMinMaxInfo(var MSG: Tmessage);
Begin
inherited;
with PMinMaxInfo(MSG.lparam)^ do
begin
with ptMinTrackSize do
begin
X := 300;
Y := 150;
end;
with ptMaxTrackSize do
begin
X := 350;
Y := 250;
end;
end;
end;
end.
Label1.Canvas.Refresh();
Image1.Canvas.Refresh();
ListBox1.Canvas.Refresh();
Var
ADesktop:TCanvas;
Var
ADesktop:TCanvas;
begin
ADesktop := TCanvas.Create;
...
End;
...
ADesktop.Handel := GetDC(0);
....
....
ADesktop.free;
End
Handle:=GetDC(Hwnd_Desktop);
CustomCanvas.Handle:=Handle;
Brush مشخصات مربوط به مساحتها
Pen مشخصات قلم ترسيم
Handle هندل
Pixels رنگ يك پيكسل خاص از تصوير
Font خصوصيات قلم متن
Arc ترسيم كمان
CopyRect كپي قسمتي از تصويربه تصويري ديگر
Creat ايجاد
Draw رسم يك گرافيك
Ellipse رسم بيضي و دايره
FillRect رنگ كردن يك مستطيل
FloodFill رنگ كردن داخل يك شكل بسته
LineTo رسم خط از نقطة جاري
MoveTo تغيير نقطة جاري
Pie رسم قسمتي از بيضي
Polygon رسم چندضلعي
Rectangle رسم مستطيل
RoundRect رسم مستطيل با گوشه هاي دايروي
TextOut نوشتن متن
Procedure TForm1.FormCreate(Sender:TObject);
Begin
End;
var
Form1: TForm1;
DesktopCanvas:TCanvas;
implementation
DesktopCanvas:=TCanvas.Create;
DesktopCanvas.Handle:=GetDC(Hwnd_Desktop);
DesktopCanvas.Brush.Style:=bsClear;
DesktopCanvas.TextOut(10,10,'This is your text on the Desktop!');
DesktopCanvas.TextOut(10,40,TimeToStr(Time));
unit dsktop_handling;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Timer1: TTimer;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
DesktopCanvas:Tcanvas;
implementation
{$R *.DFM}
procedure TForm1.FormCreate(Sender: TObject);
begin
DesktopCanvas:=TCanvas.Create;
DesktopCanvas.Handle:=GetDC(HWND_DESKTOP);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
DesktopCanvas.Brush.Style:=bsClear;
DesktopCanvas.TextOut(10,10,'This is a text');
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
DesktopCanvas.TextOut(10,40,TimeToStr(Time));
end;
end.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;
type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Edit4: TEdit;
Edit5: TEdit;
Edit6: TEdit;
Edit7: TEdit;
Edit8: TEdit;
Edit9: TEdit;
Edit10: TEdit;
Edit11: TEdit;
Label1: TLabel;
Button1: TButton;
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
end.
procedure TForm1.Button1Click(Sender: TObject);
begin
end;
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Edit4: TEdit;
Edit5: TEdit;
Edit6: TEdit;
Edit7: TEdit;
Edit8: TEdit;
Edit9: TEdit;
Edit10: TEdit;
Edit11: TEdit;
Label1: TLabel;
Button1: TButton;
procedure Button1Click(Sender: TObject);
edit11.text := floattostrf(((strtofloat(edit1.text) +
strtofloat(edit2.text) + strtofloat(edit3.text) +
strtofloat(edit4.text) + strtofloat(edit5.text) +
strtofloat(edit6.text) + strtofloat(edit7.text) +
strtofloat(edit8.text) + strtofloat(edit9.text) +
strtofloat(edit10.text)) / 10), ffgeneral, 10, 4);
edit11.text := floattostrf(
(
(
strtofloat(edit1.text) +
strtofloat(edit2.text) + strtofloat(edit3.text) +
strtofloat(edit4.text) + strtofloat(edit5.text) +
strtofloat(edit6.text) + strtofloat(edit7.text) +
strtofloat(edit8.text) + strtofloat(edit9.text) +
strtofloat(edit10.text)
)
/ 10)
, ffgeneral, 10, 4);
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Edit4: TEdit;
Edit5: TEdit;
Edit6: TEdit;
Edit7: TEdit;
Edit8: TEdit;
Edit9: TEdit;
Edit10: TEdit;
Edit11: TEdit;
Label1: TLabel;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
edit11.text := floattostrf(
(
(
strtofloat(edit1.text) +
strtofloat(edit2.text) + strtofloat(edit3.text) +
strtofloat(edit4.text) + strtofloat(edit5.text) +
strtofloat(edit6.text) + strtofloat(edit7.text) +
strtofloat(edit8.text) + strtofloat(edit9.text) +
strtofloat(edit10.text)
)
/ 10)
, ffgeneral, 10, 4);
end;
end.
Downloading the book and the code
After a tentative publication as HTML, the book is now available in a single file in PDF format. Due to the high download volume I actually had to put the PDF file inside a ZIP (this isn't really smaller but you'll have to save it instead of downloading the material each time). The download of a single PDF for the entire book is bigger, but you'll benefit from having the entire book easily available on your local computer in one file, rather than having to handle multiple chapters with many files for the figures.
So you can download the file Essential Delphi_103.zip (which is almost 1.2 Mbytes). You can also download the source code in edelphicode103.zip (about 200 Kbyes).
program Hello;
uses
Forms,
Hellof in 'Hellof.pas' {Form1};
{$R *.RES}
begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
unit Hellof;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls,
Forms, Dialogs, StdCtrls;
type
TForm1 = class(TForm)
BtnHello: TButton;
procedure BtnHelloClick(Sender: TObject);
procedure FormClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.BtnHelloClick(Sender: TObject);
begin
MessageDlg ('Hello, guys', mtInformation, [mbOK], 0);
end;
procedure TForm1.FormClick(Sender: TObject);
begin
MessageDlg ('You have clicked outside of the button',
mtWarning, [mbOK], 0);
end;
end.
type
TForm1 = class(TForm)
BtnHello: TButton;
procedure BtnHelloClick(Sender: TObject);
procedure FormClick(Sender: TObject);
procedure FormResize(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
procedure TForm1.BtnHelloClick(Sender: TObject);
begin
MessageDlg ('Hello, guys', mtInformation, [mbOK], 0);
BtnHello.Caption := 'Say Hello Again';
end;
procedure TForm1.FormClick(Sender: TObject);
begin
MessageDlg ('You have clicked outside of the button',
mtWarning, [mbOK], 0);
end;
procedure TForm1.FormResize(Sender: TObject);
begin
BtnHello.Top :=
Form1.ClientHeight div 2 -
BtnHello.Height div 2;
BtnHello.Left :=
Form1.ClientWidth div 2 -
BtnHello.Width div 2;
end;
BtnHello.Caption := 'Say Hello Again';
BtnHello.Caption := 'i am ready click me.';
program Project2;
{$APPTYPE CONSOLE}
uses
SysUtils;
begin
{ TODO -oUser -cConsole Main : Insert code here }
end.
program Project2;
{$APPTYPE CONSOLE}
uses
SysUtils;
var
s : String;
begin
Writeln('Enter in a word');
Readln(s);
Writeln('You Typed: ', s);
Writeln('Press Enter to exit');
Readln;
end.
برای تست این نوع دستورات روش من این است که یک پروژه اول می سازم(که دلفی خودش با باز شدنش خودش یک پروژه ویندوزی آماده می کند برای ما ) بعد یک دکمه روی فرم می اندازم. بعد روی دکمه دابل کلیک می کنم و کد را داخل روال کلیک دکمه پیست می کنم و اجرا را می زنم.دستور اول caption هست
که به صورت زیر نوشته میشه
خوب این اسم بالای فرم رو تغییر میده.کد:caption:=' nevisande soldier';
BtnHello.Caption := 'Say Hello Again';
با سلام.
راجع به متغیرها در دلفی توضیحاتی می خواستم.
ما در هر یونیت یک قسمت var داریم که متغیرها را آنجا تعریف می کنیم . آیا نمیشه متغیر را داخل یک روال تعریف کرد؟ یعنی متغیر محلی داشت؟
unit[COLOR="Orange"] Unit1;[/COLOR]
interface
uses
[COLOR="Orange"] Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;[/COLOR]
type
[COLOR="Orange"]TForm1[/COLOR] = class([COLOR="Orange"]TForm[/COLOR])
private
{ Private declarations }
public
{ Public declarations }
end;
var
[COLOR="Orange"]Form1: TForm1;[/COLOR]
implementation
{$R *.dfm}
end.
type
TForm1 = class(TForm)
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
procedure TForm1.Button1Click(Sender: TObject);
var
num1,num2:integer;
name, family,address:string;
begin
num1:=5;
family:='Borland'
end;
Function showhello:integer;
Begin
ShowMessage('Hello!');
Result:= 0 ;
End;
library Project2;
{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }
uses
SysUtils,
[COLOR="Red"]dialogs[/COLOR],
Classes;
{$R *.res}
Function showhello:integer;
Begin
ShowMessage('Hello!');
Result:= 0 ;
End;
[COLOR="#ff0000"]exports showhello[/COLOR];
begin
end.
function showhello:integer ; stdcall; external 'eeuok.dll';
procedure TForm1.FormCreate(Sender: TObject);
begin
showhello;
end;
uses
SysUtils;
uses
SysUtils,
dialogs,
Classes;
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;