delphi定义结构体

//结构体的比较普遍的两用法,具体还是得靠多看例子,帮助等。

//1.作为一个对象结构型使用

type

TclassRecord = record

rDate : string;

rTime : string;

end;

//2.作为记录类型(数据协议解析时比较多使用,灵活方便)

//首先枚举几种数据类型

type TDateType =(TFullData,TRedDate,TReadTime);

type

TtextRecord = record

case i : TDateType of

//FullData与下面所有的变量,共享同一个内存区域,各个子类自己定义就可以了,i并无实际意义。

//TtextRecord作为一个字符串的形式存在内存中

TFullData :(fulldata : array[0..19]of char);

TRedDate :

(

rYear : array[0..1]of char; // rYear的值就是 TFullData 的第1,第2位

rMonth: array[0..1]of char; // rMonth的值就是 TFullData 的第3,第4位

rDay : array[0..1]of char; // rDay的值就是 TFullData 的第5,第6位

);

TReadTime :

(

rHour : array[0..1]of char; //rHour的值也是 TFullData 的第1,第2位

rMinute : array[0..1]of char; //rMinute的值也是 TFullData 的第3,第4位

rSecs : array[0..1]of char; // rSecs的值也是 TFullData 的第5,第6位

);

end;

var

Form1: TForm1;

GvTdatetimerecord : ^Tclassrecord;

GvTtextRecord : ^TtextRecord;

implementation

{$R *.dfm}

//----------作为对象类型使用-----------

procedure TForm1.Button1Click(Sender: TObject);

begin

New(GvTdatetimerecord); //分配内存,按F1翻译吧

GvTdatetimerecord.rDate := DateToStr(now);

GvTdatetimerecord.rTime := TimeToStr(now);

ShowMessage('当前日期时间:'+GvTdatetimerecord.rDate+GvTdatetimerecord.rTime);

dispose(GvTdatetimerecord); //释放

end;

//---------------作为记录体使用----------

procedure TForm1.Button2Click(Sender: TObject);

begin

new(GvTtextRecord);

// 赋值

fillchar(GvTtextRecord.fulldata,Sizeof(GvTtextRecord.fulldata),0);

StrCopy(GvTtextRecord.fulldata,Pchar(FormatDateTime('yyMMDD',now)));

showmessage(GvTtextRecord.fulldata);

showmessage('年份:'+GvTtextRecord.rYear);

//这些子结构体的值你可以尝试字做些例子,比较能理解记录类型的作用

// fillchar(GvTtextRecord.fulldata,Sizeof(GvTtextRecord.fulldata),0);

// StrCopy(GvTtextRecord.fulldata,Pchar(FormatDateTime('hhmmss',now)));

// showmessage('小时:'+GvTtextRecord.rHour);

dispose(GvTtextRecord)

end;

//结构体的比较普遍的两用法,具体还是得靠多看例子,帮助等。

//1.作为一个对象结构型使用

type

TclassRecord = record

rDate : string;

rTime : string;

end;

//2.作为记录类型(数据协议解析时比较多使用,灵活方便)

//首先枚举几种数据类型

type TDateType =(TFullData,TRedDate,TReadTime);

type

TtextRecord = record

case i : TDateType of

//FullData与下面所有的变量,共享同一个内存区域,各个子类自己定义就可以了,i并无实际意义。

//TtextRecord作为一个字符串的形式存在内存中

TFullData :(fulldata : array[0..19]of char);

TRedDate :

(

rYear : array[0..1]of char; // rYear的值就是 TFullData 的第1,第2位

rMonth: array[0..1]of char; // rMonth的值就是 TFullData 的第3,第4位

rDay : array[0..1]of char; // rDay的值就是 TFullData 的第5,第6位

);

TReadTime :

(

rHour : array[0..1]of char; //rHour的值也是 TFullData 的第1,第2位

rMinute : array[0..1]of char; //rMinute的值也是 TFullData 的第3,第4位

rSecs : array[0..1]of char; // rSecs的值也是 TFullData 的第5,第6位

);

end;

var

Form1: TForm1;

GvTdatetimerecord : ^Tclassrecord;

GvTtextRecord : ^TtextRecord;

implementation

{$R *.dfm}

//----------作为对象类型使用-----------

procedure TForm1.Button1Click(Sender: TObject);

begin

New(GvTdatetimerecord); //分配内存,按F1翻译吧

GvTdatetimerecord.rDate := DateToStr(now);

GvTdatetimerecord.rTime := TimeToStr(now);

ShowMessage('当前日期时间:'+GvTdatetimerecord.rDate+GvTdatetimerecord.rTime);

dispose(GvTdatetimerecord); //释放

end;

//---------------作为记录体使用----------

procedure TForm1.Button2Click(Sender: TObject);

begin

new(GvTtextRecord);

// 赋值

fillchar(GvTtextRecord.fulldata,Sizeof(GvTtextRecord.fulldata),0);

StrCopy(GvTtextRecord.fulldata,Pchar(FormatDateTime('yyMMDD',now)));

showmessage(GvTtextRecord.fulldata);

showmessage('年份:'+GvTtextRecord.rYear);

//这些子结构体的值你可以尝试字做些例子,比较能理解记录类型的作用

// fillchar(GvTtextRecord.fulldata,Sizeof(GvTtextRecord.fulldata),0);

// StrCopy(GvTtextRecord.fulldata,Pchar(FormatDateTime('hhmmss',now)));

// showmessage('小时:'+GvTtextRecord.rHour);

dispose(GvTtextRecord)

end;