修改 Delphi DFM 资源以在显示时关闭?
是否可以编辑 DFM(Delphi 的表单脚本格式),使表单在显示时自行关闭?
我不在 Delphi 中编写代码,所以我不熟悉这些表单的工作原理,但似乎我可以将代码(但不是看起来的标准 Delphi 代码)放在 OnShow 或 表单的 >OnCreate 事件。但是,在尝试了诸如 Close、Exit、FormNameExit、Destroy 等多种语句后,将不起作用(将创建一个日志,指出 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不在 DFM 中。您必须修改源。
Not in DFM. You would have to modify the source.
您看到的 OnShow 和 OnCreate 行仅用于给出源代码中已定义的方法的名称。您根本无法通过修改 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.
编辑:在我输入文本墙时看到您添加的一些评论使事情变得更加清晰。
我不认为我完全理解你想要做什么,因为它没有意义。
在我看来,您希望表单在显示后立即自动关闭;这没有道理。 :S
因此,如果我的理解正确,请解释一下您为什么要这样做;对于您的实际问题可能有更好的解决方案。
但是,一些一般概念...
ModalResult
关闭。ModalResult
没有任何效果。FormStyle
是最重要的),它们会影响表单的行为、显示和关闭方式。 (例如,MDI 子窗体默认情况下会最小化,或者在关闭时不执行任何操作。)现在,一些技术细节...
MyForm.Show;
或ModalResult := MyForm.ShowModal;
ModalResult
以找出用户的答案并采取相应的操作。ModalResult
,表单将自行关闭。执行此操作的一个简单方法是将ModalResult
分配给表单上的按钮;那么该按钮将在单击时自动设置表单的ModalResult
。MyForm.Close
。 NB:请注意,它们还有“关闭”表单的其他方法,但最好使用此方法,因为它允许您处理OnCloseQuery
事件,这是一种机制确认表单是否允许关闭。OnCloseQuery
来确认是否允许关闭表单。OnClose
被调用以查明表单应如何关闭(如前所述)。回到你的问题(听起来你希望表单自动关闭)。而不是自动关闭表单;只是不费心去展示它。这很容易做到。所有表单都有一个
Visible
属性;如果设置为True
,Delphi 将在创建表单时自动正常显示表单。因此,您所需要做的就是确保该属性为False
。EDIT: Seeing some of your comments added while I typed my text-wall clarifies things a bit.
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...
ModalResult
.ModalResult
has no effect.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.)Now, some of the technicalities...
MyForm.Show;
orModalResult := MyForm.ShowModal;
ModalResult
to find out the user's answer and act accordingly.ModalResult
and the form will close itself. An easy way to do this is to assign aModalResult
to the buttons on the form; then the button will automatically set the form'sModalResult
when clicked.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 theOnCloseQuery
event which is a mechanism to confirm whether the form is allowed to close.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 toTrue
, Delphi will automatically show the form normally when it is created. So all you need to do is ensure the property isFalse
.如果没有源代码,您实际上无能为力,只能移动文件或更改现有属性。如果您有程序的 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.