OnShow事件c ++构建器上的ProcessMessages [英] ProcessMessages on OnShow Event c++ builder
问题描述
我正在使用c ++ builder(bcb6)和on:
I'm using c++ builder (bcb6) and on:
FormShow
事件是:
Application->ProcessMessages
我想知道究竟是什么责任:
I would like to know what exactly the responsibility of:
Application->ProcessMessages
它究竟是什么?和当我们将使用的?
What exactly it did? and when we shall use by that? when it can cause exp.?
推荐答案
- $
BDS 2006 IDE帮助 Application-> ProcessMessages
表示:
The BDS 2006 IDE help states for
Application->ProcessMessages
this:
中断应用程序的执行,以便它可以处理消息队列。
Interrupts the execution of an application so that it can process the message queue.
调用 ProcessMessages
允许应用程序处理当前在消息队列中的消息。 ProcessMessages
循环Windows消息循环,直到它为空,然后将控制权返回给应用程序。
Call ProcessMessages
to permit the application to process messages that are currently in the message queue. ProcessMessages
cycles the Windows message loop until it is empty, and then returns control to the application.
忽略消息处理仅影响调用 ProcessMessages
的应用程序,而不影响其他应用程序。在长时间的操作中,调用 ProcessMessages
定期允许应用程序响应绘图和其他消息。
Neglecting message processing affects only the application calling ProcessMessages
, not other applications. In lengthy operations, calling ProcessMessages
periodically allows the application to respond to paint and other messages.
ProcessMessages
不允许应用程序闲置,而 HandleMessage
则不允许。
ProcessMessages
does not allow the application to go idle, whereas HandleMessage
does.
所以是什么?
它可以响应 Windows邮件正在阻止正常的 WindProc 操作(位于 VCL 中)。例如,如果你在一些需要几分钟的事件上得到一些冗长的计算应用程序将冻结(不能点击,移动,调整大小,重绘,直到操作完成)。如果你曾经在一个时间调用 ProcessMessages
从那个长的循环(计时器也不会工作在这段时间),这将允许在这段时间内你的应用程序响应...所以它不会冻结。
It allows to respond to Windows messages in case your app is blocking normal WindProc operation (inside VCL). For example if you got some lengthy computation on some event that takes minutes the application would freeze (can not click,move,resize,redraw,... until operation is done). If you once in a time call ProcessMessages
from that long loop (timers would also not work during that time) that will allow to make your app responsive during this time... so it will not freeze.
我通常使用主题或OnIdle活动
我不太相信 OnShow
在这种阻塞期间被调用。我将把 ProcessMessages
放在阻塞App的计算中(如果计算在 OnShow
strong> OK ,否则它将是无用的。无论如何 OnShow
只有当你的窗体转向 Visible
不要误认为 OnActivate
或 OnPaint
。
I am reluctant to believe that OnShow
is called during such blocking. I would place the ProcessMessages
inside the computation that blocks the App (if the computations is inside the OnShow
then it is OK otherwise it would be useless. Anyway OnShow
is called only if your Form is turning to Visible
do not mistake it for OnActivate
or OnPaint
.
小示例
创建空白表单应用并在其中放置2个按钮( btStart,btStop
)然后为他们创建点击事件如下:
Create empty form app and place 2 buttons in it (btStart,btStop
) then create on click event for them as following:
//---------------------------------------------------------------------------
bool go=false;
//---------------------------------------------------------------------------
void __fastcall TForm1::btStartClick(TObject *Sender)
{
int i=0;
for (go=true;go;)
{
Caption=i; i++;
Application->ProcessMessages();
Sleep(100);
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btStopClick(TObject *Sender)
{
go=false;
}
//---------------------------------------------------------------------------
当您启动应用程序并点击 btStart
,它将开始递增整数 Caption
字段,并在单击
btStop
时停止。在计数期间,应用程序仍然响应(可以单击,移动,调整大小,...)。你需要在关闭应用程序之前停止应用程序是可能的(析构函数等待从所有事件返回)。如果你注销 Application-> ProcessMessages();
,那么应用程序将会计数,但永远不会停止,因为你不能点击 btStop
由于冻结。要关闭,请点击 IDE ,然后按 CTRL + F2 。
When you start app and click btStart
it will start incrementing integer in Caption
field of the Form1
and stop when you click btStop
. during counting the App is still responsive (can click,move,resize,...). You need to stop before closing App is possible (destructors wait for returning from all events). if you rem out the Application->ProcessMessages();
then the App will count but will never stop because you can not click on btStop
due to the freeze. To close click on the IDE and press CTRL+F2.
希望它清除一些东西。
这篇关于OnShow事件c ++构建器上的ProcessMessages的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!