Simply put, your moving code should do this:
- When the mouse goes down, check to see if the mouse position is above the control that you can drag. If so, set a variable named
FDragControl to refer to this control. This code lives in the OnMouseDown event handler. - When the mouse moves, if
FDragControl not zero, move the control. This code is in the OnMouseMove event handler. - When the mouse rises, set
FDragControl to zero.
Thatβs almost all you need. The main caveat is that you must also remember the X, Y values ββof the mouse when you start dragging. Therefore, in your OnMouseDown handler, you write:
FStartMousePos := Point(X, Y); FStartDragControlPos := Point(FDragControl.Left, FDragControl.Top);
And then in OnMouseMove your position code reads:
FDragControl.Left := FStartDragControlPos.X + (X-FStartX); FDragControl.Top := FStartDragControlPos.Y + (Y-FStartY);
You will also need to grab the mouse when you start to drag.
The resize code is the same. Again, you need to decide in OnMouseDown that you are resizing rather than dragging and dropping, but the code still includes mouse processing, moving, and events.
As for drawing, you need to force redrawing when one of your event handlers changes a property that will affect the appearance of your form. You can use the FDragControl value to decide whether to use a special drawing of your control and indicate that it is being dragged. And also for resizing.
I did not code a full working implementation, since your question is high and conceptual. The implementation is up to you.
source share