【问题标题】:Using constant across .dfm files in embarcadero / RAD studio在 embarcadero / RAD 工作室中跨 .dfm 文件使用常量
【发布时间】:2020-01-23 08:36:23
【问题描述】:

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

比如这个main from就是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】:

    自定义字符串标识符可以在 DFM 中用于整数/枚举属性。为此,您需要在&lt;System.Classes.hpp&gt; 中调用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() 通常只用于更独特的数据类型,例如 TColorTFontCharset 等。

    您无法更改 ClientHeight/ClientWidth 属性本身以使用不同的数据类型,因此您可以将标识符映射到独特的东西。但是,您可以定义自己的属性,这些属性使用您自己的数据类型,然后您可以映射。或者,您可以尝试让您的表单override the DefineProperties() method 为 DFM 流式传输创建“假”属性。无论哪种方式,您都可以选择在 Form 类中重新声明 ClientHeight/ClientWidth 属性以包含 stored=false 属性,因此它们根本不会在 DFM 中流式传输。让您的自定义属性在内部读取/设置ClientHeight/ClientWidth 属性。

    【讨论】:

      猜你喜欢
      • 2015-01-16
      • 2016-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-01
      相关资源
      最近更新 更多