45fan.com - 路饭网

搜索: 您的位置主页 > 网络频道 > 阅读资讯:DELPHI常用函数集介绍

DELPHI常用函数集介绍

2016-09-03 12:00:43 来源:www.45fan.com 【

DELPHI常用函数集介绍

abs(x)绝对值
arctan(x)反正切
cos(x)传回馀弦函数值
exp(x)e的x次幂
frac(x)取小数部分
int(x)取整
ln(x)自然对数
sin(x)传回正弦函数值
sqr(x)x*x
sqrt(x)平方根
其它
pred(x)pred('D')='C',pred(true)=1;
succ(x)succ('Y')='Z',succ(pred(x))=x
ord(x)求x在字符集中的序号,如ord('A')=65
chr(x)chr(65)='A'
round(x)四舍五入
trunc(x)trunc(4.8)=4,trunc('-3.6')=-3
upcase(x)upcase('a')='A'
hi(I)hi($2A30)=$2A
lo(I)lo($2A30)=$30
random(n)产生[0,n)间的随机整数
sizeof(name)求出某类型或变量在内存中占用的字节数
swap(num)swap($3621)=$2136
================================
Arithmeticroutines数学运算
================================
Abs绝对值
---------------------------------------------------------
UnitSystem
函数原型functionAbs(X);
说明X为整数or实数.
范例
var
r:Real;
i:Integer;
begin
r:=Abs(-2.3);{2.3}
i:=Abs(-157);{157}
end;
----------------------------------------------------------
ArcTan三角函数
----------------------------------------------------------
范例
Cos
varR:Extended;
begin
R:=Cos(Pi);
end;
----------------------------------------------------------
Sin
----------------------------------------------------------
范例
var
R:Extended;
S:string;
begin
R:=Sin(Pi);
Str(R:5:3,S);
Canvas.TextOut(10,10,'TheSinofPiis'+S);
end;
----------------------------------------------------------
UnitSystem
函数原型functionArcTan(X:Extended):Extended;
函数原型functionCos(X:Extended):Extended;
函数原型functionSin(X:Extended):Extended;
----------------------------------------------------------
说明X为径度.
Tan(x)===Sin(x)/Cos(x)
ArcSin(x)=ArcTan(x/sqrt(1-sqr(x)))
ArcCos(x)=ArcTan(sqrt(1-sqr(x))/x)
左边这三个不是函数,而是右边运算求得.
范例
var
R:Extended;
begin
R:=ArcTan(Pi);
end;
范例var
R:Extended;
S:string;
begin
R:=Sin(Pi);
Str(R:5:3,S);
Canvas.TextOut(10,10,'TheSinofPiis'+S);
end;
----------------------------------------------------------
Frac求一个实数的小数部份
----------------------------------------------------------
UnitSystem
函数原型functionFrac(X:Real):Real;
说明X为实数.
范例var
R:Real;
begin
R:=Frac(123.456);{0.456}
R:=Frac(-123.456);{-0.456}
end;
------------------------------------------
Int求一个实数的整数部份
------------------------------------------
UnitSystem
函数原型functionInt(X:Real):Real;
说明X为实数.
范例var
R:Real;
begin
R:=Int(123.456);{123.0}
R:=Int(-123.456);{-123.0}
end;
------------------------------------------
Pi就是数学的Pi
------------------------------------------
UnitSystem
函数原型functionPi:Extended;
说明它是一个函数,但我们就把它当作是预设的变数来用吧!
Pi=3.1415926535897932385
------------------------------------------
SqrX的平方
-----------------------------------------
范例
var
S,Temp:string;
begin
Str(Sqr(5.0):3:1,Temp);
S:='5squaredis'+Temp+#13#10;
Str(Sqrt(2.0):5:4,Temp);
S:=S+'Thesquarerootof2is'+Temp;
MessageDlg(S,mtInformation,[mbOk],0);
end;
-----------------------------------------
SqrtX的平方根
------------------------------------------
UnitSystem
函数原型functionSqr(X:Extended):Extended;
函数原型functionSqrt(X:Extended):Extended;
范例var
S,Temp:string;
begin
Str(Sqr(5.0):3:1,Temp);
S:='5squaredis'+Temp+#13#10;
Str(Sqrt(2.0):5:4,Temp);
S:=S+'Thesquarerootof2is'+Temp;
MessageDlg(S,mtInformation,[mbOk],0);
end;
------------------------------------------
Ln自然对数
------------------------------------------
范例
var
e:real;
S:string;
begin
e:=Exp(1.0);
Str(ln(e):3:2,S);
S:='e='+FloatToStr(e)+';ln(e)='+S;
Canvas.TextOut(10,10,S);
end;
----------------------------------------
Exp指数
------------------------------------------
UnitSystem
函数原型functionLn(X:Real):Real;
函数原型functionExp(X:Real):Real;
范例var
e:real;
S:string;
begin
e:=Exp(1.0);
Str(ln(e):3:2,S);
S:='ln(e)='+S;
Canvas.TextOut(10,10,S);
end;
------------------------------------------
Dateandtimeroutines日期及时间函数
------------------------------------------
Date传回目前的日期
UnitSysUtils
函数原型functionDate:TDateTime;
范例procedureTForm1.Button1Click(Sender:TObject);
begin
Label1.Caption:='Todayis'+DateToStr(Date);
end;
------------------------------------------
DateTimeToStr日期时间转换成内定型字串(1996/12/2009:12:20PM)
------------------------------------------
UnitSysUtils
函数原型functionDateTimeToStr(DateTime:TDateTime):string;
范例procedureTForm1.Button1Click(Sender:TObject);
begin
Label1.Caption:=DateTimeToStr(Now);
end;
--------------------------------------------------------
DateTimeToString日期时间转换成自定型字串
-------------------------------------------------------
UnitSysUtils
函数原型procedureDateTimeToString(varResult:string;constFormat:
string;DateTime:TDateTime);
范例procedureTForm1.FormCreate(Sender:TObject);
var
s:string;
begin
DateTimeToString(s,'dddd,mmmmd,yyyy"at"hh:mm
AM/PM',Now);
Label1.Caption:=s;
end;
结果星期五,十二月20,1996at09:20PM
-----------------------------------------------------------------------------
****Format格式叁考下面.FormatDateTime.
--------------------------------------------------------
DateToStr日期转换成内定型字串.(1996/12/20)
--------------------------------------------------------
UnitSysUtils
函数原型functionDateToStr(Date:TDateTime):string;
范例
procedureTForm1.Button1Click(Sender:TObject);
begin
Label1.Caption:='Todayis'+DateToStr(Date);
end;
#Date,DateToStrExample
--------------------------------------------------------
DayOfWeek求叁数日期是星期几.
--------------------------------------------------------
UnitSysUtils
函数原型functionDayOfWeek(Date:TDateTime):Integer;
说明传回值是一整数,1~7.
星期日为1.
范例
procedureTForm1.Button1Click(Sender:TObject);
var
ADate:TDateTime;
days:array[1..7]ofstring;
begin
days[1]:='Sunday';
days[2]:='Monday';
days[3]:='Tuesday';
days[4]:='Wednesday';
days[5]:='Thursday';
days[6]:='Friday';
days[7]:='Saturday';
ADate:=StrToDate(Edit1.Text);
ShowMessage(Edit1.Text+'isa'+days[DayOfWeek(ADate)];
end;
#StrToDate,DayOfWeekExample
--------------------------------------------------------
DecodeDate将TDateTime型态的日期变数,转为Word型态.
--------------------------------------------------------
范例
procedureTForm1.Button1Click(Sender:TObject);
var
Present:TDateTime;
Year,Month,Day,Hour,Min,Sec,MSec:Word;
begin
Present:=Now;
DecodeDate(Present,Year,Month,Day);
Label1.Caption:='TodayisDay'+IntToStr(Day)+'ofMonth'
+IntToStr(Month)+'ofYear'+IntToStr(Year);
DecodeTime(Present,Hour,Min,Sec,MSec);
Label2.Caption:='ThetimeisMinute'+IntToStr(Min)+'ofHour'
+IntToStr(Hour);
end;
#DecodeDate,DecodeTimeExample
--------------------------------------------------------
DecodeTime将TDateTime型态的时间变数,转为Word型态.
--------------------------------------------------------
UnitSysUtils
函数原型procedureDecodeDate(Date:TDateTime;varYear,Month,Day:Word);
函数原型procedureDecodeTime(Time:TDateTime;varHour,Min,Sec,MSec:Word);
范例procedureTForm1.Button1Click(Sender:TObject);
var
Present:TDateTime;
Year,Month,Day,Hour,Min,Sec,MSec:Word;
begin
Present:=Now;
DecodeDate(Present,Year,Month,Day);
Label1.Caption:='TodayisDay'+IntToStr(Day)+'of
Month'+IntToStr(Month)+'ofYear'+IntToStr(Year);
DecodeTime(Present,Hour,Min,Sec,MSec);
Label2.Caption:='ThetimeisMinute'+IntToStr(Min)+'of
Hour'+IntToStr(Hour);
end;
--------------------------------------------------------
EncodeDate将Word型态的日期变数,转为TDateTime型态.
--------------------------------------------------------
范例
procedureTForm1.Button1Click(Sender:TObject);
var
MyDate:TDateTime;
begin
MyDate:=EncodeDate(StrToInt(Edit1.Text),StrToInt(Edit2.Text),StrToInt(Edit3.Text));
Label1.Caption:=DateToStr(MyDate);
end;
-------------------------------------------------------
EncodeTime将Word型态的时间变数,转为TDateTime型态.
--------------------------------------------------------
UnitSysUtils
函数原型functionEncodeDate(Year,Month,Day:Word):TDateTime;
函数原型functionEncodeTime(Hour,Min,Sec,MSec:Word):
TDateTime;
范例procedureTForm1.Button1Click(Sender:TObject);
var
MyDate:TDateTime;
MyTime:TDateTime;
begin
MyDate:=EncodeDate(83,12,31);
Label1.Caption:=DateToStr(MyDate);
MyTime:=EncodeTime(0,45,45,7);
Label2.Caption:=TimeToStr(MyTime);
end;
范例
procedureTForm1.Button1Click(Sender:TObject);
var
MyTime:TDateTime;
begin
MyTime:=EncodeTime(0,45,45,7);
Label1.Caption:=TimeToStr(MyTime);
end;
--------------------------------------------------------
FormatDateTime将日期时间依Format的格式转换给一字串.
--------------------------------------------------------
UnitSysUtils
函数原型functionFormatDateTime(constFormat:string;DateTime:
TDateTime):string;
****类似DateTimeToString.
Format格式
c内定值ShortDateFormat的格式.(1996/12/2009:20:15PM).
d日期,前面不补0.(1-31)
dd日期,前面补0.(01-31)
ddd星期.(星期日).
Dddd中文2.01版,同上.
ddddd日期.(1996/12/20)
dddddd日期.(1996年12月20日)
m月份,前面不补0.(1-12)
mm月份,前面补0.(01-12)
mmm中文显示.(十二月)
mmmm中文2.01版,同上.
Yy年度.(00-99)
yyyy年度.(0000-9999)
h小时.(0-23)
hh小时.(00-23)
n分钟.(0-59)
nn分钟.(00-59)
s秒钟.(0-59)
ss秒钟.(00-59)
t时间.(09:20PM)
tt时间.(09:20:15PM)
am/pm单独显示amorpm.(若大写,则显示大写)
a/p单独显示aorp.
范例
Thefollowingexampleassigns'ThemeetingisonWednesday,February15,1995at10:30AM'tothestringvariableS.
S:=FormatDateTime('"Themeetingison"dddd,mmmmd,yyyy,"at"hh:mmAM/PM',
StrToDateTime('2/15/9510:30am'));//???
--------------------------------------------------------
Now传回目前的日期时间.
--------------------------------------------------------
UnitSysUtils
函数原型functionNow:TDateTime;
范例
procedureTForm1.Button1Click(Sender:TObject);
begin
Label1.Caption:=DateTimeToStr(Now);
end;
#Now,DateTimeToStrExample
--------------------------------------------------------
StrToDate将字串转为TDateTime型态的日期.
--------------------------------------------------------
UnitSysUtils
函数原型functionStrToDate(constS:string):TDateTime;
范例procedureTForm1.Button1Click(Sender:TObject);
var
ADate:TDateTime;
begin
ADate:=StrToDate(Edit1.Text);
Label1.Caption:=DateToStr(ADate);
end;
范例
procedureTForm1.Button1Click(Sender:TObject);
var
ADate:TDateTime;
days:array[1..7]ofstring;
begin
days[1]:='Sunday';
days[2]:='Monday';
days[3]:='Tuesday';
days[4]:='Wednesday';
days[5]:='Thursday';
days[6]:='Friday';
days[7]:='Saturday';
ADate:=StrToDate(Edit1.Text);
ShowMessage(Edit1.Text+'isa'+days[DayOfWeek(ADate)];
end;
#StrToDate,DayOfWeekExample
--------------------------------------------------------
StrToDateTime将字串转为TDateTime型态的日期时间.
--------------------------------------------------------
UnitSysUtils
函数原型functionStrToDateTime(constS:string):TDateTime;
范例
procedureTForm1.Button1Click(Sender:TObject);
var
ADateAndTime:TDateTime;
begin
ADateAndTime:=StrToDateTime(Edit1.Text);
Table1.FieldByName('TimeStamp').AsDateTime:=ADateAndTime;
end;
--------------------------------------------------------
StrToTime将字串转为TDateTime型态的时间.
--------------------------------------------------------
UnitSysUtils
函数原型functionStrToTime(constS:string):TDateTime;
范例
procedureTForm1.Button1Click(Sender:TObject);
var
ATime:TDateTime;
begin
ATime:=StrToTime(Edit1.Text);
ifATime<0.50then
ShowMessage('GoodMorning')
else
ShowMessage('GoodAfternoon');
end;
--------------------------------------------------------
Time传回目前的时间.
--------------------------------------------------------
UnitSysUtils
函数原型functionTime:TDateTime;
范例
procedureTForm1.Timer1Timer(Sender:TObject);
var
DateTime:TDateTime;
str:string;
begin
DateTime:=Time;//storethecurrentdateandtime
str:=TimeToStr(DateTime);//convertthetimeintoastring
Caption:=str;//displaythetimeontheform'scaption
{NoteThiscouldhavebeendonewiththefollowinglineofcode:
Caption:=TimeToStr(Time);}
end;
#Time,TimeToStrExample
--------------------------------------------------------
TimeToStr时间转换成内定型字串.(09:20:15PM)
--------------------------------------------------------
UnitSysUtils
函数原型functionTimeToStr(Time:TDateTime):string;
GetMemprocedure配置记忆体程序
New配置指位器P的记忆体空间,
大小为P所指型态的大小.
--------------------------------------------------------
Dispose释放New所配置的记忆体.
--------------------------------------------------------
UnitSystem
函数原型procedureNew(varP:Pointer);
函数原型procedureDispose(varP:Pointer);
范例type
PListEntry=^TListEntry;
TListEntry=record
Next:PListEntry;
Text:string;
Count:Integer;
end;
var
List,P:PListEntry;
begin
...
New(P);
P^.Next:=List;
P^.Text:='Helloworld';
P^.Count:=1;
List:=P;
...
Dispose(P);

end;
范例
type
Str18=string[18];
var
P:^Str18;
begin
New(P);
P^:='Nowyouseeit...';
Dispose(P);{Nowyoudon't...}
end;
--------------------------------------------------------
GetMem配置指位器P的记忆体空间,大小可自行设定.
--------------------------------------------------------
范例
var
F:file;
Size:Integer;
Buffer:PChar;
begin
AssignFile(F,'test.txt');
Reset(F,1);
try
Size:=FileSize(F);
GetMem(Buffer,Size);
try
BlockRead(F,Buffer^,Size);
ProcessFile(Buffer,Size);
finally
FreeMem(Buffer);
end;
finally
CloseFile(F);
end;
end;
--------------------------------------------------------
FreeMem释放GetMem所配置的记忆体.
--------------------------------------------------------
UnitSystem
函数原型procedureGetMem(varP:Pointer;Size:Integer);
函数原型procedureFreeMem(varP:Pointer[;Size:Integer]);
范例var
F:file;
Size:Integer;
Buffer:PChar;
begin
AssignFile(F,'test.txt');
Reset(F,1);
try
Size:=FileSize(F);
GetMem(Buffer,Size);
try
BlockRead(F,Buffer^,Size);
ProcessFile(Buffer,Size);
finally
FreeMem(Buffer);
end;
finally
CloseFile(F);
end;
end;

====================================
File-managementroutines档案管理常式
====================================
--------------------------------------------------------
ChangeFileExt变更档案的副档名
--------------------------------------------------------
UnitSysUtils
函数原型functionChangeFileExt(constFileName,Extension:string):
string;
范例procedureTForm1.Button1Click(Sender:TObject);
var
S:String;
P1:String;
P2:String;
begin
P1:='abc.txt';
P2:='.ini';
S:=ChangeFileExt(P1,P2);
Label1.Caption:=S;
end;

结果S=='abc.ini'

P1:='abc'
P2:='.ini'
S=='abc.ini'

P1:='c:/windows/abc.txt'
P2:='.ini'
S=='c:/windows/abc.ini'

P1:='abc.txt'
P2:='ini'
S=='abcini'
**注意:P2的第一位元必须有一点'.ini'
范例
procedureTForm1.ConvertIcon2BitmapClick(Sender:TObject);

var
s:string;
Icon:TIcon;
begin

OpenDialog1.DefaultExt:='.ICO';

OpenDialog1.Filter:='icons(*.ico)|*.ICO';
OpenDialog1.Options:=[ofOverwritePrompt,ofFileMustExist,ofHideReadOnly];
ifOpenDialog1.Executethen
begin
Icon:=TIcon.Create;
try
Icon.Loadfromfile(OpenDialog1.FileName);
s:=ChangeFileExt(OpenDialog1.FileName,'.BMP');
Image1.Width:=Icon.Width;
Image1.Height:=Icon.Height;
Image1.Canvas.Draw(0,0,Icon);
Image1.Picture.SaveToFile(s);

ShowMessage(OpenDialog1.FileName+'Savedto'+s);
finally
Icon.Free;
end;
end;
end;
#SaveToFile,Create,Height,Width,Canvas,ChangeFileExtexample
--------------------------------------------------------
ExpandFileName将档案名称加在目前所在之路径全名之後
--------------------------------------------------------
UnitSysUtils
函数原型functionExpandFileName(constFileName:string):string;
说明设目前目录为c:/windows档案名称为abc.txt
则结果为c:/windows/abc.txt
****此函数并不是求abc.txt的所在路径.
范例procedureTForm1.Button1Click(Sender:TObject);
var
S:String;
begin
S:=ExpandFileName('abc.txt');
Label1.Caption:=S;
end;
范例
procedureTForm1.Button1Click(Sender:TObject)
begin
ListBox1.Items.Add(ExpandFileName(Edit1.Text));
end;

------------------------------------------------------------------
DirectoryExists目录是否存在------------------------------------------------------------------
Unit
FileCtrl

usesFileCtrl;

procedureTForm1.Button1Click(Sender:TObject);
begin
ifnotDirectoryExists('c:/temp')then
ifnotCreateDir('C:/temp')then
raiseException.Create('Cannotcreatec:/temp');
end;
--------------------------------------------------------
ForceDirectories目录
---------------------------------------------------------
UnitFileCtrl
函数原型functionForceDirectories(Dir:string):Boolean;

procedureTForm1.Button1Click(Sender:TObject);
var
Dir:string;
begin
Dir:='C:/APPS/SALES/LOCAL';
ifDirectoryExists(Dir)then
Label1.Caption:=Dir+'wascreated'
end;
--------------------------------------------------------
ExpandUNCFileName同上(只是得到网路上的路径)
--------------------------------------------------------
UnitSysUtils
函数原型functionExpandUNCFileName(constFileName:string):string;
ExtractFileDir分析字串中的路径
UnitSysUtils
函数原型functionExtractFileDir(constFileName:string):string;
说明设S字串为c:/windows/abc.txt
则结果为c:/windows
****功能在於由任何部份传来的叁数,加以分析它的路径
范例procedureTForm1.Button1Click(Sender:TObject);
var
S:String;
P1:String;
begin
P1:='c:/windows/abc.txt';
S:=ExtractFileDir(P1);
Label1.Caption:=S;
end;

S=='c:/windows'

P1:='abc.txt'
S=='

P1:='c:abc.txt'
S=='c:'

P1:='c:/abc.txt'
S=='c:/'
--------------------------------------------------------
ExtractFileDrive分析字串中的磁碟机名称
--------------------------------------------------------
UnitSysUtils
函数原型functionExtractFileDrive(constFileName:string):string;
****功能同上,只是传回磁碟机名称.
范例procedureTForm1.Button1Click(Sender:TObject);
var
S:String;
P1:String;
begin
P1:='c:/windows/abc.txt';
S:=ExtractFileDrive(P1);
Label1.Caption:=S;
end;

S:='c:'

P1:='abc.txt'
S=='
--------------------------------------------------------
ExtractFileExt分析字串中的档案名称的副档名
--------------------------------------------------------
UnitSysUtils
函数原型functionExtractFileExt(constFileName:string):string;
范例procedureTForm1.Button1Click(Sender:TObject);
var
S:String;
P1:String;
begin
P1:='c:/windows/abc.txt';
S:=ExtractFileExt(P1);
Label1.Caption:=S;
end;

S=='.txt'

P1:='c:/windows/abc'
S=='
范例MyFilesExtension:=ExtractFileExt(MyFileName);
--------------------------------------------------------
ExtractFileName分析字串中的档案名称(只传回档案名称)
--------------------------------------------------------
UnitSysUtils
函数原型functionExtractFileName(constFileName:string):string;
范例procedureTForm1.Button1Click(Sender:TObject);
var
S:String;
P1:String;
begin
P1:='c:/windows/abc.txt';
S:=ExtractFileName(P1);
Label1.Caption:=S;
end;

S=='abc.txt'
范例
procedureTForm1.Button1Click(Sender:TObject);
var
BackupName:string;
FileHandle:Integer;
StringLen:Integer;
X:Integer;
Y:Integer;
begin
ifSaveDialog1.Executethen
begin
ifFileExists(SaveDialog1.FileName)then
begin
BackupName:=ExtractFileName(SaveDialog1.FileName);
BackupName:=ChangeFileExt(BackupName,'.BAK');
ifnotRenameFile(SaveDialog1.FileName,BackupName)then
raiseException.Create('Unabletocreatebackupfile.');
end;
FileHandle:=FileCreate(SaveDialog1.FileName);
{Writeoutthenumberofrowsandcolumnsinthegrid.}
FileWrite(FileHandle,
StringGrid1.ColCount,SizeOf(StringGrid1.ColCount));
FileWrite(FileHandle,
StringGrid1.RowCount,SizeOf(StringGrid1.RowCount));
forX:=0toStringGrid1.ColCount?1do
begin
forY:=0toStringGrid1.RowCount?1do
begin
{Writeoutthelengthofeachstring,followedbythestringitself.}
StringLen:=Length(StringGrid1.Cells[X,Y]);
FileWrite(FileHandle,StringLen,SizeOf(StringLen));
FileWrite(FileHandle,
StringGrid1.Cells[X,Y],Length(StringGrid1.Cells[X,Y]);
end;
end;
FileClose(FileHandle);
end;
end;
##FileExists,RenameFile,FileCreate,FileWrite,FileClose,ExtractFileNameExample
--------------------------------------------------------
ExtractFilePath分析字串中的路径
--------------------------------------------------------
UnitSysUtils
函数原型functionExtractFilePath(constFileName:string):string;
说明设S字串为c:/windows/abc.txt
则结果为c:/windows范例procedureTForm1.Button1Click(Sender:TObject);
var
S:String;
P1:String;
begin
P1:='c:/windows/abc.txt';
S:=ExtractFilePath(P1);
Label1.Caption:=S;
end;
范例
begin
withSessiondo
begin
ConfigMode:=cmSession;
try
AddStandardAlias('TEMPDB',ExtractFilePath(ParamStr(0)),'PARADOX');
finally
ConfigMode:=cmAll;
end;
end;
##ConfigMode,AddStandardAlias,ExtractFilePathexample
--------------------------------------------------------
FileSearch寻找档案在磁碟机中的正确路径
--------------------------------------------------------
UnitSysUtils
函数原型functionFileSearch(constName,DirList:string):string;
范例var
s:string;
begin
s:=FileSearch('abc.txt','c:/window/');
Label1.Caption:=s;
end;
说明找到传回c:/window/abc.txt找不到传回空字串.
范例
procedureTForm1.Button1Click(Sender:TObject);
var
buffer:array[0..255]ofchar;
FileToFind:string;
begin
GetWindowsDirectory(buffer,SizeOf(buffer));
FileToFind:=FileSearch(Edit1.Text,GetCurrentDir+';'+buffer);
ifFileToFind='then
ShowMessage('Couldn'tfind'+Edit1.Text+'.')
else
ShowMessage('Found'+FileToFind+'.');
end;
##FileSearch,ShowMessageExample
--------------------------------------------------------
FileAge传回档案的日期及时间(DOS型态).
--------------------------------------------------------
UnitSysUtils
函数原型functionFileAge(constFileName:string):Integer;
说明就是档案总管中档案内容裹面的修改日期.
范例procedureTForm1.Button1Click(Sender:TObject);
var
S:String;
FileDate1:Integer;
DateTime1:TDateTime;
begin
FileDate1:=FileAge('c:/delphi_d/delphi_help1.txt');
DateTime1:=FileDateToDateTime(FileDate1);
S:=DateTimeToStr(DateTime1);
Label1.Caption:=S;
end;
--------------------------------------------------------
FileDateToDateTime将DOS型态的日期时间转换为TDateTime型态.
--------------------------------------------------------
UnitSysUtils
函数原型functionFileDateToDateTime(FileDate:Integer):TDateTime;
-----------------------------------------------------------------------------
DateTimeToFileDate将TDateTime型态的日期时间转换为DOS型态.
-----------------------------------------------------------------------------
UnitSysUtils
函数原型functionDateTimeToFileDate(DateTime:TDateTime):Integer;
FileGetDate传回档案的日期及时间(DOS型态).
UnitSysUtils
函数原型functionFileGetDate(Handle:Integer):Integer;
说明就是档案总管中档案内容裹面的修改日期.
范例procedureTForm1.Button1Click(Sender:TObject);
var
FileHandle:Integer;
S:String;
FileDate1:Integer;
DateTime1:TDateTime;
begin
FileHandle:=FileOpen('c:/delphi_d/delphi_help2.txt',
fmOpenReadWrite);
ifFileHandle>0then
Begin
FileDate1:=FileGetDate(FileHandle);
DateTime1:=FileDateToDateTime(FileDate1);
S:=DateTimeToStr(DateTime1);
FileClose(FileHandle);
End
else
S:='OpenFileError';
Label1.Caption:=S;
end;
-----------------------------------------------------------------------------
FileSetDate设定档案的日期及时间(DOS型态).
-----------------------------------------------------------------------------
UnitSysUtils
函数原型functionFileSetDate(Handle:Integer;Age:Integer):Integer;
说明传回值为0表示成功.
-----------------------------------------------------------------------------
DeleteFile删除档案
-----------------------------------------------------------------------------
UnitSysUtils
函数原型functionDeleteFile(constFileName:string):Boolean;
范例一DeleteFile('DELETE.ME');

范例二ifFileExists(FileName)then
ifMessageDlg('Doyoureallywanttodelete'+
ExtractFileName(FileName)+'?'),[])=IDYesthen
DeleteFile(FileName);
##FileExists,DeleteFileExample
-----------------------------------------------------------------------------
RenameFile更改档名
-----------------------------------------------------------------------------
UnitSysUtils
函数原型functionRenameFile(constOldName,NewName:string):Boolean;
范例
procedureTForm1.Button1Click(Sender:TObject);
var
BackupName:string;
FileHandle:Integer;
StringLen:Integer;
X:Integer;
Y:Integer;
begin
ifSaveDialog1.Executethen
begin
ifFileExists(SaveDialog1.FileName)then
begin
BackupName:=ExtractFileName(SaveDialog1.FileName);
BackupName:=ChangeFileExt(BackupName,'.BAK');
ifnotRenameFile(SaveDialog1.FileName,BackupName)then
raiseException.Create('Unabletocreatebackupfile.');
end;
FileHandle:=FileCreate(SaveDialog1.FileName);
{Writeoutthenumberofrowsandcolumnsinthegrid.}
FileWrite(FileHandle,
StringGrid1.ColCount,SizeOf(StringGrid1.ColCount));
FileWrite(FileHandle,
StringGrid1.RowCount,SizeOf(StringGrid1.RowCount));
forX:=0toStringGrid1.ColCount?1do
begin
forY:=0toStringGrid1.RowCount?1do
begin
{Writeoutthelengthofeachstring,followedbythestringitself.}
StringLen:=Length(StringGrid1.Cells[X,Y]);
FileWrite(FileHandle,StringLen,SizeOf(StringLen));
FileWrite(FileHandle,
StringGrid1.Cells[X,Y],Length(StringGrid1.Cells[X,Y]);
end;
end;
FileClose(FileHandle);
end;
end;
-----------------------------------------------------------------------------
DiskFree磁碟机剩馀空间(Bytes)
-----------------------------------------------------------------------------
UnitSysUtils
函数原型functionDiskFree(Drive:Byte):Integer;
范例var
S:string;
begin
S:=IntToStr(DiskFree(0)div1024)+'Kbytesfree.';
Label1.Caption:=S;
end;
说明Drive
0=目前磁碟机,1=A磁碟机,2=B磁碟机...传回值若为-1,表示磁碟机侦测错误.
范例
var
S:string;
AmtFree:Int64;
Total:Int64;
begin
AmtFree:=DiskFree(0);
Total:=DiskSize(0);
S:=IntToStr(AmtFreedivTotal)+'percentofthespaceondrive0isfree:'(AmtFreediv1024)+'Kbytesfree.';
Canvas.TextOut(10,10,S);
end;
##DiskFree,DiskSizeExample
-----------------------------------------------------------------------------
DiskSize磁碟机空间大小(Bytes)
-----------------------------------------------------------------------------
UnitSysUtils
函数原型functionDiskSize(Drive:Byte):Integer;
范例var
S:string;
begin
S:=IntToStr(DiskSize(0)div1024)+'Kbytesfree.';
Label1.Caption:=S;
end;
说明Drive
0=目前磁碟机,1=A磁碟机,2=B磁碟机....传回值若为-1,表示磁碟机侦测错误.
##DiskFree,DiskSizeExample
-----------------------------------------------------------------------------
FileExists判断档案是否存在.
-----------------------------------------------------------------------------
UnitSysUtils
函数原型functionFileExists(constFileName:string):Boolean;
类似FileExists,RenameFile,FileCreate,FileWrite,FileClose,ExtractFileNameExample
范例
procedureTForm1.Button1Click(Sender:TObject);
var
BackupName:string;
FileHandle:Integer;
StringLen:Integer;
X:Integer;
Y:Integer;
begin
ifSaveDialog1.Executethen
begin
ifFileExists(SaveDialog1.FileName)then
begin
BackupName:=ExtractFileName(SaveDialog1.FileName);
BackupName:=ChangeFileExt(BackupName,'.BAK');
ifnotRenameFile(SaveDialog1.FileName,BackupName)then
raiseException.Create('Unabletocreatebackupfile.');
end;
FileHandle:=FileCreate(SaveDialog1.FileName);
{Writeoutthenumberofrowsandcolumnsinthegrid.}
FileWrite(FileHandle,
StringGrid1.ColCount,SizeOf(StringGrid1.ColCount));
FileWrite(FileHandle,
StringGrid1.RowCount,SizeOf(StringGrid1.RowCount));
forX:=0toStringGrid1.ColCount?1do
begin
forY:=0toStringGrid1.RowCount?1do
begin
{Writeoutthelengthofeachstring,followedbythestringitself.}
StringLen:=Length(StringGrid1.Cells[X,Y]);
FileWrite(FileHandle,StringLen,SizeOf(StringLen));
FileWrite(FileHandle,
StringGrid1.Cells[X,Y],Length(StringGrid1.Cells[X,Y]);
end;
end;
FileClose(FileHandle);
end;
end;
##FileExists,DeleteFileExample
##FileExists,RenameFile,FileCreate,FileWrite,FileClose,ExtractFileNameExample
-----------------------------------------------------------------------------
FileOpen开档.
-----------------------------------------------------------------------------
UnitSysUtils
函数原型functionFileOpen(constFileName:string;Mode:
Integer):Integer;
****开档失败传回-1.
说明以下有关档案读取都属低阶,如DosInt21h中有关档案的部
分.
fmOpenRead=$0000;
fmOpenWrite=$0001;
fmOpenReadWrite=$0002;
fmShareCompat=$0000;
fmShareExclusive=$0010;
fmShareDenyWrite=$0020;
fmShareDenyRead=$0030;
fmShareDenyNone=$0040;

fmOpenReadOpenforreadaccessonly.
FmOpenWriteOpenforwriteaccessonly.
FmOpenReadWriteOpenforreadandwriteaccess.
fmShareCompatCompatiblewiththewayFCBsare
opened.
fmShareExclusiveReadandwriteaccessisdenied.
fmShareDenyWriteWriteaccessisdenied.
fmShareDenyReadReadaccessisdenied.
fmShareDenyNoneAllowsfullaccessforothers.
范例
procedureOpenForShare(constFileName:String);
var
FileHandle:Integer;
begin
FileHandle:=FileOpen(FileName,fmOpenWriteorfmShareDenyNone);
ifFileHandle>0then
{validfilehandle}
else
{Openerror:FileHandle=negativeDOSerrorcode}
end;
范例
procedureTForm1.Button1Click(Sender:TObject);
var
iFileHandle:Integer;
iFileLength:Integer;
iBytesRead:Integer;
Buffer:PChar;
i:Integer
begin
ifOpenDialog1.Executethen
begin
try
iFileHandle:=FileOpen(OpenDialog1.FileName,fmOpenRead);
iFileLength:=FileSeek(iFileHandle,0,2);
FileSeek(iFileHandle,0,0);
Buffer:=PChar(AllocMem(iFileLength+1));
iBytesRead=FileRead(iFileHandle,Buffer,iFileLength);
FileClose(iFileHandle);
fori:=0toiBytesRead-1do
begin
StringGrid1.RowCount:=StringGrid1.RowCount+1;
StringGrid1.Cells[1,i+1]:=Buffer[i];
StringGrid1.Cells[2,i+1]:=IntToStr(Integer(Buffer[i]));
end;
finally
FreeMem(Buffer);
end;
end;
end;
##FileOpen,FileSeek,FileReadExample
-----------------------------------------------------------------------------
FileCreate建档
-----------------------------------------------------------------------------
UnitSysUtils
函数原型functionFileCreate(constFileName:string):Integer;

范例
procedureTForm1.Button1Click(Sender:TObject);
var
BackupName:string;
FileHandle:Integer;
StringLen:Integer;
X:Integer;
Y:Integer;
begin
ifSaveDialog1.Executethen
begin
ifFileExists(SaveDialog1.FileName)then
begin
BackupName:=ExtractFileName(SaveDialog1.FileName);
BackupName:=ChangeFileExt(BackupName,'.BAK');
ifnotRenameFile(SaveDialog1.FileName,BackupName)then

raiseException.Create('Unabletocreatebackupfile.');
end;
FileHandle:=FileCreate(SaveDialog1.FileName);
{Writeoutthenumberofrowsandcolumnsinthegrid.}
FileWrite(FileHandle,
StringGrid1.ColCount,SizeOf(StringGrid1.ColCount));
FileWrite(FileHandle,
StringGrid1.RowCount,SizeOf(StringGrid1.RowCount));
forX:=0toStringGrid1.ColCount?1do
begin

forY:=0toStringGrid1.RowCount?1do
begin
{Writeoutthelengthofeachstring,followedbythestringitself.}
StringLen:=Length(StringGrid1.Cells[X,Y]);
FileWrite(FileHandle,StringLen,SizeOf(StringLen));
FileWrite(FileHandle,
StringGrid1.Cells[X,Y],Length(StringGrid1.Cells[X,Y]);
end;
end;
FileClose(FileHandle);
end;

end;
##FileExists,RenameFile,FileCreate,FileWrite,FileClose,ExtractFileNameExample
-----------------------------------------------------------------------------
FileClose关档
-----------------------------------------------------------------------------
UnitSysUtils
函数原型procedureFileClose(Handle:Integer);
范例
procedureTForm1.Button1Click(Sender:TObject);
var
BackupName:string;
FileHandle:Integer;
StringLen:Integer;
X:Integer;
Y:Integer;
begin
ifSaveDialog1.Executethen
begin
ifFileExists(SaveDialog1.FileName)then
begin
BackupName:=ExtractFileName(SaveDialog1.FileName);
BackupName:=ChangeFileExt(BackupName,'.BAK');
ifnotRenameFile(SaveDialog1.FileName,BackupName)then
raiseException.Create('Unabletocreatebackupfile.');
end;
FileHandle:=FileCreate(SaveDialog1.FileName);
{Writeoutthenumberofrowsandcolumnsinthegrid.}
FileWrite(FileHandle,
StringGrid1.ColCount,SizeOf(StringGrid1.ColCount));
FileWrite(FileHandle,
StringGrid1.RowCount,SizeOf(StringGrid1.RowCount));
forX:=0toStringGrid1.ColCount?1do
begin
forY:=0toStringGrid1.RowCount?1do
begin
{Writeoutthelengthofeachstring,followedbythestringitself.}
StringLen:=Length(StringGrid1.Cells[X,Y]);
FileWrite(FileHandle,StringLen,SizeOf(StringLen));
FileWrite(FileHandle,
StringGrid1.Cells[X,Y],Length(StringGrid1.Cells[X,Y]);
end;
end;
FileClose(FileHandle);
end;
end;
##FileExists,RenameFile,FileCreate,FileWrite,FileClose,ExtractFileNameExample

============================================
****它是以Handle为叁数.
============================================
FileRead读取档案
-----------------------------------------------------------------------------
UnitSysUtils
函数原型functionFileRead(Handle:Integer;varBuffer;Count:Integer):Integer;
范例
procedureTForm1.Button1Click(Sender:TObject);

var
iFileHandle:Integer;
iFileLength:Integer;
iBytesRead:Integer;
Buffer:PChar;
i:Integer
begin
ifOpenDialog1.Executethen
begin
try
iFileHandle:=FileOpen(OpenDialog1.FileName,fmOpenRead);
iFileLength:=FileSeek(iFileHandle,0,2);
FileSeek(iFileHandle,0,0);
Buffer:=PChar(AllocMem(iFileLength+1));
iBytesRead=FileRead(iFileHandle,Buffer,iFileLength);

本文地址:http://www.45fan.com/a/question/71696.html
Tags: Delphi 范例 数集
编辑:路饭网
关于我们 | 联系我们 | 友情链接 | 网站地图 | Sitemap | App | 返回顶部