Al final lo he conseguido con esto:
Código:
void __fastcall TForm1::ControlMouseMove(TObject *Sender, TShiftState Shift,
int X, int Y)
{
TControl *Ctrl = static_cast<TControl*>(Sender);
if (Moving)
{
if((Ctrl->Left + X + Ctrl->Left - TheSpot->x <= 0) &&
(Ctrl->Width + Ctrl->Left + X + Ctrl->Left - TheSpot->x - ScrollBox1->Width >= 0) &&
(Ctrl->Top + Y + Ctrl->Top - TheSpot->y <= 0) &&
(Ctrl->Height + Ctrl->Top + Y + Ctrl->Top - TheSpot->y - ScrollBox1->Height >= 0))
{
Ctrl->Left = X + Ctrl->Left - TheSpot->x;
Ctrl->Top = Y + Ctrl->Top - TheSpot->y;
}
else if((Ctrl->Left + X + Ctrl->Left - TheSpot->x <= 0) &&
(Ctrl->Width + Ctrl->Left + X + Ctrl->Left - TheSpot->x - ScrollBox1->Width >= 0))
{
Ctrl->Left = X + Ctrl->Left - TheSpot->x;
}
else if((Ctrl->Top + Y + Ctrl->Top - TheSpot->y <= 0) &&
(Ctrl->Height + Ctrl->Top + Y + Ctrl->Top - TheSpot->y - ScrollBox1->Height >= 0))
{
Ctrl->Top = Y + Ctrl->Top - TheSpot->y;
}
}
}
Solo veo un problema y es que cuando lo empiezo a arrastrar sin soltar y arrastro mucho empieza como a quedarse congelado el programa y no se actualiza la ventana. No se si es cosa de mi código o es cosa del ordenador que últimamente con las calores no va muy bien. ¿Alguien puede analizarlo y decirme si ve algo raro? Si se puede simplificar mas mejor pero yo no he visto otra manera.