procedure MainFormPaint;
var
ATop, ALeft, AWidth, AHeight, SWidth, SHeight, SLeft, STop, T: integer;
begin
ALeft:= Application.MainForm.Left;
ATop:= Application.MainForm.Top;
AWidth:= Application.MainForm.Width;
AHeight:= Application.MainForm.Height;
SWidth:= Screen.WorkAreaWidth;
SHeight:= Screen.WorkAreaHeight;
STop:= Screen.WorkAreaTop;
SLeft:= Screen.WorkAreaLeft;
if ((SWidth-AWidth) < 0)
or ((SHeight-AHeight) < 0)
then
begin
Application.MainForm.HorzScrollBar.Range:= AWidth;
Application.MainForm.VertScrollBar.Range:= AHeight;
AWidth:= SWidth;
AHeight:= SHeight;
end;
T:= ALeft;
while (T+SLeft+AWidth) > SWidth
do Dec(T);
if T < SLeft
then T:= SLeft;
ALeft:= T;
T:= ATop;
while (T+STop+AHeight) > SHeight
do Dec(T);
if T < STop
then T:= STop;
ATop:= T;
Application.MainForm.SetBounds(ALeft, ATop, AWidth, AHeight);
end;