5,388
社区成员
发帖
与我相关
我的任务
分享
procedure TCustomForm.SetWindowToMonitor;
var
AppMon, WinMon: HMONITOR;
I, J: Integer;
ALeft, ATop: Integer;
LRect: TRect;
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.PrimaryMonitor.Handle;
WinMon := Monitor.Handle;
for I := 0 to Screen.MonitorCount - 1 do
if (Screen.Monitors[I].Handle = AppMon) then
if (AppMon <> WinMon) then
begin
for J := 0 to Screen.MonitorCount - 1 do
begin
if (Screen.Monitors[J].Handle = WinMon) then
begin
if FPosition = poScreenCenter then
begin
LRect := Screen.Monitors[I].WorkareaRect;
SetBounds(LRect.Left + ((RectWidth(LRect) - Width) div 2),
LRect.Top + ((RectHeight(LRect) - Height) div 2), Width, Height);
end
else
if FPosition = poMainFormCenter then
begin
SetBounds(Screen.Monitors[I].Left + ((Screen.Monitors[I].Width - Width) div 2),
Screen.Monitors[I].Top + ((Screen.Monitors[I].Height - Height) div 2),
Width, Height)
end
else
begin
ALeft := Screen.Monitors[I].Left + Left - Screen.Monitors[J].Left;
if ALeft + Width > Screen.Monitors[I].Left + Screen.Monitors[I].Width then
ALeft := Screen.Monitors[I].Left + Screen.Monitors[I].Width - Width;
ATop := Screen.Monitors[I].Top + Top - Screen.Monitors[J].Top;
if ATop + Height > Screen.Monitors[I].Top + Screen.Monitors[I].Height then
ATop := Screen.Monitors[I].Top + Screen.Monitors[I].Height - Height;
SetBounds(ALeft, ATop, Width, Height);
end;
end;
end;
end
else
begin
if FPosition = poScreenCenter then
begin
LRect := Screen.Monitors[I].WorkareaRect;
SetBounds(LRect.Left + ((RectWidth(LRect) - Width) div 2),
LRect.Top + ((RectHeight(LRect) - Height) div 2), Width, Height);
end;
end;
end;
end;