Es gibt millionen Lösungen,
aber im Allgemeinen ist es besser das WorkareaRect anstatt dem BoundsRect (Top/Left/Width/Height) zu verwenden.
Delphi-Quellcode:
if not Assigned(Monitor) or Monitor.WorkareaRect.Contains(BoundsRect) then begin
Self.Left := M.WorkareaRect.Left;
Self.Top := M.WorkareaRect.Top;
end;
Delphi-Quellcode:
var M := Self.Monitor;
if not Assigned(M) then
M := Screen.Monitors[0];
if not M.WorkareaRect.Contains(Self.BoundsRect) then begin // or WorkareaRect.IntersectsWith
if Self.Left < M.WorkareaRect.Left then Self.Left := M.WorkareaRect.Left else Self.Left := M.WorkareaRect.Right - Self.Width;
if Self.Top < M.WorkareaRect.Top then Self.Top := M.WorkareaRect.Top else Self.Top := M.WorkareaRect.Bottom - Self.Height;
if Self.Width > M.WorkareaRect.Width then Self.Width := M.WorkareaRect.Width;
if Self.Height > M.WorkareaRect.Height then Self.Height := M.WorkareaRect.Height;
end;
....