//***********************************************************************
//此文本以样例的形式介绍了常用的DELPHI OBJECT PASCAL语法、数据类型
//以及一些使用规范。希望对大家有用并提出宝贵意见。
//************************************************************************
//******************************************************************
//主程序规范和语法
Program
Ueses useunit1,useunit2;
Begin
End.
//**********************************************************************
//unit 单元规范和语法
Unit name
//定义部分
Interface
//单元引用
Uses unitname1,unitname2;
//条件编译预定义
{$DEFINE DEFTEST1}
{$IFDEF DEFTEST}
…….
{$ELSE}
……..
{$ENDIF}
//包含预编译指令
{$I INCLUDEFILE.INC}
//数据类型
{
Type Range Format
Shortint -128..127 signed 8-bit
Smallint -32768..32767 signed 16-bit
Longint -2147483648..2147483647 signed 32-bit
Int64 -2^63..2^63-1 signed 64-bit
Byte 0..255 unsigned 8-bit
Word 0..65535 unsigned 16-bit
Longword 0..4294967295 unsigned 32-bit
Pointer 指针unsigned 32-bit
Boolean bool类型
Char 字符类型
浮点类型
Type Range Significant digits Size in bytes
Real48 2.9 x 10^-39 .. 1.7 x 10^38 11-12 6
Single 1.5 x 10^-45 .. 3.4 x 10^38 7-8 4
Double 5.0 x 10^-324 .. 1.7 x 10^308 15-16 8
Extended 3.6 x 10^-4951 .. 1.1 x 10^4932 19-20 10
Comp -2^63+1 .. 2^63 -1 19-20 8
Currency -922337203685477.5808.. 922337203685477.5807 19-20 8
字符串类型
Type Maximum length Memory required
ShortString 255 characters 2 to 256 bytes
AnsiString ~2^31 characters 4 bytes to 2GB
WideString ~2^30 characters 4 bytes to 2GB
}
//全局变量
Var
giInt : integer;
//数组
gArray : array[0..10] as integer;
//全局常量
Const
giInt = 1234;
//类型定义
Type
//记录定义
Trecodr = record
iInt : integer;
pPointer : pointer;
//记录联合
Case integer of
0 : (wWord : word;
wWord2 : word);
1 : (dDword : Dword);
End;
//自定义数据类型
TselfData = (tdself1,tdself2,tdself3t,tdself4);
//自定义枚举类型
TsetSelfData = set of TselfData;
//函数指针和事件函数定义
TselfOnChanged = procedure (sender : tobject;tagid,id : integer)of object;
//类定义
TselfClass = class(Tobject)
Private //私有函数和变量
FiInt : integer;
Function selffunc(par1 : word;ppar : pointer) : integer;
//message 映射
Procedure WMMessage(var msg : Tmessage); message WM_MESSAGE;
//属性存取函数
Function GetiINt : integer;
Procedure SetiInt(value : integer);
Protected //保护函数和变量,允许类继承者访问。
Function selfoverride(ppar : pointer) : dword;override;//类函数继承
//类函数重载,同样适用用于不同函数。
Procedure selfoverload(ppar : pointer);overload;
Procedure selfoverload(ppar : pointer;iindex : integer);overload;
Public //共有函数和变量,允许所有人访问
//构造函数
Constructor create(sender : Tobject);override;
//析构函数
Destructor destroy;override;
//属性定义
Property selfpro : integer read FiInt Write FiInt;
Property selfprofunc : integer read GetiInt Write SetiInt;//使用函数存取属性
//数组属性定义,自读属性,类默认属性
property items[index : integer] : integer Read GetItems;default;
end;
//全局函数定义
Function gfunc(iIndex : integer) : word;stdcall;//stdcall定义参数调用方法,如果是dll,必须使用
//----------------------------------------------------------------------------------------------------------
//程序实现
Implementation
//引用
Uses useunit1;
//在delphi控件栏注册函数
procedure Register;
begin
RegisterComponents('WISDOSOFT',[TselfControl1,Tselfcontrol2]);
End
//函数实现以及语法简介
Function gFunc(iIndex : integer) : word;
Var//变量定义
iCount : integer;
const//常量定义
MAX_COUNT = 200;
CONST_STR = ‘THIS ONLY TO TEST’;
Begin
//FOR 循环
FOR iCount = 0 to MAX_COUNT DO
Begin
…..
End;
//for 递减循环
For iCount = MAX_COUNT downto 0 do
Begin
…..
End;
//while 循环
While iCount > 0 do
Begin
iCount := iCount – 1;
…..
End;
//until 循环
Repeat
iCount := iCount + 1;
….
Until iCount > MAX_COUNT;
//if 语句
If iCount > 0 then
Begin
….
End else if iCount = 0 then
Begin
….
End else Begin
….
End;
End;
//单元初始运行代码
initialization
CreateSysImages;
//单元退出运行代码
finalization
DestroySysImages;
End.//单元结束
//***************************************************************************
//dll编写规范
library FileBackDll;
{ 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;
//函数定义
function dllStartBackup(hwin : Thandle;pData : PRFCBackUpData):boolean;stdcall;
//dll输出函数定义
exports
DllStartBackUp index 1;
begin
//函数实现
function dllStartBackup(hwin : Thandle;pData : PRFCBackUpData):boolean;
begin
……
end;
end.
//****************************************************************
//dll函数引用使用方法
Unit dllunit;
Uses
function MoveFile(lpExistingFileName, lpNewFileName: PChar): BOOL; stdcall;
Implementation
function MoveFile; external ‘kernel32’ name 'MoveFileA';
end.
Function TselfClass.GetiInt : integer;
Begin
Result : = FiInt;
….
End;
// 构造函数实现
Constructor TselfClass.create(sender : Tobject);
Begin
inherited create(sender); // 函数继承
….
End;
……