【问题标题】:WPF client to screen point transformationWPF客户端到屏幕点的转换
【发布时间】:2013-08-08 20:07:42
【问题描述】:

我正在寻找一种方法来转换与屏幕上的视觉点相关的给定点。 我找到了这个解决方案:

http://social.msdn.microsoft.com/Forums/vstudio/en-US/b327c0bc-d27e-44fe-b833-c7db3400d632/how-to-get-control-location-in-screen-coordinate-system

我无法理解pointRoot 和pointClient 之间的区别,因为它们似乎一直都是相等的:

// [...]
// Translate the point from the visual to the root.
GeneralTransform transformToRoot = relativeTo.TransformToAncestor(root);
Point pointRoot = transformToRoot.Transform(point);

// Transform the point from the root to client coordinates.
Matrix m = Matrix.Identity;
Transform transform = VisualTreeHelper.GetTransform(root);
if (transform != null)
    m = Matrix.Multiply(m, transform.Value);

Vector offset = VisualTreeHelper.GetOffset(root);
m.Translate(offset.X, offset.Y);
Point pointClient = m.Transform(pointRoot);
// [...]

(完整代码点击链接)

看来VisualTreeHelper.GetOffset(root) 试图获取窗口的变换...

【问题讨论】:

    标签: c# wpf transform transformation


    【解决方案1】:

    假设您的Visual 来自Button 控件...您在寻找这样的东西吗?:

    Point locationFromWindow = button1.TranslatePoint(new Point(0, 0), this);
    
    Point locationFromScreen = button1.PointToScreen(locationFromWindow);
    

    注意:这些都是Visual类的方法,所以你也可以直接从Visual调用它们。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多