在 Delphi 中,如何在创建应用程序的主窗体之前分配组件的属性值?

发布于 2024-12-04 20:54:33 字数 363 浏览 1 评论 0原文

TMS 的 FormSize 组件将 aForm 的大小和位置保存在 .ini 文件中。该文件的路径存储在组件的 SaveName 属性中。我想将 FormSize.SaveName 分配给用户的 AppData 文件夹中的文件。我可以在源代码中找到 AppData 路径。

有谁知道我在哪里(在我的代码中)将 AppData 路径分配给 FormSize.SaveName?我认为在创建 Form 之前,已创建 FormSize 组件并初始化默认的 SaveName。换句话说,FormSize 在 aForm 的 FormCreate 事件触发之前加载配置文件;在 aForm.FormCreate 期间为 FormSize.SaveName 赋值为时已晚。

谢谢,一如既往。

TMS's FormSize component saves aForm's size and position in an .ini file. This file's path is stored in the component's SaveName property. I would like to assign FormSize.SaveName to a file in the user's AppData folder. I can find the AppData path in my source code.

Does anyone know where (in my code) I assign the AppData path to FormSize.SaveName? I am thinking the FormSize component is created, and a default SaveName initialized, BEFORE aForm is created. In other words, FormSize loads the config file BEFORE aForm's FormCreate event fires; assigning a value to FormSize.SaveName during aForm.FormCreate is too late.

Thanks, as always.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

撕心裂肺的伤痛 2024-12-11 20:54:33

表单的调整是在TFormSizeLoaded方法中完成的,而不是在更改SaveName属性时完成的(尽管它是从之前的 DFM)。

如果在设计时将属性 SavePositionSaveSize 设置为 false,则在运行时不会加载任何内容。在这种情况下,您可以通过调用 LoadFormSettingsSaveFormSettings 在代码中的方便位置手动加载和保存设置。

The adjustment of the form is done in the Loaded method of TFormSize, not when you change the SaveName property (although it has been read from the DFM before).

If you set the properties SavePosition and SaveSize to false during designtime, there will nothing be loaded at runtime. In that case you can manually load and save the settings at a convenient place in your code by calling LoadFormSettings and SaveFormSettings.

凉城凉梦凉人心 2024-12-11 20:54:33

我希望 SaveName 存储在 .dfm 文件中,因此应该在加载时将其分配给组件。

如果您想确定代码中的保存名称,它可能应该在循环的早期。我只是检查了几种可能性:

  1. 在表单的构造函数(覆盖)中,在调用继承之前;
  2. 在表单的构造函数中(覆盖),在调用继承之后;
  3. 在表单的 FormCreate 事件处理程序中;
  4. 在窗体的 Loaded 过程(覆盖)中,在调用继承之前以及
  5. 在窗体的 Loaded 过程(覆盖)中,在调用继承之后。

可能性 4 和 5 符合预期。 3 和 2 没有任何反应,1 引发了 AV。所以大卫的建议似乎不错。

I'd expect SaveName to be stored in the .dfm file, so it should be assigned to the component at load up.

If you want to determine the save name in code, it should probably be early in the cycle. I just checked a few possibilities:

  1. In the form's constructor (override), before the call to inherited;
  2. in the form's constructor (override), after the call to inherited;
  3. in the form's FormCreate event handler;
  4. in the form's Loaded procedure (override), before the call to inherited and
  5. in the form's Loaded procedure (override), after the call to inherited.

Possibilities 4 and 5 worked as expected. 3 and 2 did nothing and 1 caused an AV. So David's suggestion seems to be fine.

场罚期间 2024-12-11 20:54:33

“在 aForm.FormCreate 期间为 FormSize.SaveName 赋值也太过分了
迟到了。”

我有类似的要求,需要修改模块拥有的组件属性。鉴于加载的属性已经生效,标准“Create”事件已经太晚了。

在 DFM 中的属性在调用期间分配(或缓存) 惯例,在对 Loaded 的受保护虚拟调用期间,可以覆盖 ReadState 和 Loaded 过程。

按照 确保TADOConnection 的 Connected 属性在开发过程中为 false,考虑到相关数据集的设计需求,

在签入后续构建/部署的代码之前必须将该属性设置为 false 是一件很痛苦的事情。因此,我重写了 Loaded 方法并将流属性值修改为 false。

interface

type
    TMyDataModule = class(TDataModule)
        MyAdoConnection: TADOConnection;
    protected
        procedure Loaded; override;
    end;

implementation

type
  TADOConnectionHack = class(TADOConnection) end;

procedure TMyDataModule.Loaded;
begin
  TADOConnectionHack(MyAdoConnection).StreamedConnected := False;
  inherited Loaded;
end;

"assigning a value to FormSize.SaveName during aForm.FormCreate is too
late."

I had a similar requirement to modify a component's property owned by a module. The standard "Create" event was too late given the loaded property was already in effect.

Properties persisted in the DFM are assigned (or cached) during the call to the protected virtual ReadState procedure. Conventionally, cached properties are activated during the protected virtual call to Loaded. Both ReadState and Loaded can be overridden.

In my case, I wanted to make sure TADOConnection's Connected property was false in the release build. During development, the component's property is normally true given design needs of dependent data sets.

It was a pain having to set the property to false prior to checking-in the code for subsequent builds/deployment. So instead, I overrode the Loaded method and hacked the streamed property value to false.

interface

type
    TMyDataModule = class(TDataModule)
        MyAdoConnection: TADOConnection;
    protected
        procedure Loaded; override;
    end;

implementation

type
  TADOConnectionHack = class(TADOConnection) end;

procedure TMyDataModule.Loaded;
begin
  TADOConnectionHack(MyAdoConnection).StreamedConnected := False;
  inherited Loaded;
end;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文