How do I get screen coordinates of the DBGrid cell

拥有回忆 提交于 2019-12-01 06:08:21

You can get the current cell coordinates, using a little deception. :)

Descendants of a component are allowed to access the protected fields of the ancestor class. Since we don't need to do anything except gain access to the protected CellRect method of TDBGrid, we'll create an interposer (do-nothing descendant) that simply allows us access to that protected method. We can then typecast the TDBGrid to that new descendant class and use it to reach the protected method. I name the descendant using THack as a prefix to make it clear that the only purpose of the descendant is to gain access ("hack") the ancestor class.

// 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;

As the OP has indicated in a comment, there's a more detailed description of how this works at delphi.about.com.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!