I’m trying to create a small bit of software to mess around with drag and drop a bit. I started out with only creating TButtons dynamically and it worked fine.
However when generalizing the functionality I’ve run into a problem due to the ‘protected’ status of the OnDragDrop and OnMouseDownEvent of the base class TControl.
procedure TForm1.FormDragDrop(Sender, Source: TObject; X, Y: Integer);
var
newControl: TControl;
selClass: TControlClass;
ctlName: string;
selItem: string;
begin
if TControl(Sender).Parent = Self then
begin
with TWinControl(Source) do
begin
Left:= X;
Top:= Y;
EndDrag(True); {drop the control}
end;
end
else begin
selItem:= TypeList.Items[TypeList.ItemIndex];
selClass:= TControlClass(GetClass(selItem));
newControl:= selClass.Create(Self);
newControl.Parent:= Self;
ctlName:= newControl.ClassName + IntToStr(GetControlCount(selClass));
Delete(ctlName, 1, 1); {Remove 'T' from name}
newControl.Name:= ctlName;
newControl.Left:= X;
newControl.Top:= Y;
{ TODO : assign events onDragDrop and onMouseDown}
(*
newControl.OnMouseDown:= @ControlMouseDown;
newControl.OnDragDrop:= @FormDragDrop;
*)
end;
end;
Add the following declaration to your unit:
and use a type cast for the new control: