飞碟说男性自慰手册:string如何转化为TFieldType
来源:百度文库 编辑:九乡新闻网 时间:2024/04/28 14:49:57
uses
TypInfo;
例子:
type
TFruits = (apples, pears);
procedure FruitBeep (const fruitName : string);
var
Fruit : TFruits;
begin
Fruit := TFruits(GetEnumValue(TypeInfo(TFruits),fruitName));
case Fruit of
apples : Windows.Beep (440, 100);
pears : Windows.Beep (660, 100);
end
end;
利用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;
procedure TForm1.btn1Click(Sender: TObject);
var
pt: PTypeInfo;
begin
pt:=TypeInfo(TTestEnum);
Caption:=GetEnumName(pt,Ord(teTwo));
end;
procedure TForm1.btn2Click(Sender: TObject);
var
pt: PTypeInfo;
begin
pt:=TypeInfo(TTestEnum);
Caption:=IntToStr(GetEnumValue(pt,'teTwo'));
end;