栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

【Delphi】理解枚举类型(Enum),你可能忘记的有用函数

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【Delphi】理解枚举类型(Enum),你可能忘记的有用函数

关于枚举类型,日常编程中经常使用到,但是是否真正理解和操作到位,可能就不一定了。Delphi中的枚举类型随处可见,例如 FMX 控件的 Align 属性 TAlignLayout,图形控件中使用的TBrushKind 都是枚举类型。

 工程文件下载

{2022-02-25 sensor 关于 枚举类型操作示例。
特别注意以下函数:
        GetEnumName:获取枚举类型对应名称字符串。GetEnumName(TypeInfo(TMyColors), 0) 将返回字符串 ' Red '。

        GetEnumProp:获取对象枚举属性字符串。如果你直接在 Form 上放置一个 TButton,那么:GetEnumProp(Button1, 'Align') 将返回字符串' None '。初始的时候我们并没有设置Button1的 Align 属性,所以默认就是 None 。

        GetEnumValue:通过枚举变量实际字符串,获取枚举变量的实际索引值。GetEnumValue(TypeInfo(TMyColors), 'Red') 将返回 0。

        SetEnumProp:根据枚举字符串,设置对象的枚举属性。SetEnumProp(Button1, 'Align', 'alLeft') 设置 Button1 的 Align 属性未 alLeft 。
}
unit uMainForm;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.ListBox,
  FMX.Controls.Presentation, FMX.StdCtrls, FMX.Objects;

type
  TForm2 = class(TForm)
    ComboBox_Align: TComboBox;
    Label1: TLabel;
    Button_Get: TButton;
    Button_Set: TButton;
    Rectangle1: TRectangle;
    Image: TImage;
    procedure Button_GetClick(Sender: TObject);
    procedure Button_SetClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

uses
  System.TypInfo;

{$R *.fmx}

procedure TForm2.Button_GetClick(Sender: TObject);
var
  i : integer;
  AlignStr : string;
begin
  //1. 获取 TAlignLayout 的列表名称字符串,放置到 Combox
  for i := Integer(Low(TAlignLayout)) to Integer(High(TAlignLayout)) do
  begin
    AlignStr := GetEnumName(TypeInfo(TAlignLayout), i);
    ComboBox_Align.ListBox.Items.AddObject(AlignStr, TObject(i));
  end;

  //2. 获取 Image 的 Align 属性索引
  AlignStr := GetEnumProp(Image, 'Align');
  //3. 通过 AlignStr 获取对应的索引值
  i := GetEnumValue(TypeInfo(TAlignLayout), AlignStr);

  //4. 设置 ComboBox_Align 的当前值
  ComboBox_Align.ItemIndex := i;

  Button_Set.Enabled := True;
end;

procedure TForm2.Button_SetClick(Sender: TObject);
var
  i : integer;
  myAlign : TAlignLayout;
begin
  //1. 获取Combox 中的 Align 值
  myAlign := TAlignLayout(Integer(ComboBox_Align.ListBox.Items.Objects[ComboBox_Align.ItemIndex]));
  //2. 设置 Image Algin 属性
  Image.Align := TAlignLayout.None;   //下面这几句是因为有些属性会更改图像大小,所以先需要恢复大小
  Image.Size.Width  := 48;
  Image.Size.Height := 48;
  Image.Align := myAlign;
  Rectangle1.Repaint;
end;

end.

举例说明如下:

TAlignLayout = (None, Top, Left, Right, Bottom, MostTop, MostBottom, MostLeft, MostRight, Client, Contents, Center, VertCenter, HorzCenter, Horizontal, Vertical, Scale, Fit, FitLeft, FitRight);

TBrushKind = (None, Solid, Gradient, Bitmap, Resource);

在程序中我们也可以自定义我们自己的枚举类型:

uses
  System.TypInfo;

type
  //关于枚举类型使用
  TMyColors = (Red, Yellow, Green, Cyan, Blue, Violet);

枚举型别(通常被缩写成enums)可以构成另一个使用者自定有序型别。在枚举类型中,我们不指定特定类型的范围,而是直接列出其中几个可能的数值,换句话说,我们只把我们需要的特定数值列出即可,举例如下:
type
     TMyColors = (Red, Yellow, Green, Cyan, Blue, Violet);
     TSuit = (Club, Diamond, Heart, Spade);
列表当中的每个值都对应一个序号,从0开始。当我们通过 Ord 函式来查找枚举类型中的特定数值,函数就会返回该数值在枚举类型中的顺序,以0为第一个元素的编号,因此Ord(Diamond)会回传1。
在 Delphi中 枚举类型有不同的内部表示方法,预设情形下,Delphi使用8位来表示它,除非有超过256个数值被列在列举型别当中,才会使用16位来表示,当然也有32位的表示法,这方法会在需要跟C或C++的函式库兼容时使用。

枚举类型的操作:注意引用单元:System.TypInfo

1. 操作枚举类型的几个函数:

        GetEnumName:获取枚举类型对应名称字符串。GetEnumName(TypeInfo(TMyColors), 0) 将返回字符串 ' Red '。

        GetEnumProp:获取对象枚举属性字符串。如果你直接在 Form 上放置一个 TButton,那么:GetEnumProp(Button1, 'Align') 将返回字符串' None '。初始的时候我们并没有设置Button1的 Align 属性,所以默认就是 None 。

        GetEnumValue:通过枚举变量实际字符串,获取枚举变量的实际索引值。GetEnumValue(TypeInfo(TMyColors), 'Red') 将返回 0。

        SetEnumProp:根据枚举字符串,设置对象的枚举属性。SetEnumProp(Button1, 'Align', 'alLeft') 设置 Button1 的 Align 属性未 alLeft 。

2. 如何将枚举类型显示到 Combox 列表中?

        如何将 TMyColors 枚举列表显示到 Combox 中,并且可以选择使用,代码如下:

var
  i : integer;
  s : string;
begin
  for i := Integer(Low(TMyColors)) to Integer(High(TMyColors)) do
  begin
    s := GetEnumName(TypeInfo(TMyColors), i);
    ComboBox_Color.ListBox.Items.AddObject(s, TObject(i));
  end;
  ComboBox_Color.ItemIndex := 0;
end;

通过 Combox 选择,使用代码如下:

var
  i : integer;
  myColor : TMyColors;
begin
  myColor := TMyColors(Integer(ComboBox_Color.ListBox.Items.Objects[ComboBox_Color.ItemIndex]));
  ShowMessage(ord(myColor).ToString);
end;

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1023540.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号