8

我正在用 Delphi 编写屏幕保护程序。我想要的是在每台显示器上全屏显示一个 TpresentationFrm。为此,我编写了以下(不完整的)程序:

program ScrTemplate;

uses
  ...

{$R *.res}

type
  TScreenSaverMode = (ssmConfig, ssmDisplay, ssmPreview, ssmPassword);

function GetScreenSaverMode: TScreenSaverMode;
begin
  // Some non-interesting code
end;

var
  i: integer;
  presentationForms: array of TpresentationFrm;

begin
  Application.Initialize;
  Application.MainFormOnTaskbar := True;

  case GetScreenSaverMode of
    ssmConfig:
      Application.CreateForm(TconfigFrm, configFrm);
    ssmDisplay:
      begin
        SetLength(presentationForms, Screen.MonitorCount);
        for i := 0 to high(presentationForms) do
        begin
          Application.CreateForm(TpresentationFrm, presentationForms[i]);
          presentationForms[i].BoundsRect := Screen.Monitors[i].BoundsRect;
          presentationForms[i].Visible := true;
        end;
      end
  else
    ShowMessage(GetEnumName(TypeInfo(TScreenSaverMode), integer(GetScreenSaverMode)));
  end;

  Application.Run;
end.

执行代码时ssmDisplay,确实创建了两个表单(是的,我正好有两个监视器)。但它们都出现在第一台监视器上(索引 0,但不是主监视器)。

单步执行代码时,我发现它们Screen.Monitors[i].BoundsRect是正确的,但由于某种原因,表单的边界不正确:

Watch Name                          Value (TRect: Left, Top, Right, Bottom, ...)
Screen.Monitors[0].BoundsRect   (-1680, 0, 0, 1050, (-1680, 0), (0, 1050))
Screen.Monitors[1].BoundsRect   (0, 0, 1920, 1080, (0, 0), (1920, 1080))

presentationForms[0].BoundsRect (-1680, 0, 0, 1050, (-1680, 0), (0, 1050))
presentationForms[1].BoundsRect (-1920, -30, 0, 1050, (-1920, -30), (0, 1050))

第一种形式获得所需的位置,但第二种形式没有。它不是从 x=0 到 1920,而是占用 x=-1920 到 0,即它出现在第一个监视器上,在第一个窗体上方。怎么了?完成我想要的正确程序是什么?

4

4 回答 4

9

表单必须可见才能使用 BoundRect 设置边界。

像这样反转行:

presentationForms[i].Visible := true;
presentationForms[i].BoundsRect := Screen.Monitors[i].BoundsRect;
于 2010-06-25T14:35:24.370 回答
2

显然我试图过早地设置位置。

for循环块替换为

Application.CreateForm(TpresentationFrm, presentationForms[i]);
presentationForms[i].Tag := i;
presentationForms[i].Visible := true;

然后写

procedure TpresentationFrm.FormShow(Sender: TObject);
begin
  BoundsRect := Screen.Monitors[Tag].BoundsRect;
end;
于 2010-06-25T14:32:05.780 回答
1

注意:如果您的应用程序在其清单中不包含 highdpi 感知标志,您将在高 DPI 显示器上遇到问题。在这种情况下,Windows 将报告错误的(虚拟化的)绑定矩形。

一种解决方案是将表单手动移动到您想要的屏幕上,如下所示:

procedure MoveFormToScreen(Form: TForm; ScreenNo: Integer);
begin
 Assert(Form.Position= poDesigned);
 Assert(Form.Visible= TRUE);

 Form.WindowState:= wsNormal;
 Form.Top := Screen.Monitors[ScreenNo].Top;
 Form.Left:= Screen.Monitors[ScreenNo].Left;
 Form.WindowState:= wsMaximized;
end;
于 2017-12-14T13:21:59.957 回答
0

这一步我成功了
比如我们要在第二台显示器上显示表格,它的索引是1

 

program ARPMandiri;

uses
Vcl.Forms,
SysUtils,
UMain in 'UMain.pas' {frmMain},
........
..............................;

{$R *.res}

begin
Application.Initialize;
Application.MainFormOnTaskbar := True;
.............
Application.CreateForm(TfrmMain, frmMain);
frmMain.Visible := true;
frmMain.BoundsRect := Screen.Monitors[1].BoundsRect;
ApplyThemes(frmMain);
Application.Run;
end.


procedure TfrmMain.FormCreate(Sender: TObject);
Var
iTm: Integer;
begin
Self.Left:= Screen.Monitors[1].Left;
Self.Top:= Screen.Monitors[1].Top;
Self.Width:= Screen.Monitors[1].Width;
Self.Height:= Screen.Monitors[1].Height;
...............

end;

于 2020-11-12T14:56:44.570 回答