您可以使用一点欺骗来获取当前的单元格坐标。:)
允许组件的后代访问祖先类的受保护字段。CellRect
因为除了访问受保护的方法之外我们不需要做任何事情TDBGrid
,我们将创建一个插入器(不做任何事的后代),它只允许我们访问该受保护的方法。然后我们可以将其类型转换TDBGrid
为新的后代类并使用它来访问受保护的方法。我使用THack
作为前缀来命名后代,以明确后代的唯一目的是获得对祖先类的访问(“hack”)。
// implementation
type
THackDBGrid=class(TDBGrid);
// Where you need the coordinates
var
CurrRow: Integer;
Rect: TRect;
begin
CurrRow := THackDBGrid(DBGrid1).Row;
Rect := THackDBGrid(DBGrid1).CellRect(ColIndexYouWant, CurrRow);
// Rect now contains the screen coordinates you need, or an empty
// rectangle if there is no cell at the col and row specified.
end;
正如 OP 在评论中指出的那样,在delphi.about.com上有更详细的描述它是如何工作的。