ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

验证控件是否重叠,获取重叠部分

2021-07-02 16:06:20  阅读:148  来源: 互联网

标签:控件 Canvas 重叠 验证 Windows System using Rect r1


某些时候,需要验证控件之间是否存在重叠的情况,可以借助  System.Windows.Rect.IntersectsWith 来验证;如果需要获取重叠的部分,则使用  System.Windows.Rect.Intersect 来实现!

<Window x:Class="轨迹规划Demo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:轨迹规划Demo"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Canvas x:Name="can">
            <Border x:Name="b1" BorderThickness="1" BorderBrush="Red" Canvas.Left="10" Canvas.Top="10" Width="200" Height="200" />
            <Border x:Name="b2" BorderThickness="1" BorderBrush="Black" Canvas.Left="50" Canvas.Top="40" Width="200" Height="200" />
        </Canvas>
        <StackPanel HorizontalAlignment="Left" VerticalAlignment="Bottom" Margin="10" >
            <Button Content="计算交集" Click="Button_Click"/>
        </StackPanel>
    </Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace 轨迹规划Demo
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Rect r1 = new Rect((double)b1.GetValue(Canvas.LeftProperty), (double)b1.GetValue(Canvas.TopProperty), 
                b1.ActualWidth, b1.ActualHeight);
            System.Windows.Rect r2 = new Rect((double)b2.GetValue(Canvas.LeftProperty), (double)b2.GetValue(Canvas.TopProperty), 
                b2.ActualWidth, b2.ActualHeight);

            r1.Intersect(r2);
            //r1.IntersectsWith(r2);

            Border b = new Border();
            b.Background = new SolidColorBrush(Colors.Blue);
            b.Width = r1.Width;
            b.Height = r1.Height;
            b.SetValue(Canvas.LeftProperty, r1.X);
            b.SetValue(Canvas.TopProperty, r1.Y);
            can.Children.Add(b);
        }
    }
}

 

   

标签:控件,Canvas,重叠,验证,Windows,System,using,Rect,r1
来源: https://blog.51cto.com/u_15241978/2970954

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有