【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;
相关推荐
dldw77715 小时前
银河麒麟定制版系统奇葩问题之鼠标坏了,还原系统
计算机外设
ken22322 天前
在被窝里使用笔记本电脑,容易损坏键盘?
计算机外设·电脑
某林2122 天前
如何使用ROS 2与STM32进行串口通信,并实现通过键盘按键‘1’来控制LED灯开关
stm32·嵌入式硬件·计算机外设
阿昭L3 天前
计算机外设与CPU通信
汇编·计算机外设
Xxtaoaooo4 天前
从普通屏到 明基RD320U:一台显示器如何提升我的编码效率?
计算机外设·专业工具·明基rd320u·专业编程显示器·提效工具
扬道财经6 天前
中小手抓握鼠标推荐:罗技GPW4小狗版,为亚洲手型而生的“小尺寸旗舰”
计算机外设
扬道财经6 天前
2025年小手无线鼠标推荐:罗技GPW4小狗版凭“小尺寸+大性能”成为“小手神器”
计算机外设
电脑小管家8 天前
笔记本蓝牙怎么开启 完整教程
windows·驱动开发·计算机外设·电脑·音频
min9701198 天前
键盘鼠标做一份CE\FCC证书需要多少钱?
计算机外设
非凡ghost11 天前
批量转双层PDF(可识别各种语言) 中文绿色版
前端·windows·pdf·计算机外设·软件需求