在多顯示器時代,經常會出現期望將程序運行在某個指定的顯示器上,特別是在調試程序的時候,期望切換分辨率,單步調試時,此時容易導致互相卡住,非常不方便,但是通過指定程序運行在不同的顯示器上就可以解決這個問題。
代碼如下:
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;