-2

基本上 WPF 元素是矩形的。
通过它们的宽度和高度很容易得到它们的轮廓。
但在一般情况下,一个元素的轮廓可以是任何一条线,包括由几个不相交的轮廓组成。
我预计 VisualTreeHelper.GetClip () 方法将返回轮廓的几何形状。但在我的尝试中,它总是返回 null。
也许我使用不正确,或者根本不适合这个目的。

我尝试获取大纲:

using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;

namespace CopyGeometryElements
{
    public partial class CopyGeometryElementsWindow : Window
    {
        public CopyGeometryElementsWindow()
        {
            InitializeComponent();
        }

        private void OnCopy(object sender, RoutedEventArgs e)
        {
            foreach (UIElement uie
                in source.Children)
            {
                // Always returns null
                var geometry = VisualTreeHelper.GetClip(uie);

                var offset = uie.TranslatePoint(new Point(), source);
                var copy = new Path()
                {
                    Data=geometry,
                    Stroke = Brushes.Red,
                    StrokeThickness=2
                };
                Canvas.SetLeft(copy, offset.X);
                Canvas.SetTop(copy, offset.Y);
                target.Children.Add(copy);
            }
        }
    }
}
<Window x:Class="CopyGeometryElements.CopyGeometryElementsWindow"
        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:CopyGeometryElements"
        mc:Ignorable="d"
        Title="CopyGeometryElementsWindow" Height="450" Width="800">
    <Grid Background="AliceBlue">
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Grid x:Name="source"
              Margin="10" Background="White">
            <Border BorderBrush="Green" BorderThickness="2"
                    CornerRadius="15"
                    Width="40" Height="60" Margin="10"
                    HorizontalAlignment="Left" VerticalAlignment="Top"/>
            
            <Ellipse Stroke="Green" StrokeThickness="2"
                     Width="40" Height="60"
                     HorizontalAlignment="Left" VerticalAlignment="Top"
                     Margin="200,100,0,0"/>

            <Polygon Stroke="Green" StrokeThickness="2"
                      HorizontalAlignment="Left" VerticalAlignment="Top"
                      Margin="100,150,0,0" 
                      Points="0,0 100,100 0,200 50,100">
            </Polygon>
        </Grid>
        <Button Grid.Column="1" Padding="15 5"
                HorizontalAlignment="Center" VerticalAlignment="Center"
                Click="OnCopy">
            <TextBlock Text="Copy the geometry of all shapes from the left panel to the right"
                       Width="80"
                       TextWrapping="Wrap"
                       TextAlignment="Center"/>
        </Button>
        <Canvas x:Name="target" Grid.Column="2" Background="White"
                Margin="10"/>
    </Grid>
</Window>
4

1 回答 1

1

我确信这段代码只是得到你的剪辑。例如:

<Button Height="35" Width="75">
    <Button.Clip>
        <GeometryGroup>
            <RectangleGeometry Rect="0, 0, 100, 100"/>
        </GeometryGroup>
    </Button.Clip>
</Button>

在这种情况下,您可以获得此剪辑。

    var figure = VisualTreeHelper.GetClip(uie);

但是您的剪辑是空的,因此您的结果将为空

于 2021-07-21T08:26:15.757 回答