非常简单...您需要将变量 MineralWalkable 保存为您的 eBlockTypes 的字符串表示形式。您将使用 GetEnumName 来执行此操作。然后,您需要将 eBlockTypes 的字符串表示形式转换为实际的 eBlockType,然后将其添加到您的 MineralWalkable。您将使用 GetEnumValue 来执行此操作。
以下示例显示了获取字符串表示...将其放入集合中...然后获取集合...并将其移回字符串...
object Form54: TForm54
Left = 0
Top = 0
Caption = 'Form54'
ClientHeight = 290
ClientWidth = 554
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
OnCreate = FormCreate
OnDestroy = FormDestroy
PixelsPerInch = 96
TextHeight = 13
object Button1: TButton
Left = 56
Top = 160
Width = 75
Height = 25
Caption = 'Button1'
TabOrder = 0
OnClick = Button1Click
end
object Edit1: TEdit
Left = 56
Top = 64
Width = 265
Height = 21
TabOrder = 1
Text = 'Edit1'
end
object cbType1: TCheckBox
Left = 248
Top = 91
Width = 97
Height = 17
Caption = 'Type1'
TabOrder = 2
end
object cbType2: TCheckBox
Tag = 1
Left = 248
Top = 114
Width = 97
Height = 17
Caption = 'Type2'
TabOrder = 3
end
object cbType3: TCheckBox
Tag = 2
Left = 248
Top = 137
Width = 97
Height = 17
Caption = 'Type3'
TabOrder = 4
end
end
unit Unit54;
{Note the code assumes cbType1.Tag = 0, cbType2.Tag = 1, and cbType3.Tag = 2}
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, TypInfo, IniFiles;
type
TMyType = (mtOne, mtTwo, mtThree);
TMyTypes= set of TMyType;
TForm54 = class(TForm)
Button1: TButton;
Edit1: TEdit;
cbType1: TCheckBox;
cbType2: TCheckBox;
cbType3: TCheckBox;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
function getTypes1: TMyTypes;
procedure setMyTypes1(const Value: TMyTypes);
{ Private declarations }
public
{ Public declarations }
property Types1: TMyTypes read getTypes1 write setMyTypes1;
procedure SaveMyTypes(aVariableName: string; aMyTypes: TMyTypes);
function ReadMyTypes(aVariableName: string): TMyTypes;
end;
var
Form54: TForm54;
implementation
{$R *.dfm}
procedure TForm54.Button1Click(Sender: TObject);
var
a_MyTypes: TMyTypes;
a_Str: string;
a_Index: integer;
begin
a_MyTypes := [];
a_Str := '';
Include(a_MyTypes, TMyType(GetEnumValue(TypeInfo(TMyType), 'mtOne')));
Include(a_MyTypes, TMyType(GetEnumValue(TypeInfo(TMyType), 'mtTwo')));
//purpoesly have mtThree3 instead of mtThree
Include(a_MyTypes, TMyType(GetEnumValue(TypeInfo(TMyType), 'mtThree3')));
for a_Index := Ord(Low(TMyType)) to Ord(High(TMyType)) do
if TMyType(a_Index) in a_MyTypes then
if a_Str = '' then
a_Str := GetEnumName(TypeInfo(TMyType), a_Index)
else
a_Str := a_Str + ',' + GetEnumName(TypeInfo(TMyType), a_Index);
//should be mtOne, mtTwo
Edit1.Text := a_Str;
end;
procedure TForm54.FormCreate(Sender: TObject);
begin
Types1 := ReadMyTypes('Types1');
end;
procedure TForm54.FormDestroy(Sender: TObject);
begin
SaveMyTypes('Types1', Types1);
end;
function TForm54.getTypes1: TMyTypes;
var
a_Index: integer;
begin
Result := [];
for a_Index := 0 to Self.ComponentCount - 1 do
if Self.Components[a_Index] is TCheckBox and (TCheckBox(Self.Components[a_Index]).Checked) then
Include(Result, TMyType(Self.Components[a_Index].Tag));
end;
function TForm54.ReadMyTypes(aVariableName: string): TMyTypes;
var
a_Ini: TIniFile;
a_Var: string;
a_List: TStrings;
a_Index: integer;
begin
Result := [];
a_Ini := nil;
a_List := nil;
a_Ini := TIniFile.Create('MyType.ini');
a_List := TStringList.Create;
try
a_Var := a_Ini.ReadString('Sets', aVariableName, '');
a_List.Delimiter := ',';
a_List.DelimitedText := a_Var;
for a_Index := 0 to a_List.Count - 1 do
begin
Include(Result, TMyType(GetEnumValue(TypeInfo(TMyType), a_List[a_Index])));
end;
finally
a_Ini.Free;
a_List.Free;
end;
end;
procedure TForm54.SaveMyTypes(aVariableName: string; aMyTypes: TMyTypes);
var
a_Ini: TIniFile;
a_Index: integer;
a_Var: string;
begin
a_Var := '';
a_Ini := TIniFile.Create('MyType.ini');
try
for a_Index := Ord(Low(TMyType)) to Ord(High(TMyType)) do
if TMyType(a_Index) in aMyTypes then
if a_Var = '' then
a_Var := GetEnumName(TypeInfo(TMyType), a_Index)
else
a_Var := a_Var + ',' + GetEnumName(TypeInfo(TMyType), a_Index);
a_Ini.WriteString('Sets', aVariablename, a_Var);
finally
a_Ini.Free;
end;
end;
procedure TForm54.setMyTypes1(const Value: TMyTypes);
var
a_Index: integer;
begin
for a_Index := 0 to Self.ComponentCount - 1 do
if Self.Components[a_Index] is TCheckBox then
TCheckBox(Self.Components[a_Index]).Checked := TMyType(TCheckBox(Self.Components[a_Index]).Tag) in Value;
end;
end.