Смотрим реализацию ShowModal
Код: Выделить всё
function TCustomForm.ShowModal: Integer;
procedure RaiseShowModalImpossible;
begin
DebugLn('TCustomForm.ShowModal Visible=',dbgs(Visible),' Enabled=',dbgs(Enabled),
' fsModal=',dbgs(fsModal in FFormState),' MDIChild=',dbgs(FormStyle = fsMDIChild));
raise EInvalidOperation.Create('TCustomForm.ShowModal impossible ');
end;
procedure RestoreFocusedForm;
begin
// needs to be called only in ShowModal
Perform(CM_DEACTIVATE, 0, 0);
if Screen.FSaveFocusedList.Count > 0 then
begin
Screen.FFocusedForm := TCustomForm(Screen.FSaveFocusedList.First);
Screen.FSaveFocusedList.Remove(Screen.FFocusedForm);
end
else
Screen.FFocusedForm := nil;
end;
var
DisabledList: TList;
SavedFocusState: TFocusState;
ActiveWindow: HWnd;
begin
if Self = nil then
raise EInvalidOperation.Create('TCustomForm.ShowModal Self = nil');
if Application.Terminated then
ModalResult := 0;
// cancel drags
DragManager.DragStop(false);
// close popupmenus
if ActivePopupMenu <> nil then
ActivePopupMenu.Close;
if Visible or not Enabled or (fsModal in FFormState) or (FormStyle = fsMDIChild) then
RaiseShowModalImpossible;
// Kill capture when opening another dialog
if GetCapture <> 0 then
SendMessage(GetCapture, LM_CANCELMODE, 0, 0);
ReleaseCapture;
Application.ModalStarted;
try
Include(FFormState, fsModal);
ActiveWindow := GetActiveWindow;
SavedFocusState := SaveFocusState;
Screen.FSaveFocusedList.Insert(0, Screen.FFocusedForm);
Screen.FFocusedForm := Self;
Screen.MoveFormToFocusFront(Self);
Screen.MoveFormToZFront(Self);
ModalResult := 0;
try
if WidgetSet.GetLCLCapability(lcModalWindow) = LCL_CAPABILITY_NO then
DisabledList := Screen.DisableForms(Self)
else
DisabledList := nil;
Show;
try
// activate must happen after show
Perform(CM_ACTIVATE, 0, 0);
TWSCustomFormClass(WidgetSetClass).ShowModal(Self);
repeat
{ Delphi calls Application.HandleMessage
But HandleMessage processes all pending events and then calls idle,
which will wait for new messages. Under Win32 there is always a next
message, so it works there. The LCL is OS independent, and so it uses
a better way: }
try
WidgetSet.AppProcessMessages; // process all events
except
if Application.CaptureExceptions then
Application.HandleException(Self)
else
raise;
end;
if Application.Terminated then
ModalResult := mrCancel;
if ModalResult <> 0 then
begin
CloseModal;
if ModalResult<>0 then break;
end;
Application.Idle(true);
until False;
Result := ModalResult;
if HandleAllocated and (GetActiveWindow <> Handle) then
ActiveWindow := 0;
RestoreFocusedForm;
finally
Screen.EnableForms(DisabledList);
{ guarantee execution of widgetset CloseModal }
TWSCustomFormClass(WidgetSetClass).CloseModal(Self);
Hide;
// free handles to save resources and to reduce overhead in the interfaces
// for bookkeeping changing between Show and ShowModal.
// (e.g.: the gtk interface creates some specials on ShowModal, so the
// combination ShowModal, Close, Show makes problems.)
DestroyHandle;
end;
finally
RestoreFocusState(SavedFocusState);
if LCLIntf.IsWindow(ActiveWindow) then
SetActiveWindow(ActiveWindow);
Exclude(FFormState, fsModal);
end;
finally
Application.ModalFinished;
end;
end;
Может перед вызовом ShowModal воспользоваться SetActiveWindow(Form1.Handle);
вобще не знаю, не могу вопроизвести ситуацию.
заупускаю из фара и все путем.
Добавлено спустя 24 минуты 56 секунд:Вот демка:
http://turbobit.net/huaz2yfj4yzr.html