【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;
相关推荐
碎碎思12 小时前
打破延迟极限的 FPGA 机械键盘
fpga开发·计算机外设
Despacito0o2 天前
瀚文机械键盘固件开发详解:HWKeyboard.cpp文件解析与应用
数据库·mongodb·计算机外设
步达硬件2 天前
【原理解析】为什么显示器Fliker dB值越大,闪烁程度越轻?
计算机外设·显示器
感叹号的豆浆2 天前
使用qt 定义全局钩子 捕获系统的键盘事件
qt·计算机外设
Despacito0o3 天前
瀚文(HelloWord)智能键盘项目深度剖析:从0到1的全流程解读
计算机外设
开开心心就好3 天前
高效视频倍速播放插件推荐
python·学习·游戏·pdf·计算机外设·电脑·音视频
Despacito0o4 天前
APM32主控键盘全功能开发实战教程:软件部分
驱动开发·计算机外设
Java&Develop5 天前
Windows如何定制键盘按键
计算机外设
LAM LAB6 天前
【键盘说明书备份】ENERGYFORT
计算机外设