Delphi中如何找到带有滚动条的网格组件的实际宽度
我有一个网格组件(DBGrid),上面有很多列。由于列数较多,创建了滚动条,因此网格的某些部分仍然隐藏。我需要找出DBGrid的真实宽度是多少,包括由于滚动条而未显示的部分。但 Width 属性仅给出组件本身的宽度。有人有什么想法吗?
I have a grid component (DBGrid) which has lots of columns on it. Because of large number of columns, a scrollbar was created, and thus some part of grid remains hidden. I need to find out what is the real width of DBGrid, including the part which is not shown due to scroll bar. But Width property gives only the width of the component itself. Anybody has any idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
TDBGrid
有一个Columns
属性。每列都有自己的Width
属性。因此,您可以循环遍历所有列并总结它们的宽度。像这样:
TDBGrid
has aColumns
property. Each of the columns has its ownWidth
property. So you could loop through all of the columns and sum up their widths.Like this:
也许这会有所帮助。它是 TDBGrid 类帮助程序的一部分,可自动调整最后一列的大小,以便网格没有空白空间。应该很容易根据您的需求进行调整。
您可能会注意到,CalcDrawInfo 方法就是您正在寻找的方法。由于它是受保护的,因此您可以使用类助手或通常的受保护黑客来获取它。
Perhaps this may be helpful. It is part of a class helper for TDBGrid that auto sizes the last column, so that the grid has no empty space. Should be easy to adjust to your needs.
As you may notice, the CalcDrawInfo method is what you are seeking for. As it is protected you can either use a class helper or the usual protected-hack to get hands on it.
我想我已经找到了解决方案(虽然看起来有点奇怪)。为了找到 DBgrid 的列宽和实际宽度之间的差异(这意味着找到最后一列之后留下的空白空间的宽度),我们需要跟踪现在显示在左侧的列(当前列是什么)滚动到的位置)。我们可以使用 OnDrawColumnCell 事件来做到这一点,因为它只会绘制现在滚动的列。然后我们需要计算所有可见列的宽度总和,并从 DBGrid 的宽度中减去该宽度。 PS 抱歉英语不好
Ex 代码:
I think I have found a solution (although it seems a little strange). In order to find the difference between column widths and real width of the DBgrid (that means find the width of the empty space left after last column), we need to keep track of which column is shown on the left now (what is current column that is scrolled to). We can do that using OnDrawColumnCell event, since it will draw only columns which are scrolled on now. Then we need to calculate sum of widths of all visible columns, and subtract that from DBGrid's width. P.S. Sorry for bad english
Ex code:
以下是我们过去使用过的函数。它考虑了基于字体的数据宽度,并且还补偿了垂直线(如果它们可见)
Here are functions we have used in the past. It takes into account the width of data based on the font and also compensates for vertical lines if they are visible