显示模态时调用覆盖的ShowModal时出错

时间:2019-03-05 14:23:36

标签: delphi delphi-10.3-rio

我正在使用显示为“全屏”的模式表单。我设法通过覆盖虚拟ShowModal()方法来做到这一点。

function TfrmComptoir.ShowModal: Integer;
begin
  FullScreen := ReadFromIni('Config.ini', Self.Name, 'FullScreen', False);
  if FullScreen then
  begin
    BorderStyle := bsNone;
    WindowState := wsMaximized;
    width := Screen.Width;
    Height := Screen.Height;
  end else
  begin
    BorderStyle := bsSizeable;
    WindowState := wsMaximized;
  end;

  Result := inherited;
end;

这是显示表单的过程:

procedure TfrmPrincipal.btnComptoirClick(Sender: TObject);
begin
  frmComptoir := TfrmComptoir.Create(nil);
  try
    frmComptoir.ShowModal;
  finally
    FreeAndNil(frmComptoir);
  end;
end;

在我的模态表单上,我有一个按钮让用户在“全屏”和“普通”模式之间切换。这是问题所在。由于出现错误,我无法再次调用ShowModal()方法:

  

无法将可见窗口设为模态

该如何解决?

1 个答案:

答案 0 :(得分:3)

You cannot call ShowModal() while the Form is already showing. Unlike Show(), ShowModal() can only be called once at a time, the Form must be closed before ShowModal() can be called.

What you can do instead is move the property-twiddling code into a method of its own, and then call that method inside of both ShowModal() and the button's OnClick handler, eg:

function TfrmComptoir.ShowModal: Integer;
begin
  SetFullScreen(ReadFromIni('Config.ini', Self.Name, 'FullScreen', False));
  Result := inherited;
  WriteToIni('Config.ini', Self.Name, 'FullScreen', FullScreen);
end;

procedure TfrmComptoir.Button1Click(Sender);
begin
  SetFullScreen(not FullScreen);
end;

procedure TfrmComptoir.SetFullScreen(Value: Boolean);
begin
  FullScreen := Value;
  if FullScreen then
  begin
    BorderStyle := bsNone;
    WindowState := wsMaximized;
    Width := Screen.Width;
    Height := Screen.Height;
  end
  else
  begin
    BorderStyle := bsSizeable;
    WindowState := wsMaximized;
  end;
end;