How does delphi convert ModalResult properties?

Hope this is quick, and "Easy if you know how" ...

I am writing some Serialization / Scripting class to generate forms on the fly, I tried installing TColor the other day and got an error   clBtnFace is not a valid integer valueor something like that, and found that the constants used in the properties are registered so that they can be convert to an integer, and so I added code to extract the converter and use it.

Now I have a similar problem with the ModalResult property, but I can’t understand how the DFM deserializer handles this property? Any ideas how it converts mrOKto an integer?

Edit

Not many examples:

PropInfo := GetPropInfo(Instance, PropertyName);
SetPropValue(Instance, PropInfo, PropertyValue);

Where in this case the instance is TButton, PropertyName is "ModalResult", and PropertyValue is "mrOK"

+3
source share
3 answers

He does not need:

const
  { Dialog Box Command IDs }
  {$EXTERNALSYM IDOK}
  IDOK = 1;          ID_OK = IDOK;

const
  mrNone     = 0;
  mrOk       = idOk;

type
  TModalResult = Low(Integer)..High(Integer);

TModalResult is to some extent an Integer subband, and mrOK is just an Integer constant.

+6
source

In fact, I do not want to answer my question, but since no one else has.

There is no converter for ModalResults, Delphi saves the Integer view in DFM, as VilleK says in a comment on the question. As a solution, I registered a new converter

const
  ModalResults: array[0..10] of TIdentMapEntry = (
    (Value: mrNone; Name: 'mrNone'),           
    (Value: mrOk; Name: 'mrOk'),               
    (Value: mrCancel; Name: 'mrCancel'),       
    (Value: mrAbort; Name: 'mrAbort'),         
    (Value: mrRetry; Name: 'mrRetry'),         
    (Value: mrIgnore; Name: 'mrIgnore'),       
    (Value: mrYes; Name: 'mrYes'),             
    (Value: mrNo; Name: 'mrNo'),               
    (Value: mrAll; Name: 'mrAll'),             
    (Value: mrNoToAll; Name: 'mrNoToAll'),     
    (Value: mrYesToAll; Name: 'mrYesToAll'));



function ModalResultToIdent(ModalResult: Longint; var Ident: string): Boolean;
begin
    Result := IntToIdent(ModalResult, Ident, ModalResults);
end;

function IdentToModalResult(const Ident: string; var ModalResult: Longint): Boolean;
begin
    Result := IdentToInt(Ident, ModalResult, ModalResults);
end;
initialization
    RegisterIntegerConsts(TypeInfo(TModalResult), IdentToModalResult, ModalResultToIdent);
+4
source

- . RTTI Integer. TCursor, TFontCharset TTabOrder.

:

  TEnum = (exOne,exTwo,exThree);

RTTI 'exOne', 'exTwo' 'exThree' .

TypInfo.pas

function GetEnumName(TypeInfo: PTypeInfo; Value: Integer): string;
function GetEnumValue(TypeInfo: PTypeInfo; const Name: string): Integer;

, ModalResults, , .

TColor Colors, , 52 , .

  Colors: array[0..51] of TIdentMapEntry = (
    (Value: clBlack; Name: 'clBlack'),
    ...
    (Value: clWindowText; Name: 'clWindowText'));

, .

var
  ColorName : String;
begin
  // Color Value must be between 0 and 51 otherwise index out of bounds
  ColorName := Colors[ColorValue];  
end;

, .

+1

Source: https://habr.com/ru/post/1757653/


All Articles