2011-03-19 33 views
0

控制I已經在Delphi在網格面板拖放控制2010移動面板/按鈕/ whateever的內容是從一個小區到另一個小區鬼混。替換現有或交換位置。我還沒有弄清楚我是如何知道哪個單元被丟棄的,因爲它們與列索引以及行索引一起工作。將n個墨滴在GridPanel中

,所以如果我有有3列和3行的GridPanel中,我在小區1/1按鈕......我拖動按鈕,從1/1到3/3我怎樣讓這個細胞來自dragdrop事件的位置?我得到了X,Y座標,但我怎樣才能確定這個單元呢?

回答

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;