自定义字符串标识符可以在 DFM 中用于整数/枚举属性。为此,您需要在<System.Classes.hpp> 中调用RegisterIntegerConsts() 来注册您自己的自定义函数,这些函数可以在字符串标识符及其序数值之间进行转换。在您的情况下,将 "AllFormHeight" 和 "AllFormWidth" 字符串转换为特定的整数值,反之亦然。
例如,这正是您展示的 DFM 示例允许 clBtnFace 标识符用于 Color 属性的方式。
试试这个:
#include <System.Classes.hpp>
#include <System.TypInfo.hpp>
#include <sysopen.h>
const int AllFormHeight = 300;
const int AllFormWidth = 400;
const TIdentMapEntry MyFormIdents[] = {
{AllFormHeight, "AllFormHeight"},
{AllFormWidth, "AllFormWidth"}
};
bool __fastcall MyFormIdentToInt(const String Ident, int &Int)
{
return IdentToInt(Ident, Int, EXISTINGARRAY(MyFormIdents));
}
bool __fastcall MyIntToFormIdent(int Int, String &Ident)
{
return IntToIdent(Int, Ident, EXISTINGARRAY(MyFormIdents));
}
// See http://bcbjournal.org/articles/vol3/9908/Registering_AnsiString_property_editors.htm
// for why this function is needed...
TTypeInfo* IntTypeInfo()
{
TTypeInfo* typeInfo = new TTypeInfo;
typeInfo->Name = "int";
typeInfo->Kind = tkInteger;
return typeInfo;
/* alternatively:
TPropInfo* PropInfo = GetPropInfo(__typeinfo(TForm), "ClientHeight");
return *PropInfo->PropType;
*/
}
RegisterIntegerConsts(IntTypeInfo(), &MyFormIdentToInt, &MyIntToFormIdent);
但是,这种方法的缺点是因为ClientHeight/ClientWidth 属性使用int 作为它们的数据类型,您的自定义标识符随后将应用于任何可流式传输类中的任何int 属性。 RegisterIntegerConsts() 通常只用于更独特的数据类型,例如 TColor、TFontCharset 等。
您无法更改 ClientHeight/ClientWidth 属性本身以使用不同的数据类型,因此您可以将标识符映射到独特的东西。但是,您可以定义自己的属性,这些属性使用您自己的数据类型,然后您可以映射。或者,您可以尝试让您的表单override the DefineProperties() method 为 DFM 流式传输创建“假”属性。无论哪种方式,您都可以选择在 Form 类中重新声明 ClientHeight/ClientWidth 属性以包含 stored=false 属性,因此它们根本不会在 DFM 中流式传输。让您的自定义属性在内部读取/设置ClientHeight/ClientWidth 属性。