【delphi】判断多显示器下,程序在那个显示器中

Delphi 中,如果你的电脑连接了多个显示器,可以通过以下步骤判断某个程序在哪个显示器上运行。

方法概述:

  1. 获取程序窗口的位置(例如窗体的 LeftTop 坐标)。
  2. 使用 Screen.MonitorFromWindow 函数来确定该窗口所属的显示器。

关键概念:

  • TMonitor :表示一个显示器,包含该显示器的边界(BoundsRect)和工作区域(WorkAreaRect)。
  • Screen.Monitors[]:存储当前连接的所有显示器。
  • MonitorFromWindow:根据窗口句柄获取所属的显示器。

示例代码:

假设我们需要判断当前窗体 Form1 所在的显示器。

Delphi 复制代码
uses
  Winapi.Windows, Vcl.Forms, Vcl.Controls, System.SysUtils;

procedure TForm1.CheckMonitor;
var
  Monitor: TMonitor;
  MonitorIndex: Integer;
begin
  // 获取窗体所在的显示器
  Monitor := Screen.MonitorFromWindow(Self.Handle, mdNearest);

  // 获取显示器的索引
  MonitorIndex := Monitor.MonitorNum;

  // 输出显示器信息
  ShowMessage(Format('The form is running on monitor %d with resolution %dx%d',
    [MonitorIndex, Monitor.BoundsRect.Width, Monitor.BoundsRect.Height]));
end;

解释:

  1. Screen.MonitorFromWindow(Self.Handle, mdNearest) :这是核心函数。Self.Handle 代表窗体句柄,mdNearest 表示如果窗口跨多个显示器,则选择最近的显示器。
  2. Monitor.MonitorNum:获取显示器的索引号。
  3. Monitor.BoundsRect:获取显示器的分辨率和位置。

显示器检测选项:

  • mdNearest:获取最近的显示器。
  • mdPrimary:获取主显示器。

获取当前所有显示器的信息:

如果你想列出所有连接的显示器并获取相关信息,可以通过 Screen.Monitors[] 来完成。

Delphi 复制代码
procedure ListAllMonitors;
var
  I: Integer;
begin
  for I := 0 to Screen.MonitorCount - 1 do
  begin
    ShowMessage(Format('Monitor %d: Resolution = %dx%d, Work Area = %dx%d',
      [I + 1,
      Screen.Monitors[I].BoundsRect.Width,
      Screen.Monitors[I].BoundsRect.Height,
      Screen.Monitors[I].WorkAreaRect.Width,
      Screen.Monitors[I].WorkAreaRect.Height]));
  end;
end;

总结:

  1. 使用 Screen.MonitorFromWindow 判断当前窗口运行在哪个显示器上。
  2. 通过 Screen.Monitors[] 获取所有显示器的信息。
相关推荐
剁椒排骨1 天前
win11什么都不动之后一段时间黑屏桌面无法显示,但鼠标仍可移动,得要熄屏之后才能进入的四种解决方法
运维·windows·经验分享·计算机外设·win11·win10
iFlyCai1 天前
对Mac文字双击或三击鼠标左键没有任何反应
macos·计算机外设
伊织code1 天前
BongoCat - 跨平台键盘猫动画工具
计算机外设·动画·键盘·bongocat
m0_738355692 天前
护眼-科学使用显示器
计算机外设
xiaowu0803 天前
C# 使用Windows API实现键盘钩子的类
windows·c#·计算机外设
成工小白3 天前
【Linux】冯诺依曼体系结构及操作系统架构图的具体剖析
linux·计算机外设
transformer_WSZ4 天前
赛睿鼠标设置宏
计算机外设
活跃家族4 天前
UI键盘操作
ui·计算机外设
啊丢_5 天前
配置tmux ctrl+a与鼠标点击切换
计算机外设
是Dream呀6 天前
AI写代码时代,我却被这块28寸方屏显示器圈了粉!
人工智能·计算机外设·明基