Board index » delphi » Dragging labels at runtime

Dragging labels at runtime

At runtime I want to create labels (or StaticText) and fields which can be
dragged around the window and dropped to a new position as you can in
delphi's form designer.

I know I could use drag and drop using the same window as the drop zone but
it's not very good.  I've briefly tried using 'onmouse..' events for the
label but it seems to jump about wildy when dragged.

Any ideas ?

--
Martin
Two Smoking Barrels - Single Player Half-Life level
http://www.geocities.com/TimesSquare/Bunker/4004/

 

Re:Dragging labels at runtime


I have a 3d Label component that has that functionality.  You might want to
take a look it.

The Property AllowUserChange is the switch that allows movement.  Just set
it to True during runtime and it will be moveable.  Set it back to False
when you're done.

http://www.bicnet.net/~skz/Label3d.pas

-Steve-

Re:Dragging labels at runtime


Quote
Martin wrote:
> At runtime I want to create labels (or StaticText) and fields which can be
> dragged around the window and dropped to a new position as you can in
> delphi's form designer.

> I know I could use drag and drop using the same window as the drop zone but
> it's not very good.  I've briefly tried using 'onmouse..' events for the
> label but it seems to jump about wildy when dragged.

> Any ideas ?

You can do label invisible when it starts dragging and do it visible againd
when it released like this:
procedure TForm1.FormDragOver(Sender, Source: TObject; X, Y: Integer;
  State: TDragState; var Accept: Boolean);
begin
  Accept := Source is TLabel;
{  if accept then//or if you want to see how label moves use this commented
code
  begin
    TLabel(Source).Left := X;
    TLabel(Source).Top := Y;
  end; }
end;

procedure TForm1.Label1StartDrag(Sender: TObject;
  var DragObject: TDragObject);
begin
  TLabel(Sender).Visible := False;
end;

procedure TForm1.Label1EndDrag(Sender, Target: TObject; X, Y: Integer);
begin
  if Target is TForm1 then
  begin
  TLabel(Sender).Left := X;
  Tlabel(Sender).Top := Y;
  end;
  TLabel(Sender).Visible := True;
end;

Re:Dragging labels at runtime


Martin,
My trick only works with Controls that have a Window handle, so TStaticText
will be OK, but TLabel will not.

Assign an OnMouseDown event to the TStaticText:

procedure TForm1.TheMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if CheckBox1.Checked then
  begin
    ReleaseCapture;
    (Sender as TControl).Perform(WM_SysCommand, $F012, 0);
  end;
end;

Where the Checkbox1 could be the 'Enter design mode'-checkbox.

HTH,
Mikael

Quote
Martin <Mar...@rdc.in2home.co.uk> wrote in message

news:940276391.12987.0.pink.d4e11021@news.in2home.co.uk...
Quote
> At runtime I want to create labels (or StaticText) and fields which can be
> dragged around the window and dropped to a new position as you can in
> delphi's form designer.

> I know I could use drag and drop using the same window as the drop zone
but
> it's not very good.  I've briefly tried using 'onmouse..' events for the
> label but it seems to jump about wildy when dragged.

> Any ideas ?

> --
> Martin
> Two Smoking Barrels - Single Player Half-Life level
> http://www.geocities.com/TimesSquare/Bunker/4004/

Other Threads