获取 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();

 

posted on 2013-03-28 12:49  JackSlaterYu  阅读(260)  评论(0编辑  收藏  举报