问题
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