function GetCurrentMonitor(AForm: TCustomForm): Integer;
var
Rect: TRect;
i, A, maxA: Integer;
begin
{
man kann sich diese Funktion auch sparen und einfach AForm.Monitor aufrufen,
wobei diese u.U. nicht so gründlich und schön auf die Maximalnutzung prüft
}
Result := 0;
maxA := 0;
for i := 0
to Screen.MonitorCount-1
do
if IntersectRect(Rect, Screen.Monitors[i].BoundsRect, AForm.BoundsRect)
then
begin
A := (Rect.Right-Rect.Left) * (Rect.Bottom-Rect.Top);
if A > maxA
then
begin
maxA := A;
Result:=i;
end;
end;
end;
function FormFullInView(AForm: TCustomForm): Boolean;
var
MonNo,
MonL, MonW,
MonT, MonH: Integer;
begin
MonNo := GetCurrentMonitor(AForm);
MonL := Screen.Monitors[MonNo].WorkareaRect.Left;
MonW := Screen.Monitors[MonNo].WorkareaRect.Right - Screen.Monitors[MonNo].WorkareaRect.Left;
MonT := Screen.Monitors[MonNo].WorkareaRect.Top;
MonH := Screen.Monitors[MonNo].WorkareaRect.Bottom - Screen.Monitors[MonNo].WorkareaRect.Top;
// handle multi-monitor windows
if (AForm.Left >= Screen.DesktopLeft)
and
(AForm.Top >= Screen.DesktopTop)
and
((AForm.Left + AForm.Width) <= (Screen.DesktopWidth + Screen.DesktopLeft))
and
((AForm.Top + AForm.Height) <= (Screen.DesktopHeight + Screen.DesktopTop))
then
Result := True
else
Result:= (AForm.Left >= MonL)
and (AForm.Top >= MonT)
and
(AForm.Left + AForm.Width <= MonL + MonW)
and
(AForm.Top + AForm.Height <= MonT + MonH);
end;
procedure FitFormFullInView(AForm: TCustomForm);
var
MonNo,
MonL, MonW,
MonT, MonH: Integer;
begin
if not (FormFullInView(AForm))
then
begin
MonNo := GetCurrentMonitor(AForm);
MonL := Screen.Monitors[MonNo].WorkareaRect.Left;
MonW := Screen.Monitors[MonNo].WorkareaRect.Right - Screen.Monitors[MonNo].WorkareaRect.Left;
MonT := Screen.Monitors[MonNo].WorkareaRect.Top;
MonH := Screen.Monitors[MonNo].WorkareaRect.Bottom - Screen.Monitors[MonNo].WorkareaRect.Top;
with AForm
do
begin
if Height > MonH
then
Height := MonH;
if Width > MonW
then
Width := MonW;
Left := MonL + (MonW-Width)
div 2;
Top := MonT + (MonH-Height)
div 2;
end;
end;
end;