【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;
相关推荐
网络探索者21 小时前
换了新显示器怎么验?我做了一个开箱即用的全平台屏幕检测工具
计算机外设
weixin_423995001 天前
unity 物体转向鼠标点击方向2d和3d
unity·计算机外设·游戏引擎
破烂儿2 天前
TMUX历史输出滚动查看全攻略(原生快捷键 + 鼠标配置优化)
服务器·学习·计算机外设
开开心心就好3 天前
支持自定义名单的实用随机抽签工具
windows·计算机视觉·计算机外设·excel·散列表·启发式算法·csdn开发云
梁山好汉(Ls_man)3 天前
建议鸿蒙系统增加虚拟鼠标功能
华为·计算机外设·harmonyos
杰杰桀桀桀3 天前
4*4无时延矩阵键盘(非阻塞)--附代码链接
stm32·单片机·嵌入式硬件·矩阵·计算机外设·无时延矩阵键盘
淮北4943 天前
全键盘最后一公里warpd
计算机外设
ZC跨境爬虫4 天前
Playwright模拟鼠标滚轮实战:从原理到百度图片_豆瓣电影爬取
爬虫·python·计算机外设
sxjk19876 天前
每日一问-20260405--戴尔显示器型号命名速查表
计算机外设
chenqianghqu6 天前
win10远程桌面访问桌面版ubuntu22.04(无显示器)
计算机外设