BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Re: Moving component in a form by mouse

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (VCL Components Usage)
View previous topic :: View next topic  
Author Message
Phil V
Guest





PostPosted: Wed Dec 17, 2003 5:03 pm    Post subject: Re: Moving component in a form by mouse Reply with quote



Hi Mario,

I had a go at something similar, hold on a sec whilst i dig it out.......

Ah, there it is, got it!

Right for simplicity we will work with one form with one TButton.

In the header file you need these two;

bool MouseDown;
int old_x; int old_y;

and then in your cpp file these bits;

//--------------------------------------------------------------------------
-
void __fastcall TForm1::ButtonMouseDown(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
MouseDown = true;
old_x = X; old_y = Y;
}
//--------------------------------------------------------------------------
-
void __fastcall TForm1::ButtonMouseUp(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
MouseDown = false;
}
//--------------------------------------------------------------------------
-
void __fastcall TForm1::ButtonMouseMove(TObject *Sender,
TShiftState Shift, int X, int Y)
{
if (MouseDown)
{
if (TButton* btn = dynamic_cast<TButton*>(Sender))
{
// If statements are just checking that the button won't move off of
the screen.

if ( (btn->Top + Y - old_y + 1 > 0) &&
(btn->Top + Y - old_y - 1 < Form1->ClientHeight-(btn->Height))
)
{
btn->Top += Y - old_y; // These bit actually cause the button to
move
}
if ( (btn->Left + X - old_x + 1 > 0) &&
(btn->Left + X - old_x - 1 < Form1->ClientWidth-(btn->Width))
)
{
btn->Left += X - old_x; // These bit actually cause the button to
move
}
}
}
}

et voila !

Hope that works for ya! It will actually work with any number of TButtons,
just set each of them to use the same MouseDown, MouseUp, and MouseMove
events :-)

Remember that unless you use some method to record where each of the buttons
was moved to when you close down your app. Then when you re-open it the
buttons will be back where they were placed at desgin-time.

Phil V

"Mario Sernicola" <mariosernicola.nospam (AT) libero (DOT) nospam.it> wrote

Quote:
Hi,

I'm developing an app in which I need to set the position of some TButton
in
a window, moving them by mouse at runtime (like in the Builder IDE).

How can I do?

I've tried with dock property

TForm1->DockSite = true;

and

TButton1->DragKind = dkDock;
TButton1->DragMode = dmAutomatic;

but I don't want any floating windows in my app!

There's a way to know instantly for instantly the position (x,y
coordinates)
of a component (a TButton in this question) while I move it by the mouse
to
set his position in the form?

Thanks

Mario Sernicola






Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (VCL Components Usage) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.