【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;
相关推荐
dujunqiu2 小时前
S32K328上芯片内部RTC的使用和唤醒配置
mcu·计算机外设
嵌入式牛马在努力帮老板娶老婆1 天前
十三、STM32----LCD屏驱动
stm32·嵌入式硬件·计算机外设
MingYue_SSS1 天前
键盘上面有F3,四,R,F,V,按下没有反应,维修记录
嵌入式硬件·计算机外设·解决办法
KWTXX2 天前
单片机键盘接口程序设计(汇编语言)
单片机·计算机外设·nosql
全栈技术负责人3 天前
移动端富文本markdown中表格滚动与页面滚动的冲突处理:Touch 事件 + 鼠标滚轮精确控制方案
前端·javascript·计算机外设
铉铉这波能秀3 天前
解决Mac电脑连接蓝牙鼠标的延迟问题
macos·计算机外设·鼠标
xqlily3 天前
解决戴尔笔记本电脑键盘按键部分失灵
计算机外设·电脑
曹勖之5 天前
如果被控端显示器分辨率是2k,远程控制软件的画质设置是4k,主控端显示器的分辨率是2k,那主控端看到的被控端画面是几k
计算机外设
卡伦啊8 天前
rk键盘 用蓝牙链接 教程
计算机外设