您的位置:首页 > 编程语言 > Delphi

Delphi中的GetEnumName和GetEnumValue的使用方法

2014-04-06 08:03 246 查看
利用TypInfo单元的GetEnumName和GetEnumValue可以遍历任意枚举类型,并获取其名称和值。下面是示例Demo。

uses TypInfo;

...

procedure TForm1.btnTestClick(Sender: TObject);
var
p: PTypeData;
i: Integer;
s: String;
pt: PTypeInfo;
begin
ListBox1.Items.Clear;
pt := TypeInfo(TWindowState);
if pt.Kind <> tkEnumeration then begin
ShowMessage('不是枚举类型');
Exit;
end;

p := GetTypeData(TypeInfo(TWindowState));

//将获取的枚举类型信息,以枚举名=枚举值的形式加入到ListBox中
ListBox1.Items.beginUpdate;
try
for i := p.MinValue to p.MaxValue do begin
S := GetEnumName(pt,i);
ListBox1.Items.Values[S] := IntToStr(GetEnumValue(pt, S));
end;
finally
ListBox1.Items.EndUpdate;
end;
end;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: