在GridPanel中拖动N个Drop控件 [英] Drag N Drop controls in a GridPanel

查看:81
本文介绍了在GridPanel中拖动N个Drop控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在delphi 2010的网格面板中,我一直在拖拉控件。在面板/按钮/内容从一个单元格移动到另一个单元格时,都将其移动。替换现有或交换位置。我还没弄清楚我怎么知道哪个单元格被放下了,因为它们与列索引以及行索引一起工作。

I have been fooling around with dragging and dropping controls in a grid panel in delphi 2010. Move a panel/button/whateever the contents are from one cell to another cell. Replacing existing or swapping places. I have not figured out how I know which cell was dropped on because they work with column indexes and also row indexes.

因此,如果我有一个包含3列和3行,我在单元格1/1中有一个按钮...,然后将该按钮从1/1拖动到3/3中,如何从拖放事件中获得该单元格的位置?我得到了x,y坐标,但是如何确定该单元格呢?

so if I have a gridpanel which has 3 columns and 3 rows, and I have a button in cell 1/1... and I drag that button from 1/1 into 3/3 how can I get that cell location from the dragdrop event? I get the x,y coords on the drop but how can I determine the cell from that?

推荐答案

您可以使用 TGridPanel.CellRect 来获取每个单元格的边界矩形。以下是如何使用 CellRect 的示例:

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;

这篇关于在GridPanel中拖动N个Drop控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆