一)首先列出用到的几個関数
・用「GetEnumValue」関数取出字符在枚挙型中対応的枚挙値(Integer型)。
function GetEnumValue(TypeInfo: PTypeInfo; const Name: String): Integer;
・用「GetEnumName」関数取出枚挙値在枚挙型中対応的枚挙名。
function GetEnumName(TypeInfo: PTypeInfo; Value: Integer): String;
・用「Include」関数向集合挿入元素、相当于 S := S + [I]
procedure Include(var S: set of T; I:T);
二)挙例
為了直観的顕示出集合中的内容、以Delphi中存在的「TMsgDlgButtons」集合為例。
・把一個字符串挿入某集合中。
如: 'mbYes, mbNo, mbOK, mbCancel, mbIgnore' → [mbYes, mbNo, mbOK, mbCancel, mbIgnore]
・把集合中的内容以字符串的形式表示出来。
如: [mbYes, mbNo, mbOK, mbCancel, mbIgnore] → 'mbYes, mbNo, mbOK, mbCancel, mbIgnore'
function GetEnumValue(TypeInfo: PTypeInfo; const Name: String): Integer;
・用「GetEnumName」関数取出枚挙値在枚挙型中対応的枚挙名。
function GetEnumName(TypeInfo: PTypeInfo; Value: Integer): String;
・用「Include」関数向集合挿入元素、相当于 S := S + [I]
procedure Include(var S: set of T; I:T);
二)挙例
為了直観的顕示出集合中的内容、以Delphi中存在的「TMsgDlgButtons」集合為例。
・把一個字符串挿入某集合中。
如: 'mbYes, mbNo, mbOK, mbCancel, mbIgnore' → [mbYes, mbNo, mbOK, mbCancel, mbIgnore]
・把集合中的内容以字符串的形式表示出来。
如: [mbYes, mbNo, mbOK, mbCancel, mbIgnore] → 'mbYes, mbNo, mbOK, mbCancel, mbIgnore'
点撃此処 Show Code
// 「TMsgDlgButtons」在Delphi中的定義
type
TMsgDlgBtn = (mbYes, mbNo, mbOK, mbCancel, mbAbort, mbRetry, mbIgnore, mbAll, mbNoToAll, mbYesToAll, mbHelp);
TMsgDlgButtons = set of TMsgDlgBtn;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses
TypInfo;
// String→集合
procedure TForm1.Button1Click(Sender: TObject);
var
StrList: TStringList;
MsgDlgBtns: TMsgDlgButtons;
Str: String;
i: Integer;
begin
Str := 'mbYes, mbNo, mbOK, mbCancel, mbIgnore';
StrList := TStringList.Create;
try
ExtractStrings([','], [' '], PChar(Str), StrList);
for i:=0 to StrList.Count-1 do
Include(MsgDlgBtns, TMsgDlgBtn(GetEnumValue(TypeInfo(TMsgDlgBtn), StrList.Strings[i])));
MessageDlg('確認',mtConfirmation, MsgDlgBtns, 0);
finally
StrList.Free;
end;
end;
// 集合→String
procedure TForm1.Button2Click(Sender: TObject);
var
MsgDlgEnum: TMsgDlgBtn;
MsgDlgBtns: TMsgDlgButtons;
Str: String;
begin
MsgDlgBtns := [mbYes, mbNo, mbOK, mbCancel, mbIgnore];
for MsgDlgEnum := Low(TMsgDlgBtn) to High(TMsgDlgBtn) do
if MsgDlgEnum in MsgDlgBtns then
Str := Str + GetEnumName(TypeInfo(TMsgDlgBtn), Ord(MsgDlgEnum)) + ',';
ShowMessage(Str);
end;
type
TMsgDlgBtn = (mbYes, mbNo, mbOK, mbCancel, mbAbort, mbRetry, mbIgnore, mbAll, mbNoToAll, mbYesToAll, mbHelp);
TMsgDlgButtons = set of TMsgDlgBtn;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses
TypInfo;
// String→集合
procedure TForm1.Button1Click(Sender: TObject);
var
StrList: TStringList;
MsgDlgBtns: TMsgDlgButtons;
Str: String;
i: Integer;
begin
Str := 'mbYes, mbNo, mbOK, mbCancel, mbIgnore';
StrList := TStringList.Create;
try
ExtractStrings([','], [' '], PChar(Str), StrList);
for i:=0 to StrList.Count-1 do
Include(MsgDlgBtns, TMsgDlgBtn(GetEnumValue(TypeInfo(TMsgDlgBtn), StrList.Strings[i])));
MessageDlg('確認',mtConfirmation, MsgDlgBtns, 0);
finally
StrList.Free;
end;
end;
// 集合→String
procedure TForm1.Button2Click(Sender: TObject);
var
MsgDlgEnum: TMsgDlgBtn;
MsgDlgBtns: TMsgDlgButtons;
Str: String;
begin
MsgDlgBtns := [mbYes, mbNo, mbOK, mbCancel, mbIgnore];
for MsgDlgEnum := Low(TMsgDlgBtn) to High(TMsgDlgBtn) do
if MsgDlgEnum in MsgDlgBtns then
Str := Str + GetEnumName(TypeInfo(TMsgDlgBtn), Ord(MsgDlgEnum)) + ',';
ShowMessage(Str);
end;
注:此程序在Delphi7上編集通過的。