在网格面板中拖放控件

时间:2011-03-19 03:56:45

标签: delphi drag-and-drop gridpanel

在delphi 2010的网格面板中,我一直在拖拽控件。无论内容是从一个单元格移动到另一个单元格,都要移动面板/按钮/按钮。替换现有或交换位置。我还没弄清楚我是如何知道哪个单元被删除的,因为它们使用列索引和行索引。

所以,如果我有一个有3列3行的网格面板,并且我在单元格1/1中有一个按钮...然后我将该按钮从1/1拖到3/3我怎样才能获得该单元格位置从拖拽事件?我得到了下降的x,y坐标,但我怎样才能从中确定细胞?

2 个答案:

答案 0 :(得分:3)

您可以使用TGridPanel.CellRect获取每个单元格的边界矩形。以下是如何使用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;

答案 1 :(得分:0)

基于Cosmin的回答(这是一个很好的起点,但在现实生活中不起作用)。

我的代码是C ++,但由于它是Consmin答案的“克隆”,因此Delphi用户可以轻松理解它(并查看更改的内容)。
PS:请注意,我拖动TPanels而不是TButtons(这是非常小的更改)。

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";
           }
        }
     }
}