0

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

因此,如果我有一个具有 3 列和 3 行的网格面板,并且我在单元格 1/1 中有一个按钮...并且我将该按钮从 1/1 拖到 3/3,我如何从拖放中获取该单元格位置事件?我得到了下降的 x,y 坐标,但我如何从中确定单元格?

4

2 回答 2

3

You can use TGridPanel.CellRect to get the bounding rectangle for each of the cells. Here's an example of how to use 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;
于 2011-03-19T05:43:17.890 回答
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";
           }
        }
     }
}
于 2018-11-21T13:01:20.117 回答