AY 一个wpf画图项目复习下【2/10】

栏目: 编程工具 · 发布时间: 6年前

内容简介:新建空白wpf项目,不会,参考上篇文章。知识点1:获得子元素相对于父元素位置和 宽高后台

新建空白wpf项目,不会,参考上篇文章。

知识点1:获得子元素相对于父元素位置和 宽高

  <Canvas x:Name="cv">
        <Rectangle x:Name="rct" Width="100" Height="80" Fill="#FFD62525" Canvas.Left="309" Canvas.Top="181" />
    </Canvas>

后台

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            Rect itemRect = VisualTreeHelper.GetDescendantBounds(rct);
            Rect itemBounds = rct.TransformToAncestor(cv).TransformBounds(itemRect);
            Console.WriteLine(itemRect);
            Console.WriteLine(itemBounds);
        }

效果:

itemRect是0,0,100,80

AY 一个wpf画图项目复习下【2/10】

我们把canvas换成grid试试

    <Grid x:Name="cv">
        <Rectangle x:Name="rct" Width="100" Height="80" Fill="#FFD62525" Margin="173,260,919,429"  />
    </Grid>

AY 一个wpf画图项目复习下【2/10】

如果子元素在滚动条范围内呢

修改前台

    <ScrollViewer Width="600" Height="400" x:Name="sv">
        <Grid x:Name="cv" Height="800">
            <Rectangle x:Name="rct" Width="100" Height="80" Fill="#FFD62525"  HorizontalAlignment="Left" VerticalAlignment="Top" Margin="300,500,0,0"/>
        </Grid>
    </ScrollViewer>

AY 一个wpf画图项目复习下【2/10】

后台的itemBounds和itemBoundsRelativeScrollViewer是一样的。

我们设置下grid的margin

AY 一个wpf画图项目复习下【2/10】

此时运行效果

AY 一个wpf画图项目复习下【2/10】AY 一个wpf画图项目复习下【2/10】

相对于滚动条的,加上了Grid的Margin的位置

我们理解了怎么求算子元素相对于某个元素的位置,就好办了。

知识点2:矩形区域计算,是否包含。

AY 一个wpf画图项目复习下【2/10】

我们可以通过Rect提供的方法。添加rct2,此时相交的。

AY 一个wpf画图项目复习下【2/10】

 Rect itemRect = VisualTreeHelper.GetDescendantBounds(rct);
            Rect itemBounds = rct.TransformToAncestor(cv).TransformBounds(itemRect);
            //Rect itemBoundsRelativeScrollViewer = rct.TransformToAncestor(sv).TransformBounds(itemRect);
            Console.WriteLine(itemRect);
            Console.WriteLine(itemBounds);
            //Console.WriteLine(itemBoundsRelativeScrollViewer);

            Rect itemRect2 = VisualTreeHelper.GetDescendantBounds(rct2);
            Rect itemBounds2 = rct2.TransformToAncestor(cv).TransformBounds(itemRect2);

            var _isIntersects = itemBounds.IntersectsWith(itemBounds2);
            var _isContains = itemBounds.Contains(itemBounds2);
            Console.WriteLine("_isIntersects="+ _isIntersects+ ",_isContains="+ _isContains);

AY 一个wpf画图项目复习下【2/10】

修改前台

AY 一个wpf画图项目复习下【2/10】

后台不变,输出

AY 一个wpf画图项目复习下【2/10】

所以根据这个,我们能实现 矩形选择框, 命中元素了。 我们把这种选区,英文名单词叫 Rubberband

知识点3,计算自己的中点坐标,然后相对于父元素的坐标。也可以理解相对于父元素的自己的中间坐标

比如一哥 宽11,高7的矩形,中点坐标,是 7,4但是外面如果有个 父元素,那么此时的坐标是多少呢?这就是解决问题的代码,如下

        public static Point GetCenterPoint(this FrameworkElement uiElement, UIElement parent)
        {
           return uiElement.TransformToAncestor(parent).Transform(new Point(uiElement.Width / 2, uiElement.Height / 2));
        }

效果:

我们能求出红色矩形的中心坐标,那么怎么求相对于父容器左上角的坐标呢?

用法如下:

AY 一个wpf画图项目复习下【2/10】

AY 一个wpf画图项目复习下【2/10】

当然这个距离,自己也可以算,矩形的一半+矩形距离左侧的矩形,上面同理,两个和的形成的坐标就是。

====================www.ayjs.net       杨洋    wpfui.com        ayui      ay  aaronyang=======请不要转载谢谢了。=========

推荐您阅读更多有关于“WPF4.5,”的文章


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Beginning ARKit for iPhone and iPad

Beginning ARKit for iPhone and iPad

Wallace Wang / Apress / 2018-11-5 / USD 39.99

Explore how to use ARKit to create iOS apps and learn the basics of augmented reality while diving into ARKit specific topics. This book reveals how augmented reality allows you to view the screen on ......一起来看看 《Beginning ARKit for iPhone and iPad》 这本书的介绍吧!

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

html转js在线工具
html转js在线工具

html转js在线工具