如何获取DBGrid单元格的屏幕坐标 [英] How do I get screen coordinates of the DBGrid cell
问题描述
我想在当前行的特定单元格的右下角显示弹出式按钮或花哨的信息(彩色背景等)。
I want to show popup button or fancy message (with coloured background, etc) just under right-bottom corner of particular cell of the current row.
现在我只想到如何获得网格坐标:
x = DBGrid.DataSource.DataSet.RecNo
y = DBGrid .Columns [index]
For now I only figured how to get grid coordinates:
x = DBGrid.DataSource.DataSet.RecNo
y = DBGrid.Columns[index]
还有TCustomGrid.CellRect,它会做我想要的,但它受到保护,我不想继承和创建另一个组件类
There is also TCustomGrid.CellRect, which would do what I want, but it's protected and I don't want to inherit and create another component class.
我可以想到的一个疯狂的解决方法是将onDrawColumnCell事件中的TRect保存到某个数组。
One crazy workaround I can think of is to save TRect-s in onDrawColumnCell event to some array.
那么你觉得怎么样?
编辑
如何获取第二个单元格的屏幕坐标在当前行?
EDIT
How do I get screen coordinates of, say, second cell in the current row ?
推荐答案
您可以使用一点欺骗获取当前的单元格坐标。 :)
You can get the current cell coordinates, using a little deception. :)
允许组件的后台访问祖先类的受保护字段。因为除了获得 CellRect
方法 TDBGrid
之外,我们不需要做任何事情,我们将创建一个插件(无任何后代),只允许我们访问该受保护的方法。然后,我们可以将 TDBGrid
键入新的后代类,并使用它来达到受保护的方法。我使用 THACK
作为前缀命名后裔,以明确说明后裔的唯一目的是获得(hack)祖先类。
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;
正如OP在评论中指出的那样,在 delphi.about.com 。
As the OP has indicated in a comment, there's a more detailed description of how this works at delphi.about.com.
这篇关于如何获取DBGrid单元格的屏幕坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!