利用
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;