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>...
>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
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
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
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
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