找出 TGridPanel 中控件的位置

发布于 2024-08-03 22:46:16 字数 104 浏览 7 评论 0原文

如何找出 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

左耳近心 2024-08-10 22:46:16

不幸的是,由于 TGridPanel 的魔力,它比仅仅获取 Top 和 Left 属性要复杂一些...

这应该适用于任何 Control,请根据您的需要进行调整:

procedure GetRowColumn(const AControl: TControl; var ARow, AColumn: Integer);
var
  I: Integer;
begin
  if AControl.Parent is TGridPanel then
  begin
    I := TGridPanel(AControl.Parent).ControlCollection.IndexOf(AControl);
    if I > -1 then
    begin
      ARow := TGridPanel(AControl.Parent).ControlCollection[I].Row;
      AColumn := TGridPanel(AControl.Parent).ControlCollection[I].Column;
    end;
  end;
end;

procedure TForm1.ButtonClick(Sender: TObject);
var
  Row, Column : Integer;
begin
  GetRowColumn(Sender as TControl, Row, Column);
  // do something with Row and Column
  ShowMessage( Format('row=%d - col=%d',[Row, Column]));
end;

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:

procedure GetRowColumn(const AControl: TControl; var ARow, AColumn: Integer);
var
  I: Integer;
begin
  if AControl.Parent is TGridPanel then
  begin
    I := TGridPanel(AControl.Parent).ControlCollection.IndexOf(AControl);
    if I > -1 then
    begin
      ARow := TGridPanel(AControl.Parent).ControlCollection[I].Row;
      AColumn := TGridPanel(AControl.Parent).ControlCollection[I].Column;
    end;
  end;
end;

procedure TForm1.ButtonClick(Sender: TObject);
var
  Row, Column : Integer;
begin
  GetRowColumn(Sender as TControl, Row, Column);
  // do something with Row and Column
  ShowMessage( Format('row=%d - col=%d',[Row, Column]));
end;
你与清晨阳光 2024-08-10 22:46:16

您可以使用 Sender 转换为 tButton,然后询问它的顶部和左侧,例如:

Procedure TForm1.OnClick(Sender:tObject);
var
  X,Y : Integer;
begin
  if Sender is TButton then
    begin
      X := TButton(Sender).Top;
      Y := TButton(Sender).Left;
      // do something with X & Y
    end;
end;

或者如果您只是想知道按下了哪个按钮,您还可以使用 TAG 属性在每个按钮中插入一个数字,然后检索onclick 事件中的标记值。请记住首先将 Tag 属性设置为某个值。如果您只是将按钮放入网格面板中或在用于创建和插入按钮的例程中,则可以在表单设计器中执行此操作。

Procedure TForm1.OnClick(Sender:tObject);
var
  iButton : integer;
begin
  if Sender is TComponent then
    begin
      iButton := TComponent(Sender).Tag;
      // do something with iButton
    end;
end;

您还可以使用标记属性来存储不仅仅是整数,因为指针当前使用与整数相同的内存大小,您可以将指针转换为整数并将该值插入到标记属性中。请注意,放置在此字段中的任何指针仍被视为整数。您对它指向的内存负责,它不会由组件管理。

You can use Sender cast as a tButton and then ask it for its top and left for example:

Procedure TForm1.OnClick(Sender:tObject);
var
  X,Y : Integer;
begin
  if Sender is TButton then
    begin
      X := TButton(Sender).Top;
      Y := TButton(Sender).Left;
      // do something with X & Y
    end;
end;

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.

Procedure TForm1.OnClick(Sender:tObject);
var
  iButton : integer;
begin
  if Sender is TComponent then
    begin
      iButton := TComponent(Sender).Tag;
      // do something with iButton
    end;
end;

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文