Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF客户端到屏幕点转换_C#_Wpf_Transform_Transformation - Fatal编程技术网

C# WPF客户端到屏幕点转换

C# WPF客户端到屏幕点转换,c#,wpf,transform,transformation,C#,Wpf,Transform,Transformation,我正在寻找一种方法,将给定的相对于视觉的点转换为屏幕上的点。 我找到了这个解决方案: 我无法理解pointRoot和pointClient之间的区别,因为它们似乎一直是相等的: // [...] // Translate the point from the visual to the root. GeneralTransform transformToRoot = relativeTo.TransformToAncestor(root); Point pointRoot = transfor

我正在寻找一种方法,将给定的相对于视觉的点转换为屏幕上的点。 我找到了这个解决方案:

我无法理解
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);
// [...]
(有关完整代码,请单击链接)


似乎
visualtreeheloper.GetOffset(root)
试图获取窗口的转换…

假设您的
Visual
来自
按钮
控件。。。你在找这样的东西吗

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

Point locationFromScreen = button1.PointToScreen(locationFromWindow);

注意:这两种方法都是
Visual
类的方法,因此您也可以直接从
Visual
调用它们。

假设您的
Visual
来自
按钮
控件。。。你在找这样的东西吗

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

Point locationFromScreen = button1.PointToScreen(locationFromWindow);
注意:这两种方法都是
Visual
类的方法,因此您也可以直接从
Visual
调用它们