size : 2909
uploaded_on : Wed Oct 14 00:00:00 1998
modified_on : Wed Dec 8 14:03:17 1999
title : Drag controls
org_filename : DragCntrls.txt
author : unknown
authoremail :
description : How to drag controls and have an outline of the dragged control follow the cursor
keywords :
tested : not tested yet
submitted_by : Mike Orriss
submitted_by_email : mjo@3kcc.co.uk
uploaded_by : nobody
modified_by : nobody
owner : nobody
lang : plain
file-type : text/plain
category : delphi-commoncontrols
__END_OF_HEADER__
How to drag controls and have an outline of the dragged control follow the cursor. The problem you noted was that just drawing on the form won't work because components get in the way.
In general, when you want to scribble on the surface of a form or even the desktop, you make a bitmap COPY of the window or desktop and draw on that. That's what we need to do here.
Start with a fresh form. Drop a notebook on it and set its Align to alClient. Design your form on the first page of the notebook. Create a second page for the notebook and drop a paint box on it, setting the paint box's Align to alClient. Add these lines to the Private declarations for the form:
Img : TBitmap;
DragX, DragY, DragW, DragH, XOff, YOff : Integer;
In the form's OnCreate handler:
Img := TBitmap.Create;
In an OnMouseDown handler shared by all the draggable components:
IF NOT (ssShift IN Shift) THEN Exit;
Img := GetFormImage;
Notebook1.PageIndex := 1;
WITH Sender AS TControl DO
BEGIN
DragW := Width;
DragH := Height;
XOff:= X;
YOff := Y;
BeginDrag(True);
END;
In the EndDrag handler for all the draggable components:
Notebook1.PageIndex := 0;
WITH Sender AS Tcontrol DO
BEGIN
Left := X-Xoff;
Top := Y-YOff;
END;
Put this line in the paint box's OnPaint:
PaintBox1.Canvas.Draw(0, 0, Img);
FINALLY, put these lines in the paint box's OnDragOver:
IF (X=DragX) AND (Y=DragY) THEN Exit;
WITH PaintBox1.Canvas DO
BEGIN
DrawFocusRect(Bounds(DragX-XOff, DragY-YOff, DragW, DragH);
DragX := X; DragY := Y;
DrawFocusRect(Bounds(DragX-XOff, DragY-YOff, DragW, DragH);
END;
WHEW! But it works! I didn't want to disable ordinary mousing on the draggable components, so you only drag them around if you're holding Shift when you start. Try it!
- Neil
I am trying to drag a TPanel being used as a ToolBar and I always get the circle with a slash Icon. I assume that means it can't drag. I haven't found any doc on why this would be. I have tried the manual and automatic settings with no luck
I can't get that dang TPanel to budge.
You assume wrong! The reason you get the "no entry" cursor is because whatever is under the cursor is not set to receive drag and drop. To do that, double click on the form's or a components OnDragOver event and in the handler set Accept to true, e.g.
procedure TForm1.FormDragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
begin
Accept := true ;
end;
Thanks for the extensive example of how to draw the rectangle while dragging. Your instructions worked perfectly the first time, and easily integrated into my own work. If you don't mind, could