在下列標記範例中,示範了包含在 TextBlock

>
  <TextBlock Name="myTextBlock" Margin="4" Text="Hello, world" />
</StackPanel>

在下列程式碼範例中,示範了如何使用 TransformToVisual 方法,擷取 GeneralTransform 值中。

// Return the general transform for the specified visual object.
GeneralTransform generalTransform1 = myStackPanel.TransformToVisual(myTextBlock);

// Retrieve the point value relative to the child.
Point currentPoint = generalTransform1.Transform(new Point(0, 0));

位移會將所有物件的 Y 也為 -4。位移值是負值,因為父物件相對於其子物件的位移是負的。

 

若取myStackPanel相对于 应用程序左上角的  绝对位置,如下:

// Return the general transform for the specified visual object.
GeneralTransform generalTransform1 = myStackPanel.TransformToVisual(null);

// Retrieve the point value relative to the child.
Point currentPoint = generalTransform1.Transform(new Point(0, 0));

相关文章: