【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;
相关推荐
卢锡荣6 小时前
Type-c OTG数据与充电如何进行交互使用应用讲解
c语言·开发语言·计算机外设·电脑·音视频
goodmao6 小时前
【macOS】【mouse】设置鼠标大小
macos·计算机外设·鼠标·mouse
开开心心就好12 小时前
键盘映射工具改键位,绿色版设置后重启生效
网络·windows·tcp/ip·pdf·计算机外设·电脑·excel
卢锡荣13 小时前
Type-c小家电性价比方案讲解LDR系列
网络·人工智能·计算机外设·电脑
春日见1 天前
win11 分屏设置
java·开发语言·驱动开发·docker·单例模式·计算机外设
TESmart碲视2 天前
如何用一套键盘鼠标切换多个输入设备?TESmart KVM热键工作原理深度解析.一份涵盖设计原理、使用方法与兼容性的完整技术指南
游戏·macos·计算机外设·kvm切换器·双屏kvm切换器
JosieBook2 天前
【Vue】13 Vue技术—— Vue 中的键盘事件处理详解
javascript·vue.js·计算机外设
Hody913 天前
【XR开发系列】与玩家交互 - 用键盘控制小球移动
计算机外设·交互
一起养小猫4 天前
Flutter for OpenHarmony 实战:双控制系统实现(按钮+键盘)
android·flutter·计算机外设·harmonyos
cong*5 天前
搜狗输入法云计算代理导致Windows 10鼠标指针不停转圈的解决方案
windows·计算机外设