原理:WPF的界面元素是由Visual元素构成的。在可视元素树Visual中,获取某个元素相对于它的父级元素(Ancestor)的坐标,可以使用TransformToAncestor与Transform方法。
指定中心点,获取相对坐标
例子一:确定TextBlock相对于窗体的位置
// Return the general transform for the specified visual object.
GeneralTransform generalTransform1 = myTextBlock.TransformToAncestor(this);
// Retrieve the point value relative to the parent.
Point currentPoint = generalTransform1.Transform(new Point(0, 0));//窗体位置为(0,0)
例子二:获取Button的中心点,相对于Canvas的位置
private void MyButton_Click(object sender, RoutedEventArgs e)
{
Point current = new Point();
current= this.myButton.TransformToAncestor(this.myCanvas)
.Transform(new Point(myButton.Width/2, myButton.Height/2));//获取中心点的位置
MessageBox.Show(current.X.ToString() + " "+ current.Y.ToString ());
}
获取相对于屏幕的坐标
Point controlPoint = new Point(0, 0);
controlPoint = ((TextBox)sender).PointToScreen(controlPoint);//获取控件相对于屏幕的位置
mkeyBoard.Top = controlPoint.Y + ((TextBox)sender).ActualHeight;
mkeyBoard.Left = controlPoint.X-20;
获得子元素相对于父元素位置和宽高
后台C#
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
Rect itemRect = VisualTreeHelper.GetDescendantBounds(rct);//itemRect是0,0,100,80
Rect itemBounds = rct.TransformToAncestor(cv).TransformBounds(itemRect);// itemBounds是309,181,100,80
Console.WriteLine(itemRect);
Console.WriteLine(itemBounds);
}