application.NormalizeAllTopMosts;
ModalForm.ShowModal;
Application.RestoreTopMosts;
使用Delphi本身提供的切换OnTop的属性,可能造成窗体闪烁,可以用API来代替:SetWindowPos(handle,hwnd_topmost,0,0,0,0,swp_nomove or swp_nosize);//Set
SetWindowPos(handle,hwnd_notopmost,0,0,0,0,swp_nomove or swp_nosize);//Unset
但是这个方法不适合于二级窗体。Hide;
form2.ShowModal;// or Show
这个时候,Form2是不能OnTop的。必须修改代码如下:try
Hide;
Application.NormalizeAllTopMosts;
LitterWin.ShowModal;
Application.RestoreTopMosts;
finally
show;
end;
而且,在Form2的OnShow中,必须添加代码:SetWindowPos(handle, hwnd_topmost, 0, 0, 0, 0, swp_nomove or swp_nosize);
procedure TForm2 . CreateParams ( var Para : TCreateParams );
begin
inherited ;
Para . WndParent := Application . MainForm . Handle ;
end ;
同时在Form2的OnShow中添加如下代码:
procedure TForm2 . FormShow ( Sender : TObject );
begin
EnableWindow ( Application . Handle , True );
end ;