我如何确定 screen/monitor 应用程序 运行 在哪个应用程序上?

How can I determine what screen/monitor the application is running on?

在多屏系统上,我希望我的一些应用程序能够记住它们上次 运行 在哪个屏幕上,然后再次 运行 在同一屏幕上。

如何确定当前 screen/monitor 是什么,同时记住屏幕可能具有不同的分辨率?程序编写于 Delphi 2007.

您可以使用 Screen.MonitorFromWindow method this will return a TMonitor class 获取任何形式的当前监视器以及您需要的所有信息。

uses
  MultiMon;

...
...
...
var
 LMonitor : TMonitor;
 LMonitorInfo : TMonitorInfoEx;
begin
  ZeroMemory(@LMonitorInfo, SizeOf(LMonitorInfo));
  LMonitorInfo.cbSize := SizeOf(LMonitorInfo);
  LMonitor:=Screen.MonitorFromWindow(Self.Handle); //pass the handle of the form
  if not GetMonitorInfo(LMonitor.Handle, @LMonitorInfo) then
     RaiseLastOSError;
  ShowMessage(Format('The form is in the monitor Index %d - %s', [LMonitor.MonitorNum, LMonitorInfo.szDevice]));
end;

VCL 公开此信息,例如通过表单的 Monitor property. This is of type TMonitor 公开监视器的各种属性:它的编号、它是否是主监视器、它的边界和工作区域等等。

不过,您要做的只是将表单恢复到 运行 之前的状态。我认为您不需要记住监视器信息。只需执行以下操作:

  1. 当应用程序关闭时记住它的边界矩形。
  2. 当应用程序启动时,将其 bounds rect 设置为记住的值。
  3. 检查表单是否包含在屏幕的矩形边界内。如果表单不在屏幕上,则可能是用户移除了显示器或以其他方式重新排列了他们的屏幕。将表单移到最近的监视器或主监视器上。
  4. 如果 window 可以最大化,则需要额外的细微差别。在这种情况下,请记住 bounds rect 以及应用程序是否最大化。你的朋友是 GetWindowPlacementSetWindowPlacement

David 是正确的,我不需要监视器信息来完成我想做的事情。仅用于文档。 DeskTopWidthDeskTopHeight 给出桌面大小。

我现在在家,只有两个相同分辨率的屏幕。如果我有 4 个屏幕,其中 1 个屏幕的分辨率不同,看看在工作中会发生什么会很有趣。如果这不起作用,那么我可能还必须查看 Monitor 信息。如果有人想知道为什么分辨率不同,第四个是投影仪。