Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Moving components at runtime

284 views
Skip to first unread message

Karim Amin

unread,
Dec 29, 1999, 3:00:00 AM12/29/99
to
I am trying to move a TShape component located on a form at runtime. I
want to do something similar to what Delphi does when you are dragging
and dropping components. Can someone help me out with any information.
I assume I need to someone get the position of the mouse cusor in
relation to the form and not the component. Then when I move, I need to
move the shape accordingly. What is the best way to do this?


Arthur E.F.Heinrich

unread,
Dec 29, 1999, 3:00:00 AM12/29/99
to
Create two variables to store mouse coordinates (integer) and an aditional
variable to indicate if the mouse is pressed (boolean).

In the OnMouseDown event store the the mouse coordinates and set the flag to
indicate mouse pressed.

In the OnMouseUp reset the flag

In the OnMouseMove, compare the new coordinates with the previous one stored
in your variables. Use the difference to change Left and Top properties of
your component.

Arthur

Karim Amin escreveu na mensagem <386A4CBF...@microtaskinc.com>...

Mark Vaughan

unread,
Dec 29, 1999, 3:00:00 AM12/29/99
to
ka...@microtaskinc.com (Karim Amin) wrote in
<386A4CBF...@microtaskinc.com>:

>I am trying to move a TShape component located on a form at runtime. I
>want to do something similar to what Delphi does when you are dragging
>and dropping components. Can someone help me out with any information.
>I assume I need to someone get the position of the mouse cusor in
>relation to the form and not the component. Then when I move, I need to
>move the shape accordingly. What is the best way to do this?
>

Borland publishes a Technical Information document that answers
your question nicely...

TI1057D.txt How to click and move components at runtime.

http://www.borland.com/devsupport/delphi/ti/TI1057D.html

--


Mark Vaughan
___________

Visit the Numerical Methods in Pascal web page at
ftp://cloudlab.larc.nasa.gov/fNMPhome.htm


Karim Amin

unread,
Dec 30, 1999, 3:00:00 AM12/30/99
to
Wow, every time I say something bad about Delphi, I find 5 things better to
say :). Is there one that resizes components?

Karim Amin

unread,
Dec 30, 1999, 3:00:00 AM12/30/99
to
By the way, this only works for windowed components and not non-graphical
components. I can use the windowed ones but I just didn't want the overhead.

Rudy Velthuis

unread,
Dec 30, 1999, 3:00:00 AM12/30/99
to
On Thu, 30 Dec 1999 10:30:39 -0500, Karim Amin wrote...

>Wow, every time I say something bad about Delphi, I find 5 things better to
>say :). Is there one that resizes components?

If you mean the components should resize as if they were anchored to some
of the sides of the form, you should use the Anchors property.
--
Rudy Velthuis

Karim Amin

unread,
Dec 30, 1999, 3:00:00 AM12/30/99
to
Well what I mean is, I want to be able to move and resize controls on a form at
runtime the same way you would do at design mode. Like when they click on the
control, you get the dots around it and they can use that to resize the
controls. I tried to move method but it doesn't work for controls derived from
TGraphicControl so it doesn't work with labels, shapes, etc... Those are what I
want to use though.

Rudy Velthuis

unread,
Dec 30, 1999, 3:00:00 AM12/30/99
to
On Thu, 30 Dec 1999 12:51:18 -0500, Karim Amin wrote...

>Well what I mean is, I want to be able to move and resize controls on a form at
>runtime the same way you would do at design mode. Like when they click on the
>control, you get the dots around it and they can use that to resize the
>controls. I tried to move method but it doesn't work for controls derived from
>TGraphicControl so it doesn't work with labels, shapes, etc... Those are what I
>want to use though.

I'd create a new control, that overlays the control you clicked. The new
control would get the focus, and draw the sizing grips, etc. As long as
it were selected, it would handle all mouse clicks, and depending on
where a click or a drag was initiated, it would move or resize the
underlying component.

I've just read something about Dream Company's Designer component. You
can find them at http://www.dream-com.com . That was a tip from Jeff
Overcash. Perhaps they have a trial for download.
--
Rudy Velthuis

Peter Below

unread,
Jan 2, 2000, 3:00:00 AM1/2/00
to
> I am trying to move a TShape component located on a form at runtime. I
> want to do something similar to what Delphi does when you are dragging
> and dropping components. Can someone help me out with any information.
> I assume I need to someone get the position of the mouse cusor in
> relation to the form and not the component. Then when I move, I need to
> move the shape accordingly. What is the best way to do this?

You can both move and size controls by handling the mouse events. Here are
two examples that may get you going. There is also a component named
TStretchHandle around (can be found on Delphi super site, i think) that
allows you to add this capability to any control at runtime.

<quote>
Dynmaically create controls and drag them around:

Here is an example. Create a new project with an empty form, add StdCtls
to the Uses clause (for the TLabel class, you can also add a single label
at design time). Add a handler to the forms OnClick method, then modify
the unit as below. Compile and run, click on the form to create a label,
drag on a label to move it.

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs,
StdCtrls;

type
TForm1 = class(TForm)
procedure FormClick(Sender: TObject);
private
{ Private declarations }
downX, downY: Integer;
dragging: Boolean;
procedure ControlMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure ControlMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure ControlMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}
Type
TCracker = Class(TControl);
{ Needed since TControl.MouseCapture is protected }

procedure TForm1.FormClick(Sender: TObject);
var
pt: TPoint;
begin
// get cursor position, convert to client coordinates
GetCursorPos( pt );
pt := ScreenToClient( pt );
// create label with top left corner at mouse position
With TLabel.Create( Self ) Do Begin
SetBounds( pt.x, pt.y, width, height );
Caption := Format('Hit at %d,%d',[pt.x, pt.y]);
Color := clBlue;
Font.Color := clWhite;
Autosize := true;
Parent := Self;

// attach the drag handlers
OnMouseDown := ControlMouseDown;
OnMouseUp := ControlMouseUp;
OnMouseMove := ControlMouseMove;
End;
end;

procedure TForm1.ControlMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
downX:= X;
downY:= Y;
dragging := TRue;
With TCracker(Sender) Do Begin
MouseCapture := TRue;
Color := clRed;
End;
end;

procedure TForm1.ControlMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
If dragging Then
with Sender As TControl Do Begin
Left := X-downX+Left;
Top := Y-downY+Top;
End;
end;

procedure TForm1.ControlMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
If dragging then Begin
dragging := False;
With TCracker(Sender) Do Begin
MouseCapture := False;
Color := clBlue;
End;
End;
end;

end.
</quote>

unit SizeablePanel;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs,
ExtCtrls;

type
TSizeablePanel = class(TPanel)
private
FDragging: Boolean;
FLastPos: TPoint;
protected
Procedure Paint; override;
Procedure MouseDown( Button: TMouseButton; Shift: TShiftState;
X, Y: Integer ); override;
Procedure MouseMove( Shift: TShiftState; X, Y: Integer ); override;
Procedure MouseUp( Button: TMouseButton; Shift: TShiftState;
X, Y: Integer ); override;
public
{ Public declarations }
published
{ Published declarations }
end;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents('PBGoodies', [TSizeablePanel]);
end;

procedure TSizeablePanel.MouseDown(Button: TMouseButton; Shift:
TShiftState; X,
Y: Integer);
begin
If (Button = mbLeft) and
((Width - x ) < 10) and
((Height - y ) < 10)
Then Begin
FDragging := TRue;
FLastPos := Point( x, y );
MouseCapture := true;
Screen.cursor := crSizeNWSE;
End
Else
inherited;
end;

procedure TSizeablePanel.MouseMove(Shift: TShiftState; X, Y: Integer);
var
r: TRect;
begin
If FDragging Then Begin
r:= BoundsRect;
SetBounds( r.left, r.top,
r.right - r.left + X - FlastPos.X,
r.bottom - r.top + Y - Flastpos.Y );
FLastPos := Point( x, y );
End
Else Begin
inherited;
If ((Width - x ) < 10) and ((Height - y ) < 10) Then
Cursor := crSizeNWSE
Else
Cursor := crDefault;

End;
end;

procedure TSizeablePanel.MouseUp(Button: TMouseButton; Shift: TShiftState;
X,
Y: Integer);
begin
If FDragging Then Begin
FDragging := False;
MouseCapture := false;
Screen.Cursor := crDefault;
End
Else
inherited;
end;

procedure TSizeablePanel.Paint;
var
x, y: Integer;
begin
inherited;
Canvas.Font.Name := 'Marlett';
Canvas.Font.Size := 10;
Canvas.Brush.Style := bsClear;
x:= clientwidth - canvas.textwidth('o');
y:= clientheight - canvas.textheight('o');
canvas.textout( x, y, 'o' );
end;


end.

Peter Below (TeamB) 10011...@compuserve.com)
No e-mail responses, please, unless explicitly requested!

Sent using Virtual Access 5.00 - download your freeware copy now
http://www.atlantic-coast.com/downloads/vasetup.exe

Nick Hodges (TeamB)

unread,
Jan 3, 2000, 3:00:00 AM1/3/00
to
Karim --

Look for the TControlSizer component at either Torrys
(http://www.torry.ru) or the http://www.delphipages.com. I think that
is where I got it. It is quite slick and works great. Nice piece of
code.


Nick Hodges - TeamB
Xapware Technologies -- http://www.xapware.com

0 new messages