修改 Delphi DFM 资源以在显示时关闭?

发布于 2024-08-15 10:32:41 字数 547 浏览 10 评论 0原文

是否可以编辑 DFM(Delphi 的表单脚本格式),使表单在显示时自行关闭?

我不在 Delphi 中编写代码,所以我不熟悉这些表单的工作原理,但似乎我可以将代码(但不是看起来的标准 Delphi 代码)放在 OnShow表单的 >OnCreate 事件。但是,在尝试了诸如 CloseExitFormNameExitDestroy 等多种语句后,将不起作用(将创建一个日志,指出 OnShow 属性的值无效等错误。)

关闭表单的正常方法是通过按钮,但该按钮没有 OnClick事件,只是一个属性,“ModalResult = 1”。

有没有办法让窗口在打开时关闭,我可以在窗体的 OnCreate 或 OnShow 事件上放置一些标准函数?或者,也许在表单本身上创建一个复选框,使 ModalResult = 1? (不知道这是否有效)

感谢您的任何建议!
=)

(注意:也许这是显而易见的,但我没有来源。)

Is it possible to edit a DFM (Delphi's form script format) in such a way that a form closes itself when shown?

I don't code in Delphi, so I'm not familiar with how these forms work, but it seems I could put code (but not standard Delphi code as it seems) in the OnShow or OnCreate events of the form. However, after trying several statements like Close, Exit, FormNameExit, Destroy, etc. won't work (a log will be created, stating the error that the value of the OnShow property was invalid, etc.)

The normal way of closing the form is through a button, but the button doesn't have a OnClick event, just a property, "ModalResult = 1".

Is there a way to make the window close upon opening, some standard function I could put on the OnCreate or OnShow events of the form? Or maybe, creating a checkbox on the form itself, that gives ModalResult = 1? (don't know if this works)

Thanks for any suggestion!
=)

(Note: maybe it's obvious, but I don't have the source.)

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

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

发布评论

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

评论(4

毁虫ゝ 2024-08-22 10:32:41

不在 DFM 中。您必须修改源。

Not in DFM. You would have to modify the source.

屌丝范 2024-08-22 10:32:41

您看到的 OnShowOnCreate 行仅用于给出源代码中已定义的方法的名称。您根本无法通过修改 DFM 文件来添加太多功能。

也许表单已经有一个匹配的事件处理程序来关闭它:也许是关闭按钮或菜单项的 OnClick 处理程序?如果是这样,您可以尝试将其设置为 OnShow 或 OnCreate 处理程序。

您也许可以向表单添加一个 TButton 并设置其 ModalResult ——我不记得您是否真的需要表单类中为 DFM 中的每个控件提供一个字段 ——但这只有在显示表单时才有效模态地,您仍然需要单击它才能关闭表单。

The OnShow and OnCreate lines you're seeing are only used to give the name of a method that's already defined in the source code. You can't add much functionality at all by modifying the DFM file.

Perhaps the form already has a matching event handler that closes it: the OnClick handler for a close button or menu item, maybe? If so, you could try setting it as the OnShow or OnCreate handler.

You might be able to add a TButton to the form and set its ModalResult -- I don't recall whether you actually need a field in the form class for each control in the DFM -- but that would only work if the form is shown modally, and you'd still have to click it to make the form close.

神妖 2024-08-22 10:32:41

编辑:在我输入文本墙时看到您添加的一些评论使事情变得更加清晰。

我猜您正在使用资源编辑器来编辑 DFM 并修改应用程序的行为,而无需实际接触源代码?

在这种情况下,您可以尝试的最好方法是将 Visible 属性设置为 False。但是,如果开发人员“主动在代码中显示表单”,这将没有任何好处。 (他可以通过调用 ShowShowModal 甚至显式设置 Visible 属性来完成此操作。)

不幸的是,如果是这种情况,那么如果不修改实际的源代码,您将无能为力。这是因为DFM是在加载表单时处理的;即在显示表单的开发人员代码之前。即使寻找设置 ModalResult 的地方也是无用的,因为调用 ShowModal 时当前的 ModalResult 会被清除。

我不认为我完全理解你想要做什么,因为它没有意义。
在我看来,您希望表单在显示后立即自动关闭;这没有道理。 :S
因此,如果我的理解正确,请解释一下您为什么要这样做;对于您的实际问题可能有更好的解决方案。

但是,一些一般概念...

  • 如果您希望关闭表单,则应该将其链接到某些关闭表单的操作。要么在表单上放置一个按钮,要么在菜单项上放置一个按钮。
  • 标准表单有一个标准的 Windows 机制来默认关闭它们。 (即右上角的 X。)
  • 显示表单的方式有两种,显示方式确实会影响表单的关闭方式。它可以模态显示(这意味着它是与用户交互的应用程序的唯一形式),也可以正常显示(这允许用户在应用程序的其他形式之间切换)。
    • 以模态方式显示表单的要点在于,它会阻止代码流,直到用户完成所需的操作为止;它通常涉及用户提供某种形式的答案或确认。
    • 当以模态方式显示时,表单应该用 ModalResult 关闭。
    • 正常显示时,ModalResult 没有任何效果。
  • 每当表单“关闭”时,可以通过几种方法来完成此操作。
    • 表单可以简单地隐藏;它仍然在那里,但看不见。下次您想要显示该表单时,只需使其再次可见即可。
    • 表格可以被销毁;意味着它不再存在于内存中。如果这样做了,那么下次您想使用该表单时,您必须重新创建它。
    • 可以主动阻止尝试关闭表单(通常不建议;但在特定情况下可能是必要的 - 例如,如果表单上的某些信息不正确)。
    • 可以简单地最小化表单(这通常通过 MDI 子表单完成)。
  • 注意:表单上还有许多属性(FormStyle 是最重要的),它们会影响表单的行为、显示和关闭方式。 (例如,MDI 子窗体默认情况下会最小化,或者在关闭时不执行任何操作。)
  • 注意:如果应用程序的主窗体正确关闭,则应用程序将关闭向下。

现在,一些技术细节...

  • 如前所述,表单可以以模式方式显示,也可以正常显示;使用 MyForm.Show;ModalResult := MyForm.ShowModal;
    • 注意:如果表单以模态方式显示,则您需要检查 ModalResult 以找出用户的答案并采取相应的操作。
  • 如果您以模态方式显示表单,则应设置ModalResult,表单将自行关闭。执行此操作的一个简单方法是将 ModalResult 分配给表单上的按钮;那么该按钮将在单击时自动设置表单的 ModalResult
  • 如果您正常显示表单,那么您所需要做的就是在适当的时间点调用MyForm.CloseNB:请注意,它们还有“关闭”表单的其他方法,但最好使用此方法,因为它允许您处理 OnCloseQuery 事件,这是一种机制确认表单是否允许关闭。
  • 注意:关闭表单时,Delphi 可以调用两个事件,您可以处理这两个事件,以便修改表单的关闭行为:
    • 调用OnCloseQuery来确认是否允许关闭表单。
    • OnClose 被调用以查明表单应如何关闭(如前所述)。

回到你的问题(听起来你希望表单自动关闭)。而不是自动关闭表单;只是不费心去展示它。这很容易做到。所有表单都有一个 Visible 属性;如果设置为True,Delphi 将在创建表单时自动正常显示表单。因此,您所需要做的就是确保该属性为 False

EDIT: Seeing some of your comments added while I typed my text-wall clarifies things a bit.

I'm guessing that you you're using a resource editor to edit the DFM and modify the behaviour of the app without actually touching the source code?

In this case, the best you could try is to set the Visible property to False. However, this will have no benefit if the developer 'actively displays the form in code'. (He could have done this by calling Show, ShowModal or even by explicitly setting the Visible property.)

Unfortunately, if this is the case, then there's nothing you can do without modifying the actual source code. This is because the DFM is processed when the form is loaded; i.e. before the developer's code that shows the form. Even looking for a place to set ModalResult is useless, because the current ModalResult is cleared when ShowModal is called.

I don't think I understand exactly what you're trying to do, because it doesn't make sense.
It seems to me that you want the form to be automatically closed as soon as it is shown; and that doesn't make sense. :S
So, if I have understood you correctly, please explain why you would want to do this; there may be a better solution for your actual problem.

However, some general concepts...

  • If you want a form to close, you should link it to some action that closes the form. Either put a button on the form, or a menu item.
  • Standard forms have a standard Windows mechanism to close them by default. (I.e. the X on the top-right.)
  • There are two ways of showing forms, and the way in which it is shown does have an effect on how it will be closed. It can be shown modally (which means it is the only form of the application which will interact with the user), or it can be shown normally (which allows the user to switch between other forms of the application).
    • The point of showing a form modally is that it blocks the flow of your code until the user has finished doing something that was required; it often involves the user providing some form of answer or confirmation.
    • When shown modally, the form should rather be closed with a ModalResult.
    • When shown normally, the ModalResult has no effect.
  • Whenever a form is 'closed', there are a few ways in which this can be done.
    • The form can simply be hidden; it's still there, but invisible. Next time you want to show the form, you just make it visible again.
    • The form can be destroyed; meaning that it no longer exists in memory. If this is done, then next time you want to use the form, you have to recreate it.
    • The attempt to close the form can be actively prevented (Not usually advisable; but may be necessary in specific cases - if for example some information on the form is incorrect).
    • The form may be simply minimised (this is often done with MDI child forms).
  • NOTE: There are also a number of attributes on forms (FormStyle being the most important) that have an effect on how it behaves, displays, and can be closed. (E.g. MDI Child forms will by default either minimise, or do nothing when closed.)
  • NB:If the main form of an application is properly closed, then the application will shut down.

Now, some of the technicalities...

  • As mentioned earlier a form can be displayed either modally, or normally; using either MyForm.Show; or ModalResult := MyForm.ShowModal;
    • NOTE: If the form was shown modally, you then need to check the ModalResult to find out the user's answer and act accordingly.
  • If you displayed the form modally, you should set the ModalResult and the form will close itself. An easy way to do this is to assign a ModalResult to the buttons on the form; then the button will automatically set the form's ModalResult when clicked.
  • If you displayed the form normally, then all you need to do is call MyForm.Close at the appropriate point in time. NB: Note their are other ways to 'close' a form, but it is better to use this method because it allows you to handle the OnCloseQuery event which is a mechanism to confirm whether the form is allowed to close.
  • NOTE: While closing the form, there are two events that Delphi can call which you can handle in order to modify how the closing of the form behaves:
    • OnCloseQuery is called to confirm whether the form is allowed to close.
    • OnClose is called to find out how the form should close (as explained previously).

Coming back to your question (which sounds like you want the form closed automatically). Rather than closing the form automatically; just don't bother showing it. This is very easy to do. All forms have a Visible property; if set to True, Delphi will automatically show the form normally when it is created. So all you need to do is ensure the property is False.

眼睛会笑 2024-08-22 10:32:41

如果没有源代码,您实际上无能为力,只能移动文件或更改现有属性。如果您有程序的 MAP 文件并且存在现有事件(onCreate/OnShow),您可以修补可执行文件以调用这些事件的不同代码,但这并不容易,您必须确保不这样做不要注入比以前更多的代码或对不存在的例程进行任何外部调用。

You really can't do much without the source but move files around or change existing properties. If you have a MAP file for the program and there are existing events in place (onCreate/OnShow) you could patch the executable to invoke different code for those events, but it won't be easy and you have to insure that you don't inject more code than was there previously or make any external calls to routines which don't exist.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文