Delphi AdvancedTCharacterControl-TCharacter类中一些高级方法示例代码

2020-04-05

本站xe10.2.3亲测。

描述

下面的示例演示在Character单元中使用一些高级方法。编辑框被设置为只允许2个unicode字符。

代码

unit Unit1; interface uses    System.Character, TypInfo,    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,    Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls; type    TForm1 = class(TForm)     edtChar: TEdit;     procedure edtCharChange(Sender: TObject);     procedure FormCreate(Sender: TObject);    private     { Private declarations }    public     { Public declarations }    end; var    Form1: TForm1; implementation {$R *.dfm} procedure TForm1.edtCharChange(Sender: TObject); var    U4: UCS4Char;    Cat: TUnicodeCategory;    Value: String; begin    Value := edtChar.Text;    { 为空时退出 }    if Value = '' then     Exit;    { 注意,两个字符可能代表一个!! }    { 只允许控制代码和字母数字值 }    if (not IsLetterOrDigit(Value[1])) and      (not Value[1].IsControl and      (not IsSurrogate(Value[1])))      then Exit;    { 将输入的char转换为UCS4 }    U4 := ConvertToUtf32(Value, 1);    { 然后再转换回来 }    if (ConvertFromUtf32(U4) <> Value) then     MessageDlg('不允许!', mtError, [mbOK], 0);    MessageDlg('字符的数值为 ' +     FloatToStr(GetNumericValue(Value[1])), mtInformation, [mbOK], 0);    { 获取字符的unicode类别 }    Cat := GetUnicodeCategory(Value[1]);    MessageDlg(Format('字符"%s"类别是: %s ',     [Value[1], GetEnumName(TypeInfo(TUnicodeCategory), Ord(Cat))]),     mtInformation, [mbOK], 0); end; procedure TForm1.FormCreate(Sender: TObject); begin  { 允许输入两个字符的编辑框。对于Unicode,    2个字符代表一个字符。    }    edtChar.MaxLength := 2; end; end.






阅读62