【问题标题】:How to get screen position of certain component如何获取某个组件的屏幕位置
【发布时间】:2013-06-03 22:29:01
【问题描述】:

问题

好吧,我设计了一个对话框,当用户在 TStringGrid 组件上选择某些单元格(单选,而不是多选)时调用它。

此对话框将位于其中一个选定单元格的中心。

但它没有发生:(

可能的解决方案 = 我想要的

我想获取一个单元格的屏幕位置,即屏幕绝对坐标,而不是CellRect()获得的那些。

我在做什么

为了计算单元格的中心,我目前必须以这种方式对以下组件的坐标求和:

TRect pos;

pos = table->CellRect(Col,Row);

pos.Left += form->Left + panel->Left + frame->Left + table->Left;
pos.Right += pos->Left;

pos.Top += form->Top + panel->Top + frame->Top + table->Top;
pos.Bottom += pos->Top;

然后将对话框居中:

dialog->Left = (pos->Left + pos->Right)/2 - dialog->Width/2;
dialog->Top = (pos->Top + pos->Bottom)/2 - dialog->Height/2;

由于某些未知原因,Col 和 Row 为对话框的正确位置添加了偏移量,因此较大的 Col 和 Row 值将对话框位置设置为距离正确位置(所选单元格的中心)很远。

 ___screen________________________________________
|                                                 |
|   ___form___________________________________    |
|  |                                          |   |
|  |                                          |   |
|  |   ___panel____________________________   |   |
|  |  |                                    |  |   |
|  |  |   ___frame_______________          |  |   |
|  |  |  |                       |         |  |   |
|  |  |  |                       |         |  |   |
|  |  |  |  ___table_________    |         |  |   |
|  |  |  | |                 |   |         |  |   |
|  |  |  | |       _cell_    |   |         |  |   |
|  |  |  | |      |______|   |   |         |  |   |
|  |  |  | |                 |   |         |  |   |
|  |  |  | |_________________|   |         |  |   |
|  |  |  |_______________________|         |  |   |
|  |  |____________________________________|  |   |
|  |                                          |   |
|  |                                          |   |
|  |__________________________________________|   |
|_________________________________________________|

如果我有表格或所选单元格的屏幕位置

实现和检测这些偏移误差将变得如此容易,因为上面总和上的分量坐标将更少......

【问题讨论】:

    标签: screen c++builder


    【解决方案1】:

    调用CellRect() 获取客户端坐标,然后将其转换为屏幕坐标。有几种方法可以做到这一点:

    1. 使用TControl::ClientToScreen() 方法:

      TRect pos = table->CellRect(Col, Row);
      
      TPoint &tl = pos.TopLeft();
      tl = table->ClientToScreen(tl);
      
      TPoint &br = pos.BottomRight();
      br = table->ClientToScreen(br);
      
    2. 使用TControl::ClientOrigin 属性偏移TRect,该属性指定StringGrid 客户区左上角的屏幕坐标

      TPoint pt = table->ClientOrigin;
      TRect pos = table->CellRect(Col, Row);
      ::OffsetRect(&pos, pt.x, pt.y);
      
    3. 使用 Win32 API MapWindowPoints() 函数(记住TStringGrid 是一个图形控件,所以它没有自己的窗口,你必须使用它的父窗口来代替),例如:

      TRect pos = table->CellRect(Col, Row);
      ::OffsetRect(&pos, table->Left, table->Top);
      ::MapWindowPoints(table->Parent->Handle, NULL, (LPPOINT)&pos, 2);
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-07
      • 1970-01-01
      • 2019-11-14
      • 1970-01-01
      • 2011-06-27
      • 2023-03-28
      • 1970-01-01
      相关资源
      最近更新 更多