【Delphi】实现在多显示器时指定程序运行在某个显示器上
在多显示器时代,经常会出现期望将程序运行在某个指定的显示器上,特别是在调试程序的时候,期望切换分辨率,单步调试时,此时容易导致互相卡住,非常不方便,但是通过指定程序运行在不同的显示器上就可以解决这个问题。
代码如下:
usesVcl.Forms, Winapi.Windows;procedure MoveFormToScreen(AForm: TForm; ScreenIndex: Integer);
beginif (ScreenIndex >= 0) and (ScreenIndex < Screen.MonitorCount) thenbeginAForm.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;endelseraise Exception.Create('Invalid ScreenIndex');
end;
使用示例
在窗体的 OnCreate
事件中调用:
procedure TForm1.FormCreate(Sender: TObject);
begin// 放置在第二个屏幕(索引1)MoveFormToScreen(Self, 1);
end;