1. 程式人生 > >獲取 UIElement 相對於屏幕原點所占用的矩形區域

獲取 UIElement 相對於屏幕原點所占用的矩形區域

www. 矩形 gen angle back form ack stack msg

原文:獲取 UIElement 相對於屏幕原點所占用的矩形區域

    <Grid Background="Transparent">
        <StackPanel Margin="120 0 0 0">
            <Grid HorizontalAlignment="Left" VerticalAlignment="Top">
                <Rectangle Name="rectangle1" Width="300" Height="200" Fill="Red" />
            </Grid
> <TextBlock Name="lblMsg" FontSize="14.667" Margin="0 10 0 0" /> </StackPanel> </Grid>
            // 演示如何獲取 UIElement 相對於屏幕原點所占用的矩形區域
            GeneralTransform generalTransform = rectangle1.TransformToVisual(null); // 獲取 rectangle1 相對於屏幕的 GeneralTransform
Point point = generalTransform.TransformPoint(new Point(0, 0)); // rectangle1 的原點(左上角頂點)相對於屏幕 0,0 點的位置 Rect rect = new Rect(point, new Size(rectangle1.ActualWidth, rectangle1.ActualHeight)); lblMsg.Text += "紅色矩形相對於屏幕原點的位置:" + rect.ToString();

獲取 UIElement 相對於屏幕原點所占用的矩形區域