【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;
相关推荐
coding4u15 小时前
Mac 外接显示器完全指南:从插上线到调好每个参数
macos·计算机外设·文件管理·显示器·策略模式·quick look扩展
秋枫要学习2 天前
你的鼠标,正在被 AI 抢走
人工智能·计算机外设
你怎么知道我是队长3 天前
计算机中的文件管理介绍
计算机外设
科技每日热闻5 天前
飞利浦5K双模商用显示器34B2U5900C——全能宽屏双模,多场景适配!
科技·计算机外设·显示器
羽翼安全5 天前
如何防止电脑显示器被手机拍照泄密?6款防拍屏软件测评
智能手机·计算机外设·电脑
单片机杂货铺5 天前
基于单片机的智能粮仓控制系统设计与实现
stm32·单片机·嵌入式硬件·物联网·计算机外设·51单片机·课程设计
上海广测检测科技有限公司7 天前
蓝牙鼠标键盘台湾NCC认证详解:射频器材型式核准制度与技术标准解读
计算机外设
Fozei8 天前
录屏原理与鼠标可视化
计算机外设·策略模式
驱动小百科9 天前
笔记本电脑键盘按键失灵怎么修复 教你快速恢复键盘正常输入
计算机外设·电脑·笔记本电脑键盘失灵·笔记本键盘按键没反应·笔记本键盘部分按键失灵
蓝斯4979 天前
从.到实现一个Windows上的虚拟hid键盘设备
windows·单片机·计算机外设