Board index » delphi » Dragging controls Created at runtime?

Dragging controls Created at runtime?

Hello,

I'm trying to implement drag and drop in an application:

* I create a TPanel at runtime and size it to client area of form.
* I create buttons at runtime and assign the TPanel as Parent.

When buttons are created, I send DragMode to dmAutomatic.

At runtime, the buttons look like they want to drag, but I get the circle
with diagonal line cursor say I can't

I tried assigning the DragOver even of the TPanel to the form and setting
Accept to True there.  That didn't work either.  Do I/Can I create procedure
and assign the DragOver event to that?

I'm a little lost.  Apparently, it's a little trickier with object created
at runtime?

Thank for your help,

Lee

 

Re:Dragging controls Created at runtime?


I finally found an excellent example posted by Peter Below that I was able
to adapt to what I need to do.

Thanks anyway,

Lee

Re:Dragging controls Created at runtime?


Quote
"Lee Jenkins" <lee_jenkin...@datatrakpos.com> wrote in message

news:tJNX7.10789$8e2.5198349@typhoon.southeast.rr.com...

Quote

> Hello,

> I'm trying to implement drag and drop in an application:
> * I create a TPanel at runtime and size it to client area of form.
> * I create buttons at runtime and assign the TPanel as Parent.

> When buttons are created, I send DragMode to dmAutomatic.

> At runtime, the buttons look like they want to drag, but I get the circle
> with diagonal line cursor say I can't

> I tried assigning the DragOver even of the TPanel to the form and setting
> Accept to True there.  That didn't work either.  Do I/Can I create
procedure
> and assign the DragOver event to that?

> I'm a little lost.  Apparently, it's a little trickier with object created
> at runtime?

> Thank for your help,

you tell it to accept but haven't given it anything to do

on a form and a button to allow dragging of the button
between form and panel

procedure TForm1.FormDragOver(Sender, Source: TObject; X, Y: Integer;
  State: TDragState; var Accept: Boolean);
begin
 accept:=true;
end;

procedure TForm1.FormDragDrop(Sender, Source: TObject; X, Y: Integer);
begin
   TWincontrol(source).parent:=TWincontrol(sender);
   with source as Twincontrol do
   begin
    left:=x;
    top:=y;
   end;
end;

//you could select to reuse the same events  for the panel in the object
inspector
//rather than code them again

procedure TForm1.Panel1DragOver(Sender, Source: TObject; X, Y: Integer;
  State: TDragState; var Accept: Boolean);
begin
accept:=true;
end;

procedure TForm1.Panel1DragDrop(Sender, Source: TObject; X, Y: Integer);
begin
   TWincontrol(source).parent:=TWincontrol(sender);
   with source as Twincontrol do
   begin
    left:=x;
    top:=y;
   end;
end;

//alternately clickable and draggable button

type TexposedTcontrol=class (TControl) end;
//exposes the protected dragmode..amd everything else
//just to illustrate how it can be done,don't do this willy-nilly

procedure TForm1.draggablebuttonClick(Sender: TObject);
begin

with sender as Tcontrol do
begin
  form1.caption:='clicked '+name+' on '+parent.name;
end;
  TexposedTcontrol(sender).dragmode:=dmautomatic;
  //because a 'with' will take exception to the the exposed class
end;

procedure TForm1.draggablebuttonEndDrag(Sender, Target: TObject; X,
  Y: Integer);
begin
with sender as Tcontrol do
begin
  form1.caption:='moved '+name+' to '+parent.name;
end;
  TexposedTcontrol(sender).dragmode:=dmmanual;
end;

If you just want to drag about in the one region:
The following will allow dragging runtime created buttons around a panel ,
their parent,
when the shift key is held down, or however you choose to decide when to
drag.

TForm1 = class(TForm)
...
    procedure MakeAutoButtonsClick(Sender: TObject);
    procedure AutoButtonClick(Sender: TObject);
    procedure AutoButtonMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure ToggleDragClick(Sender: TObject);

...

//declared somewhere global or in class
const  SC_DragMove = $F012;  { thanks to originator..somewhere on google}
var buttonum:integer=0;
var tagem:boolean=true;

procedure TForm1.MakeAutoButtonsClick(Sender: TObject);
var B:Tbutton;
begin
   b:=tbutton.create(form1);;
   with b do
   begin
    parent:=panel1;
    caption:=inttostr(buttonnum)+' autocreated';
    top:=10;left:=10;
    onmousedown:=AutoButtonmousedown;
    onclick:=AutoButtonClick;
    tag:=buttonnum;
    inc(buttonnum);
    taborder:=32767;
   end;
end;

procedure TForm1.AutoButtonClick(Sender: TObject);
begin
   form1.caption:='Autobutton '+inttostr(Tbutton(sender).tag)+' Clicked';
end;

procedure TForm1.AutoButtonMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if ssshift in shift then
 // if (TButton(sender).tag and $40000000) <> 0 then  //tag->flag ;-)
  begin
  form1.caption:='Dragging Autobutton '+inttostr(Tbutton(sender).tag);
  ReleaseCapture;
  Tcontrol(sender).perform(WM_SysCommand, SC_DragMove, 0);
  end;
end;

procedure TForm1.ToggleDragClick(Sender: TObject);
var i:integer;
begin
   if tagem then
   begin
     for i:=0 to panel1.ControlCount-1 do
      if panel1.Controls[i] is TButton then
         panel1.Controls[i].tag := panel1.Controls[i].tag or $40000000;
   end
   else
   begin
     for i:=0 to panel1.ControlCount-1 do
      if panel1.Controls[i] is TButton then
         panel1.Controls[i].tag := panel1.Controls[i].tag and $Afffffff;
   end;
   tagem:=not tagem;
end;

Re:Dragging controls Created at runtime?


Thank Terry, for the excellent examples.

Lee

Other Threads