Phil V Guest
|
Posted: Wed Dec 17, 2003 5:03 pm Post subject: Re: Moving component in a form by mouse |
|
|
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
|
|
|