ShowModal的实现是这样的:
function TCustomForm.ShowModal: Integer;
...
begin
...
repeat
Application.HandleMessage;
~~~~~~~~~~~~~
if Application.FTerminate then ModalResult := mrCancel else
if ModalResult <> 0 then CloseModal;
until ModalResult <> 0;
...
end;
在代码中,用到了函数HandleMessage,在Borland的文档中,明确指出:
TApplication.HandleMessage
Note:
If the application goes idle, HandleMessage may take a long time
~~~~~~~~~~~~~~~~~~~~
to return. Therefore, do not call HandleMessage when waiting for
~~~~~~~~~
something message-based while priority actions are also being
processed. Instead, call ProcessMessages when processing more
than just messages.
TApplication.ProcessMessages
Note:
ProcessMessages does not allow the application to go idle, whereas
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
HandleMessage does.
~~~~~~~~~~~~~~~~~~~
大家可以看到,这两个函数是有冲突的的。而我们的程序恰好是同时使用到了
这两个函数!我试过这样来做
repeat
//your code
while PeekMessage(Msg, 0, 0, 0, PM_REMOVE) do
begin
if (Msg.Message <> WM_QUIT) and (Msg.Message <> WM_CLOSE) and not FStop then
begin
TranslateMessage(Msg);
DispatchMessage(Msg);
end
else FStop := True;
end;
until FStop;
但是不行。估计是HandleMessage发出运行了Idle,Idle又调用了WaitMessage,
于是PeekMessage总取不出东西,结果就没什么好结果了。
我的建议是:不要使用ShowModal!要实在想用的话,自己写一个不允许切换的
窗口好了。