Hi edexter,

This is just some basic maths from what I can see.

If my understanding is correct, you want to calculate the position of a rectangle (top left = RX1,RY1, bottom right = RX2, RY2) based on some mouse coordinates (mx,my) and some valid range (minX, minY, maxX, maxY) where the rectangle can be. Is that about right?

If so, I think this will work. Note:- When the entire rectangle is the allowed range, the center will follow the mouse.

[pascal]
procedure getRectangleCoords(mx,my:integer;minX,minY,maxX,ma xY:integer;width,height:integer;var rx1,ry1,rx2,ry2:integer);
begin
rx1:=mx-(width div 2);
ry1:=my-(height div 2);
rx2:=rx1+width;
ry2:=ry1+height;

if (rx1<minX>maxX) then rx1:=maxX-width;
if (ry1<minY>maxY) then ry1:=maxY-height;

rx2:=rx1+width;
ry2:=ry1+height;
end;
[/pascal]

So, quick explanation... calculate an initial top left corner position, then the bottom right position. Then check the coordinates fit into the bounding box and adjust the top left corner accordingly if they don't. The final stage, recalculate the bottom right corner based on the revised top left corner coordinates.

As for graphics libraries relating to freepascal, I can't help I'm afraid.