【问题标题】:PointToScreen not returning screen coordinatesPointToScreen 不返回屏幕坐标
【发布时间】:2019-10-11 07:26:17
【问题描述】:

我正在使用 EyeShot 12。我正在使用 EyeShot Line 实体创建一个矩形,它沿长度和宽度有 2 个维度。

我的功能包括使用 action->SelectByPick 更改 Dimension Text,然后选择任何一个维度并通过调出 TextBox 来更改其值,以便用户可以加值。这里TextBox在鼠标指针的位置上弹出。

更进一步,我点击 Tab(键盘按钮)切换到下一个维度,并确保突出显示特定维度。但我担心的是我无法在突出显示的维度旁边找到 TextBox。

我能够在 Eyeshot 坐标 中定位现有 Line 的位置(对应于所选尺寸),但 TextBox 需要 屏幕坐标 值用于准确定位。

所以我使用 control.PointToScreen 将视野坐标转换为屏幕,但它返回的点与视野坐标相同。

代码:

foreach (Entity ent in model1.Entities)      
{
    if (ent.Selected)
    {
        Line lin = (Line)ent;

        Point3D midpt = lin.MidPoint;

        string newpt1X = midpt.X.ToString();
        string newpt1Y = midpt.Y.ToString();

        System.Drawing.Point startPtX = model1.PointToScreen(new 
        System.Drawing.Point(int.Parse(newpt1X) + 20, int.Parse(newpt1Y) + 20));

        TextBox tb = new TextBox();
        tb.Text = "some text";
        tb.Width = 50;
        tb.Location = startPtX;
        model1.Controls.Add(tb);
    }

我查找了其他结果,但每个人都触发到 PointToScreen 以获得此转换。

希望有人能指出我在做什么。

提前致谢

苏拉杰

【问题讨论】:

  • model1.Controls.Add(tb); 使文本框成为model1 的子控件,这意味着tb.Location 属性需要在model1 的客户端坐标中,而不是在屏幕坐标中。
  • 顺便说一句,你为什么使用int.Parse(midpt.X.ToString())这样的奇怪转换?你不能直接转换成整数(int)midpt.X吗?
  • @SergeyShevchenko 谢谢。我做了更改 System.Drawing.Point clientLocation = model1.PointToClient(new System.Drawing.Point((int)midpt.X, (int)midpt.Y)); tb.Location = clientLocation; 但坐标仍然出现在屏幕的左上角,而 Lines 的视线坐标为 (0,0) -> (50,0) 。转换后我得到相同的坐标值。
  • 要在model1 中放置一个文本框,您需要将midpt 坐标转换为model1 客户端坐标。为此,您需要知道 midpt 当前拥有的坐标(是屏幕、客户端还是任何自定义坐标)。顺便说一句,model1 变量的类型是什么?
  • midpt 有客户端坐标,model1 是 Model Form1 的类型。

标签: c# winforms eyeshot


【解决方案1】:

您使您的对象 (TextBox) 成为 ViewportLayout 的子对象,因此您需要相对于它的点。但是控件不在世界坐标中,而是基于其父级的屏幕坐标。

您真正需要的是两 (2) 次转换。

// first grab the entity point you want
// this is a world point in 3D. I used your line entity
// of your loop here
var entityPoint = ((Line)ent).MidPoint;

// now use your Viewport to transform the world point to a screen point
// this screen point is actually a point on your real physical monitor(s)
// so it is very generic, it need further conversion to be local to the control
var screenPoint = model1.WorldToScreen(entityPoint);

// now create a window 2d point
var window2Dpoint = new System.Drawing.Point(screenPoint.X, screenPoint.Y);

// now the point is on the complete screen but you want to know
// relative to your viewport where that is window-wise
var pointLocalToViewport = model1.PointToClient(window2Dpoint);

// now you can setup the textbox position with this point as it's local
// in X, Y relative to the model control.
tb.Left = pointLocalToViewport.X;
tb.Top = pointLocalToViewport.Y;

// then you can add the textbox to the model1.Controls

【讨论】:

  • 感谢这个有据可查的答案。 @sergey 也给了我同样的建议。
猜你喜欢
  • 1970-01-01
  • 2020-07-16
  • 2023-03-29
  • 2018-01-29
  • 1970-01-01
  • 1970-01-01
  • 2017-12-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多