`
禹爸爸
  • 浏览: 79884 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

获取Delphi中定义的枚举类型的名称和值

阅读更多

利用TypInfo单元的GetEnumNameGetEnumValue可以遍历任意枚举类型,并获取其名称和值。下面是示例Demo。

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

p:
=GetTypeData(TypeInfo(TWindowState));

//将获取的枚举类型信息,以枚举名=枚举值的形式加入到ListBox中
ListBox1.Items.beginUpdate;
try
fori:=p.MinValuetop.MaxValuedobegin
S:
=GetEnumName(pt,i);
ListBox1.Items.Values[S]:
=IntToStr(GetEnumValue(pt,S));
end;
finally
ListBox1.Items.EndUpdate;
end;
end;

版权声明:本文为博主原创文章,未经博主允许不得转载。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics