【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;
相关推荐
小灰灰搞电子8 小时前
QAccel 类讲解与代码示例:Qt 3 中的键盘加速键管理
qt·计算机外设
芝麻粒儿5 天前
Scratch少儿编程案例-鼠标控制练习
游戏·青少年编程·计算机外设·scratch·少儿编程
开开心心就好6 天前
显示器关不掉怎么办,用小工具一键息屏
算法·智能手机·pdf·计算机外设·ocr·lua·1024程序员节
Helen_Liang_七仔的AI工具箱9 天前
读英文画册总卡词?鼠标停 0.3 秒,生词自己浮出来(附阅读智能悬浮翻译器skill安装地址)
计算机外设
Hello_Pyhx12 天前
Hookshot v2.1.0:用键鼠玩《时之笛》,把操控和画面调顺手
计算机外设
Rattenking12 天前
OpenCV 轨迹栏操作和键盘响应操作
人工智能·opencv·计算机外设
jllws118 天前
键盘实现输入的思路
计算机外设
不脱发的程序猿20 天前
STM32制作单手鼠标键盘
stm32·嵌入式硬件·计算机外设·usb·qmk
AntsData22 天前
用 12K+ 条亚马逊评论信号,定位机械键盘下一版该改什么
java·大数据·计算机外设
时间的情敌22 天前
Vue3 + Cesium 鼠标交互绘制多边形 / 线(贴地绘制,动态预览,Entity 实现)
计算机外设