Arrastrar y soltar controles en una gridpanel
-
27-10-2019 - |
Pregunta
He estado jugando con controles de arrastre y soltamiento en un panel de cuadrícula en Delphi 2010. Mueva un panel/botón/a quien el contenido sea de una celda a otra. Reemplazo de lugares existentes o de intercambio. No he descubierto cómo sé en qué celda se dejó caer porque funcionan con índices de columna y también índices de fila.
Entonces, si tengo un GridPanel que tiene 3 columnas y 3 filas, y tengo un botón en la celda 1/1 ... y arrasto ese botón de 1/1 a 3/3 ¿Cómo puedo obtener esa ubicación de la celda desde el dragdrop ¿evento? Tengo las coordinas X, Y en la gota, pero ¿cómo puedo determinar la celda a partir de eso?
Solución
Puedes usar TGridPanel.CellRect
Para obtener el rectángulo delimitador para cada una de las celdas. Aquí hay un ejemplo de cómo usar CellRect
:
// GP: TGridPanel
// This is the "OnDragDrop" handler.
procedure TForm13.GPDragDrop(Sender, Source: TObject; X, Y: Integer);
var DropPoint: TPoint;
CellRect: TRect;
i_col, i_row: Integer;
begin
if Source = Panel1 then // Simple test, is this a drop I want to handle?
begin
DropPoint := Point(X, Y); // Where did the suer drop? We need this so we can easily call PtInRect
for i_col := 0 to GP.ColumnCollection.Count-1 do
for i_row := 0 to GP.RowCollection.Count-1 do
begin
CellRect := GP.CellRect[i_col, i_row]; // Get the bounding rect for Col[i_col, i_row]
if PtInRect(CellRect, DropPoint) then
begin
// Panel1 was dropped over Cell[i_col, i_row]
end;
end;
end;
end;
Otros consejos
Basado en la respuesta de Cosmin (que es un buen punto de partida pero no funciona en la vida real).
Mi código está en C ++, pero dado que es un "clon" de la respuesta de Consmin, los usuarios de Delphi pueden entenderlo fácilmente (y ver lo que se cambió).
PD: Tenga en cuenta que arrasto los tpanels en lugar de tbutton (un cambio muy menor).
void __fastcall TfrmVCL::ButtonDragDrop(TObject *Sender, TObject *Source, int X, int Y)
{
TRect CurCellRect;
TRect DestCellRect;
int Col;
int Row;
int srcCol; int srcRow;
int destCol; int destRow;
int srcIndex; int destIndex;
TPanel *SrcBtn;
TPanel *DestBtn;
SrcBtn = dynamic_cast<TPanel *>(Source);
if (SrcBtn)
{
int ColCount = GridPnl->ColumnCollection->Count ;
int RowCount = GridPnl->RowCollection->Count ;
// SOURCE
srcIndex = GridPnl->ControlCollection->IndexOf( SrcBtn );
srcCol = GridPnl->ControlCollection->Items[ srcIndex ]->Column; // the column for the dragged button
srcRow = GridPnl->ControlCollection->Items[ srcIndex ]->Row;
// DESTINATION
// we get coordinates of the button I drag onto
DestBtn= dynamic_cast<TPanel *>(Sender);
if (!DestBtn) return;
destIndex = GridPnl->ControlCollection->IndexOf( DestBtn );
destCol = GridPnl->ControlCollection->Items[ destIndex ]->Column; // the column for the dragged button
destRow = GridPnl->ControlCollection->Items[ destIndex ]->Row;
DestCellRect = GridPnl->CellRect[ destCol ][ destRow ];
// Check all cells
for ( Col = 0 ; Col < ColCount ; Col++ )
{
for ( Row = 0 ; Row < RowCount ; Row++ )
{
// Get the bounding rect for this cell
CurCellRect = GridPnl->CellRect[ Col ][ Row ];
if (IntersectRect_ForReal(DestCellRect, CurCellRect))
{
GridPnl->ControlCollection->Items[srcIndex]->SetLocation(Col, Row, false);
return;
}
else
lblCurCellRect->Caption= "NO HIT";
}
}
}
}