2011-03-19 28 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;