利用TypInfo单元的GetEnumName和GetEnumValue可以遍历任意枚举类型,并获取其名称和值。下面是示例Demo。
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;

本文介绍了一个使用Delphi中的TypInfo单元来遍历枚举类型的示例。通过GetEnumName和GetEnumValue函数,可以获取枚举类型的名称和值,并将其添加到ListBox中显示。
183

被折叠的 条评论
为什么被折叠?



