问题描述
我认为这一定是一个常见问题解答,但是谷歌搜索并没有真正的帮助.
I think this must be a FAQ, but googling hasn't really helped.
在 FormCreate()
中我可以做什么-可以不做什么?
What may I do - and may do not - in FormCreate()
?
我想知道表单的所有子控件是否都已完全创建并且可以访问等.
I am wondering if all of the form's child controls are fully created and available for access, etc.
我问的原因是我偶然发现了一个旧项目,其中我的 FormCreate()
仅由
The reason I ask is that I stumbled over an old project where my FormCreate()
simply consists of
Sleep(1000);
PostMessage(Handle, UM_PROGRAM_START, 0, 0);
似乎我想稍等片刻",然后在事情解决后"进行一些初始化...
It seems that I want to "wait a bit" and then do some initialization "when things have settled down" ...
我当时确实有一个理由(?),但是由于缺乏启发性的评论,我无法回忆起为什么我觉得有必要这样做.
Surely I had a reason for it at the time(?), but, in the absence of an enlightening comment I am unable to recall why I felt that to be necessary.
任何人都可以声明或引用链接说明在 FormCreate()
中可以做什么的任何限制吗?
Can anyone state, or reference a link which states, any restrictions on what one may do in FormCreate()
?
[更新]我认为DavidHefferman写道:应用程序开始泵送消息.当您在.dpr文件中调用Application.Run时,就会发生这种情况."
[Update] I think thta DavidHefferman found the solution when he wrote "the application starts pumping messages. That happens when you call Application.Run in your .dpr file".
我想我并不担心单一表格.例如,我的主窗体想在启动时对config/options窗体做一些处理,因此显然必须等到它创建之后.
I guess that I wasn't concerned about a single form. For instance, my main form wants to do somethign with my config/options form at start up, so obviously would have to wait until it is created.
这是我的一个项目中的典型.DPR ...
Here's a typical .DPR from one of my projects ...
Application.Initialize;
Application.CreateForm(TGlobal, Global);
Application.MainFormOnTaskbar := True;
Application.CreateForm(TMainForm, MainForm);
Application.CreateForm(TLoginForm, LoginForm);
Application.CreateForm(TConfigurationForm, ConfigurationForm);
//[snip] a bunch of other forms ...
Application.Run();
因此,对于我的应用程序的 mainForm.CreateForm()
来说,向其自身发送 UM_APPLICATION_START
是有意义的,直到创建完所有表单后,它才会进行处理.初始化(或者,我可以只调用调用 Application.Run()
之后消息从我的.DPR触发的fn();但是我更喜欢该消息,因为它更明显-我很少看在我的.DPR文件中.
So, it makes sense for my app's mainForm.CreateForm()
to send a UM_APPLICATION_START
to itself which it won't process until all forms are created & initialized (or, I could just call the fn() which the message triggers from my .DPR after Application.Run()
is called; but I prefer the message as it is more obvious - I rarely look at my .DPR files).
推荐答案
没有确定的文档提供您可以在表单的OnCreate中执行和不执行的所有操作的列表.
There's no definitive documentation giving the list of all the things you can do and connot do in a form's OnCreate.
关于.dfm文件是否已处理以及是否创建了表单的所有自有组件,是的.
As for whether or not the .dfm file has been processed and all the form's owned components created, yes they have.
我不会在您找到的代码中放置太多存储空间.在启动过程中调用Sleep来让主线程等待,绝对不是一个好习惯.如果代码想要等待另一个线程,则可能会阻塞该线程,或者等待从该线程获取消息.这看起来就像是由一个不了解他/她在做什么的人输入的代码.而且代码从未删除.
I wouldn't place much store in the code you have found. Calling Sleep during start up, to make the main thread wait, is absolutely not good practice. If the code wanted to wait for another thread it could block for that thread, or wait to get a message from that thread. This just looks like code that got put in by someone who didn't understand what he/she was doing. And the code never got removed.
另一行代码是合理的:
PostMessage(Handle, UM_PROGRAM_START, 0, 0);
由于此消息已发布,因此直到应用程序开始泵送消息之前,该消息都不会得到处理.当您在.dpr文件中调用Application.Run时,就会发生这种情况.这意味着与您创建主表单有关的所有事情都在该消息被移出队列之前发生.
Because this message is posted, it won't get processed until the application starts pumping messages. That happens when you call Application.Run in your .dpr file. Which means that everything related to the creation of you main form happens before that message is pulled off the queue.
这篇关于我可以在FormCreate()中做什么以及不做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!