【Delphi】实现在多显示器时指定程序运行在某个显示器上

在多显示器时代,经常会出现期望将程序运行在某个指定的显示器上,特别是在调试程序的时候,期望切换分辨率,单步调试时,此时容易导致互相卡住,非常不方便,但是通过指定程序运行在不同的显示器上就可以解决这个问题。

代码如下:

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

procedure MoveFormToScreen(AForm: TForm; ScreenIndex: Integer);
begin
  if (ScreenIndex >= 0) and (ScreenIndex < Screen.MonitorCount) then
  begin
    AForm.Left := Screen.Monitors[ScreenIndex].BoundsRect.Left;
    AForm.Top := Screen.Monitors[ScreenIndex].BoundsRect.Top;
    AForm.Width := Screen.Monitors[ScreenIndex].BoundsRect.Width;
    AForm.Height := Screen.Monitors[ScreenIndex].BoundsRect.Height;

    // 可选:如果只想让窗口在特定屏幕上显示
    // 例如居中
    AForm.Position := poDesigned; 
    AForm.Left := Screen.Monitors[ScreenIndex].BoundsRect.Left +
                  (Screen.Monitors[ScreenIndex].BoundsRect.Width - AForm.Width) div 2;
    AForm.Top := Screen.Monitors[ScreenIndex].BoundsRect.Top +
                 (Screen.Monitors[ScreenIndex].BoundsRect.Height - AForm.Height) div 2;
  end
  else
    raise Exception.Create('Invalid ScreenIndex');
end;

使用示例

在窗体的 OnCreate 事件中调用:

Delphi 复制代码
procedure TForm1.FormCreate(Sender: TObject);
begin
  // 放置在第二个屏幕(索引1)
  MoveFormToScreen(Self, 1);
end;
相关推荐
大飞记Python1 小时前
刚从 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·编辑器·计算机外设