【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[] 获取所有显示器的信息。
相关推荐
大飞记Python31 分钟前
刚从 Win 转 Mac?鼠标滚轮反向、触控板乱跑、第三方鼠标卡顿——这一篇就够了
macos·计算机外设·mac鼠标
优选资源分享13 小时前
Pixelscope v8 屏幕放大镜 | Windows 轻量化鼠标跟随放大工具
计算机外设
ACP广源盛139246256731 天前
ASW3742@ACP# 产品规格详解
网络·人工智能·嵌入式硬件·计算机外设·电脑
nashane1 天前
HarmonyOS 6学习:悬浮键盘抖动修复与长截图“滚动裁缝”实战
学习·计算机外设·harmonyos·harmonyos 5
Joseph Cooper2 天前
Linux HID 子系统实战:从虚拟键盘到 input 事件上报
linux·c语言·计算机外设
私人珍藏库2 天前
[Windows] 鼠标速度锁定/调节工具 KeepMouseSpeedOK v3.55
计算机外设·工具·软件·win·多功能
Name_NaN_None2 天前
Android 手机投屏 iPad :公网+局域网免费方案
网络·计算机外设·电脑·远程工作
善恶怪客2 天前
Vga和Hdmi接口
计算机外设
Jwest20213 天前
佳维视工业显示器在健康体检一体机中的应用
计算机外设
weixin_402278453 天前
解决打开vscode编辑器ctrl+鼠标左键不能跳转定义问题 环境C++
vscode·编辑器·计算机外设