A long time ago, for the purpose of a thread on another forum, I wrote a simple application for moving virtual rectangles in a window. The code of this application was written in Delphi 7 and it was quite long, so I decided to refresh it a bit—use generic lists and generally the richer functionality of Free Pascal. Thus, the source of this application is added to the attachments.
The program displays several colored rectangles that you can drag in the window with the left mouse button. To render them, it uses the TPaintBox control. Changes to the position of rectangles while dragging are visible when you drag. The program has protection against moving the rectangle outside the window. If the rectangle touches the edge of the window, the appropriate information is displayed inside rectangle.
I hope that the code of this application will come in handy. Good luck.