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

How to Move a TShape component at runtime

194 views
Skip to first unread message

James

unread,
Feb 6, 2003, 8:23:15 PM2/6/03
to
Can somebody please help me, and look at the below code to see what i'm
doing wrong? I'm trying to move a TShape component at runtime. I want the
TShape component to be centered at the current x,y positions of the mouse.
The below code compiles but doesn't do it.
Shape1 and Shape2 are set to stCircle and their height and width are 60.
Please help


unit Unit1;

interface

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

type
TForm1 = class(TForm)
Shape1: TShape;
Shape2: TShape;
StatusBar1: TStatusBar;
Shape3: TShape;
procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure FormCreate(Sender: TObject);
procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
private
{ Private declarations }
newX, newY, oldX, oldY: Integer;
startDrag, EndDrag: Boolean;
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if (Sender is TShape) then
begin
newX := (Sender AS TShape).Width div 2 + (Sender AS TShape).Left;
newY := (Sender AS TShape).Height div 2 + (Sender AS TShape).Top;
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
with Shape1 do
begin
OnMouseDown := Form1.OnMouseDown;
OnMouseMove := Form1.OnMouseMove;
OnMouseUp := Form1.OnMouseUp;
end;

with Shape2 do
begin
OnMouseDown := Form1.OnMouseDown;
OnMouseMove := Form1.OnMouseMove;
OnMouseUp := Form1.OnMouseUp;
end;
end;

procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
StatusBar1.SimpleText := 'X = ' + IntToStr(newX) + ' ' + ' Y = ' +
IntToStr(newY);
if (SSsHIFT in Shift) then
begin
if (Sender is TShape) then
begin
oldX := newX;
oldY := newY;
newX := X;
newY := Y;
(Sender AS TShape).Left := newX + (Sender AS TShape).Width div 2;
(Sender AS TShape).Top := newY + (Sender AS TShape).Height div 2;
end;
end;
end;

end.


Erik Turner

unread,
Feb 6, 2003, 11:07:01 PM2/6/03
to
I think the problem is that in the MouseDown and MouseMove
messages the X,Y coordinates are relative to the TShape.
If you try to move the TShape the new mouse coords don't
change.

Try intercepting the message at the form level (I think you'll
have to intercept WM_MOUSEDOWN and WM_MOUSEMOVE
not use the TForm.OnMouseDown and TForm.OnMouseMove
events.

Erik Turner

"James" <ar...@lineone.net> wrote in message
news:3e43...@newsgroups.borland.com...

Peter Below (TeamB)

unread,
Feb 7, 2003, 8:47:12 AM2/7/03
to
In article <3e43...@newsgroups.borland.com>, James wrote:
> Can somebody please help me, and look at the below code to see what i'm
> doing wrong? I'm trying to move a TShape component at runtime.

You have to keep in mind that the mouse coordinates you get in the OnMouse
handlers are relative to the control (shape in this case). So you have to
record the mouse position (in screen coordinates!) in mouse down and in mouse
move move the control by the difference of the stored mouse position and the
new one. Finally you store the new mouse position to be prepared for the next
mouse move. You use the shapes ClientToScreen method to convert from
shape-relative to screen-relative coordinates.

Search the newsgroup archives for ControlMouseDOwn and my name, that should
turn up an example.


--
Peter Below (TeamB)
Use the newsgroup archives :
http://www.mers.com/searchsite.html
http://www.tamaracka.com/search.htm
http://groups.google.com
http://www.prolix.be


Klaus V.

unread,
Feb 7, 2003, 11:11:27 AM2/7/03
to
hi James,

the errors are in 'TForm1.FormMouseMove'.

Why StatusBar1 isn't updating the contens of simpleText on From1 (also not
with the 'upDate'-method) i don't know, perhaps at first you have to set
some Flags on True for this.

(if you want to have a reaction in 'TForm1.FormMouseMove' you may not forget
to press always the Shift-button because your line: if (SSsHIFT in Shift)
then....... (of course :-))

also you only will see something if you enter a shape with your mouse
because your line: if (Sender is TShape) then...

then the X,Y-coordinates you get from the parameterlist are the coordinates
only INSIDE the shape. (It's most left-top insidepoint is X,Y =0,0). And the
coordinates you get are exactly the one at the moment you entering the shape
from one side. Next you add up to them the half of the Width and Height of
the shape themself. So the shape have to disappear and coming up near at the
left-top-corner oft your form1. Note it all and it will work.

have much fun

KV


- - - - - - - - -

"James" <ar...@lineone.net> schrieb im Newsbeitrag
news:3e43...@newsgroups.borland.com...

0 new messages