找出 TGridPanel 中控件的位置
如何找出 TGridPanel 内控件的位置(行和列索引)?我想对按钮数量使用常见的 OnClick 事件,并且需要知道按钮的 X、Y 位置。
我正在使用Delphi 2007。
How I can find out the position (row and column index) of controls inside TGridPanel? I'd like to use common OnClick event for number of buttons and need to know the X,Y position of the button.
I'm using Delphi 2007.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不幸的是,由于 TGridPanel 的魔力,它比仅仅获取 Top 和 Left 属性要复杂一些...
这应该适用于任何 Control,请根据您的需要进行调整:
Unfortunately, because of the magic of TGridPanel, it is a little more complicated than just getting the Top and Left properties...
This should do it for any Control, adapt it to your needs:
您可以使用 Sender 转换为 tButton,然后询问它的顶部和左侧,例如:
或者如果您只是想知道按下了哪个按钮,您还可以使用 TAG 属性在每个按钮中插入一个数字,然后检索onclick 事件中的标记值。请记住首先将 Tag 属性设置为某个值。如果您只是将按钮放入网格面板中或在用于创建和插入按钮的例程中,则可以在表单设计器中执行此操作。
您还可以使用标记属性来存储不仅仅是整数,因为指针当前使用与整数相同的内存大小,您可以将指针转换为整数并将该值插入到标记属性中。请注意,放置在此字段中的任何指针仍被视为整数。您对它指向的内存负责,它不会由组件管理。
You can use Sender cast as a tButton and then ask it for its top and left for example:
Or if your just wanting to know what button was pressed, you can also use the TAG property to insert a number into each button, and then retrieve the tag value in your onclick event. Just remember to first set the Tag property to something. You can do this in the form designer if your just dropping buttons into the grid panel or in the routine your using to create and insert your buttons.
You can also use the tag property to store more than just an integer, since a pointer currently uses the same memory size as the integer you can cast a pointer to an integer and insert that value into the tag property. Just be aware that any pointer you place in this field is still treated as an integer. You are responsible for the memory it points to, it will not be managed by the component.