以下代码是从VCL中找到的,我没条件试,你可以试看看:
// from ".../Source/VCL/Forms.pas"
procedure TCustomForm.SetWindowToMonitor;
var
AppMon, WinMon: HMONITOR;
I, J: Integer;
ALeft, ATop: Integer;
begin
if (FDefaultMonitor <> dmDesktop) and (Application.MainForm <> nil) then
begin
AppMon := 0;
if FDefaultMonitor = dmMainForm then
AppMon := Application.MainForm.Monitor.Handle
else if (FDefaultMonitor = dmActiveForm) and (Screen.ActiveCustomForm <> nil) then
AppMon := Screen.ActiveCustomForm.Monitor.Handle
else if FDefaultMonitor = dmPrimary then
AppMon := Screen.Monitors[0].Handle;
WinMon := Monitor.Handle;
for I := 0 to Screen.MonitorCount - 1 do
if (Screen.Monitors.Handle = AppMon) then
if (AppMon <> WinMon) then
for J := 0 to Screen.MonitorCount - 1 do
if (Screen.Monitors[J].Handle = WinMon) then
begin
if FPosition = poScreenCenter then
SetBounds(Screen.Monitors.Left + ((Screen.Monitors.Width - Width) div 2),
Screen.Monitors.Top + ((Screen.Monitors.Height - Height) div 2),
Width, Height)
else
begin
ALeft := Screen.Monitors.Left + Left - Screen.Monitors[J].Left;
if ALeft + Width > Screen.Monitors.Left + Screen.Monitors.Width then
ALeft := Screen.Monitors.Left + Screen.Monitors.Width - Width;
ATop := Screen.Monitors.Top + Top - Screen.Monitors[J].Top;
if ATop + Height > Screen.Monitors.Top + Screen.Monitors.Height then
ATop := Screen.Monitors.Top + Screen.Monitors.Height - Height;
SetBounds(ALeft, ATop, Width, Height);
end;
end;
end;
end;