Board index » delphi » Movable boxes

Movable boxes

I teach first grade and want to do a very simple program with Delphi 1.  I'd
like to make shapes with labels on them.  The labels (captions?) would be
beginning words (see, look, cat and the names of the kids).  The kids could
move each word separately around to make sentences.
Later I would try to enhance the program to make it print, add punctuation
marks, a splash screen, reading of the completed sentence, etc.
But at present I can't make the boxes move when I use ideas from Delphi for
Dummies. And then the Form1 goes black and locks up...
Anyone give me a start to make the shapes?
Thanks,
Joe
 

Re:Movable boxes


In article <frqE1.53$vA1.185...@news.san.rr.com>, "Me" <janth...@san.rr.com>
writes:

Quote
>I teach first grade and want to do a very simple program with Delphi 1.  I'd
>like to make shapes with labels on them.  The labels (captions?) would be
>beginning words (see, look, cat and the names of the kids).  The kids could
>move each word separately around to make sentences.
>Later I would try to enhance the program to make it print, add punctuation
>marks, a splash screen, reading of the completed sentence, etc.
>But at present I can't make the boxes move when I use ideas from Delphi for
>Dummies. And then the Form1 goes black and locks up...
>Anyone give me a start to make the shapes?

Although I roughed out this code using a TShape - you said a Shape <g> - it
really would be better to use a TPanel because you already have the Caption of
the TPanel, whereas a TShape will not parent aTLabel. Just change the component
from Shape1 to Panel1.

Basically you start a drag (note that DragMode is dmManual - you start the
dragging in code) in the OnMouseDown event of the shape at the same time
rememberiung where the mouse was at mousedown, and you start the drag. Then you
set the Accept for the TShape to true if it is the TShape that is dragging.
Then in th OnMouseMove you move the TShape by calculation of where the mouse is
from where it started from.

var
  Form1: TForm1;
  MX, MY : integer;

implementation

{$R *.DFM}

procedure TForm1.Shape1DragOver(Sender, Source: TObject; X, Y: Integer;
  State: TDragState; var Accept: Boolean);
begin
  {Accept the dragged object if you agree}
  Accept := (TShape(Sender).Name = 'Shape1');
end;

procedure TForm1.Shape1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
    {note the mouse position}
   MX := X;
    MY := Y;
    Shape1.BeginDrag(False);
end;

procedure TForm1.Shape1MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  if Shape1.Dragging then begin
    {move the dragged object}
    Shape1.Top := Shape1.Top + Y - MY;
    Shape1.Left := Shape1.Left + X - MX;
  end;
end;

Now comes the clever bit <g>

You write your event handlers in a generic way which can be used for any TShape
and you couple all the TShapes to them (Click and Shift-Click all the TShapes
on your form and then select the AlShapeWhatever event handler in the Whatever
event in the Object inspector.

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

procedure TForm1.AnyShapeMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  MX := X;
  MY := Y;
  TControl(Sender).BeginDrag(False);
end;

procedure TForm1.AnyShapeMouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  With TControl(Sender) do
    if Dragging then begin
      Top := Top + Y - MY;
      Left := Left + X - MX;
    end;
  {end; with Sender}
end;

Note that I've typecast the Sender TObject as a TControl (which has the Top,
Left, Dragging and Accept properties and BeginDrag methods and for which TShape
is a descendent). This means that I can now use the same  handlers not only for
TShapes BUT ALSO for TPanels - in fact for any Control you want to handle.

BTW the correct way to change the handle's name is to selct it in the Object
Inspector and then change the name in the OI selection combo box. Just check
afterwards that the correct handler is specified in the other objects you use
it with..

Alan Lloyd
alangll...@aol.com

Other Threads