"Chad Eddy" <
XXXX@XXXXX.COM >wrote:
Quote
[...] Just getting started with drag and drop so not sure
why this works [...]
If you just want to drag the object around it's Parent,
you don't actually want to Drag-N-Drop. You just want to drag:
TPoint mPoint; // make global
void __fastcall TForm1::ObjectMouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y)
{
TControl* C = dynamic_cast<TControl *>( Sender );
if( C )
{
mPoint = TPoint(X, Y);
C->Tag = true;
}
}
//-------------------------------------------------------------
void __fastcall TForm1::ObjectMouseMove(TObject *Sender, TShiftState Shift, int X, int Y)
{
static bool Positioning = false;
TControl* C = dynamic_cast<TControl *>( Sender );
if( C && C->Tag && !Positioning )
{
Positioning = true;
if( X < mPoint.x ) C->Left = C->Left - ( mPoint.x - X );
else C->Left = C->Left + ( X - mPoint.x );
if( Y < mPoint.y ) C->Top = C->Top - ( mPoint.y - Y );
else C->Top = C->Top + ( Y - mPoint.y );
Positioning = false;
}
}
//-------------------------------------------------------------
void __fastcall TForm1::ObjectMouseUp(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y)
{
TControl* C = dynamic_cast<TControl *>( Sender );
if( C && C->Tag )
{
//If the Parent is not the form, you can cast the
//parent to get a different height and width.
//Note that if the form is the parent, the 'else'
//checks have no effect because the form will resize
//unless it's Style is bsSingle
if( C->Left < 0 ) C->Left = 0;
else if( (C->Left + C->Width)>Width ) C->Left = Width - C->Width;
if( C->Top < 0 ) C->Top = 0;
else if( (C->Top + C->Height)>Height ) C->Top = Height - C->Height;
C->Tag = false;
}
}
//-------------------------------------------------------------
~ JD