在embarcadero / RAD studio中在.dfm文件中使用常量

问题描述 投票:2回答:1

我有一个Windows Vcl应用程序,里面有几种形式。我想标准化所有这些表格的布局。因此,我想声明一些常量,这些常量可以应用于所有.dfm布局文件。

例如,此主文件来自IDE中自动生成的表单:

object frm_MainForm: Tfrm_MainForm
  Left = 0
  Top = 0
  Caption = 'Main Form'
  ClientHeight = 300
  ClientWidth = 400    
  Color = clBtnFace
end

我想做的是声明类似的内容:

AllFormHeight = 300
AllFormWidth = 400

这样,我就可以适用于以下所有形式:

object frm_MainForm: Tfrm_MainForm
  Left = 0
  Top = 0
  Caption = 'Main Form'
  ClientHeight = AllFormHeight <--- Like this
  ClientWidth = AllFormWidth <--- And this
  Color = clBtnFace
end

我尝试做几乎与Vcl.Graphics.hpp中的颜色常量相似的操作,但是它不起作用。我正在使用Embarcadero RAD Studio C ++ Builder 10.3。我使用C ++进行编程,并使用dfm文件作为UI文件。

c++ c++builder vcl dfm
1个回答
3
投票

自定义字符串标识符CAN在DFM中用于整数/枚举属性。为此,您需要在RegisterIntegerConsts()中调用RegisterIntegerConsts()来注册自己的自定义函数,以在字符串标识符及其序数值之间进行转换。根据您的情况,将<System.Classes.hpp>"AllFormHeight"字符串转换为特定的整数值,反之亦然。

例如,这正是您所显示的DFM示例如何将"AllFormWidth"标识符用于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作为其数据类型,因此您的自定义标识符将应用于ANY streamable类中的ANY int属性。 int通常仅用于更独特的数据类型,例如RegisterIntegerConsts()TColor等。

您无法自行更改TFontCharset / ClientHeight属性以使用其他数据类型,因此您需要将标识符映射到的唯一值。但是,您可以定义自己的属性,这些属性使用可以映射的自己的数据类型。或者,您可以尝试使用表格ClientWidth创建仅用于DFM流的“假”属性。无论哪种方式,您都可以选择在Form类中重新声明override the DefineProperties() method / DefineProperties()属性,以包括ClientHeight属性,这样它们就不会在DFM中流式传输。让您的自定义属性在内部读取/设置ClientWidth / stored=false属性。

© www.soinside.com 2019 - 2024. All rights reserved.