Wpf控制变换

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Wpf控制变换相关的知识,希望对你有一定的参考价值。

我正在尝试将边框从旋转的画布移动到另一画布,但是粘贴后边框的位置不相同。代码中有2个边框以查看旧边框位置。粘贴后如何使边框保持在同一位置?

<Grid>
    <Button Grid.Column="0" Width="50" Height="50" HorizontalAlignment="Left" VerticalAlignment="Top" Click="Button_Click"></Button>
    <Canvas x:Name="canvas1" Canvas.Left="300" Canvas.Top="100" Width="300" Height="250" RenderTransformOrigin="0.5,0.5" Background="Gray" Opacity="0.1">
        <Canvas.RenderTransform>
            <TransformGroup>
                <ScaleTransform/>
                <SkewTransform/>
                <RotateTransform Angle="45"/>
                <TranslateTransform/>
            </TransformGroup>
        </Canvas.RenderTransform>
        <Border x:Name="brd1" Canvas.Left="80" Background="Black" Width="100" Height="200" CornerRadius="30,0,10,0" RenderTransformOrigin="0.29,0.862">
            <Border.RenderTransform>
                <TransformGroup>
                    <ScaleTransform/>
                    <SkewTransform/>
                    <RotateTransform Angle="47.958"/>
                    <TranslateTransform/>
                </TransformGroup>
            </Border.RenderTransform>
        </Border>
        <Border x:Name="brd2" Canvas.Left="80" Background="Black" Width="100" Height="200" CornerRadius="30,0,10,0" RenderTransformOrigin="0.29,0.862">
            <Border.RenderTransform>
                <TransformGroup>
                    <ScaleTransform/>
                    <SkewTransform/>
                    <RotateTransform Angle="47.958"/>
                    <TranslateTransform/>
                </TransformGroup>
            </Border.RenderTransform>
        </Border>
    </Canvas>
    <Canvas x:Name="canvas2" RenderTransformOrigin="0.5, 0.5">

    </Canvas>
</Grid>
private void Button_Click(object sender, RoutedEventArgs e)
{
    Point point = brd2.TransformToVisual(canvas2).Transform(new Point());

    canvas1.Children.Remove(brd2);

    brd2.Background = Brushes.Green;
    brd2.Opacity = 0.4;

    canvas2.Children.Add(brd2);

    Canvas.SetLeft(brd2, point.X);
    Canvas.SetTop(brd2, point.Y);

    ((brd2.RenderTransform as TransformGroup).Children[2] as RotateTransform).Angle +=
       ((canvas1.RenderTransform as TransformGroup).Children[2] as RotateTransform).Angle;
}

粘贴前:

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9jNJjndS5wbmcifQ==” alt =“开始状态(png)。”>

粘贴后:“粘贴到画布上后,位置不好,(png)。”

答案

我发现了如何变换边框。

这里是后面的代码:

private void Button_Click(object sender, RoutedEventArgs e)
{
    double offsetx = ((brd2.RenderTransform as TransformGroup).Children[3] as TranslateTransform).X;
    double offsety = ((brd2.RenderTransform as TransformGroup).Children[3] as TranslateTransform).Y;

    Point point = brd2.TranslatePoint(new Point(brd2.RenderTransformOrigin.X * brd2.RenderSize.Width, brd2.RenderTransformOrigin.Y * brd2.RenderSize.Height), canvas2);

    canvas1.Children.Remove(brd2);

    brd2.Background = Brushes.Green;
    brd2.Opacity = 0.4;

    canvas2.Children.Add(brd2);

    Canvas.SetLeft(brd2, point.X - brd2.RenderTransformOrigin.X * brd2.RenderSize.Width - offsetx);
    Canvas.SetTop(brd2, point.Y - brd2.RenderTransformOrigin.Y * brd2.RenderSize.Height - offsety);

    ((brd2.RenderTransform as TransformGroup).Children[2] as RotateTransform).Angle +=
       ((canvas1.RenderTransform as TransformGroup).Children[2] as RotateTransform).Angle;
}

以上是关于Wpf控制变换的主要内容,如果未能解决你的问题,请参考以下文章

WPF 通过透明度遮罩和变换制作倒影效果

WPF布局

WPF的三维变换应用

二维图形的矩阵变换——在WPF中的应用矩阵变换

WPF 3D 小小小小引擎 - ·WPF 3D变换应用

WPF 3D变换应用