WPF 客户端到屏幕点转换 [英] WPF client to screen point transformation
问题描述
我正在寻找一种方法来将与视觉相关的给定点转换为屏幕上的点.我找到了这个解决方案:
I'm looking for a way to transofrm given points that are relative to a Visual to Points on the screen. I found this solution:
我无法理解 pointRoot
和 pointClient
之间的不同,因为它们似乎一直都相等:
I can't understand the different beween the pointRoot
and the pointClient
as they seem to be equal all the time:
// [...]
// 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);
// [...]
(点击链接查看完整代码)
(for the full code click on the link)
似乎 VisualTreeHelper.GetOffset(root)
试图获取窗口的变换...
It seems that the VisualTreeHelper.GetOffset(root)
tries to get the transform of the window...
推荐答案
假设您的 Visual
来自 Button
控件...您是否正在寻找这样的东西?:
Assuming that your Visual
comes from a Button
control... are you looking for something like this?:
Point locationFromWindow = button1.TranslatePoint(new Point(0, 0), this);
Point locationFromScreen = button1.PointToScreen(locationFromWindow);
注意:这些都是 Visual
类的方法,所以你也可以直接从你的 Visual
调用它们.
Note: these are both methods of the Visual
class, so you can also call them from your Visual
directly.
这篇关于WPF 客户端到屏幕点转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!