WPF client to screen point transformation

时光总嘲笑我的痴心妄想 提交于 2019-12-06 14:00:38

问题


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:

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

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)

It seems that the VisualTreeHelper.GetOffset(root) tries to get the transform of the window...


回答1:


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);

Note: these are both methods of the Visual class, so you can also call them from your Visual directly.



来源:https://stackoverflow.com/questions/18103540/wpf-client-to-screen-point-transformation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!