WPF绘图通过矩阵运算实现移动

WPF绘图通过矩阵运算实现移动

前台代码:

<Window x:Class="Chapter3.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:Chapter3"
        mc:Ignorable="d"
        Title="Matrix OPeration" Height="400" Width="500">

    <Viewbox Stretch="Uniform">
        <Grid Width="430" Height="300" VerticalAlignment="Top">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="150"/>
                <ColumnDefinition Width="280"/>
            Grid.ColumnDefinitions>
            <Grid Width="140" Height="300" Margin="5 10 5 5">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="60"/>
                    <ColumnDefinition Width="70"/>
                Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                Grid.RowDefinitions>

                
                <TextBlock Grid.Row="0" Grid.Column="0" HorizontalAlignment="Right"
                           Margin="5 5 10 5 " >M11TextBlock>
                <TextBox Name="tbM11" Grid.Row="0" Grid.Column="1" TextAlignment="Center">50TextBox>

                
                <TextBlock Grid.Row="1" Grid.Column="0" HorizontalAlignment="Right"
                           Margin="5 5 10 5 " >M12TextBlock>
                <TextBox Name="tbM12" Grid.Row="1" Grid.Column="1" TextAlignment="Center">200TextBox>

                
                <TextBlock Grid.Row="2" Grid.Column="0" HorizontalAlignment="Right"
                           Margin="5 5 10 5 " >M21TextBlock>
                <TextBox Name="tbM21" Grid.Row="2" Grid.Column="1" TextAlignment="Center">150TextBox>

                
                <TextBlock Grid.Row="3" Grid.Column="0" HorizontalAlignment="Right"
                           Margin="5 5 10 5 " >M22TextBlock>
                <TextBox Name="tbM22" Grid.Row="3" Grid.Column="1" TextAlignment="Center">100TextBox>

                
                <TextBlock Grid.Row="4" Grid.Column="0" HorizontalAlignment="Right"
                           Margin="5 5 10 5 " >OffsetXTextBlock>
                <TextBox Name="tbOffsetX" Grid.Row="4" Grid.Column="1" TextAlignment="Center">100TextBox>

                
                <TextBlock Grid.Row="5" Grid.Column="0" HorizontalAlignment="Right"
                           Margin="5 5 10 5 " >OffsetYTextBlock>
                <TextBox Name="tbOffsetY" Grid.Row="5" Grid.Column="1" TextAlignment="Center">100TextBox>


                
                <Button Name="btnApply" Grid.Row="6" Grid.Column="0" Margin="15 20 15 5" Height="25" 
                        Grid.ColumnSpan="2" Click="btnApply_Click">ApplyButton>

                
                <Button Name="btnClose" Grid.Row="7" Grid.Column="0" Margin="15 5 15 5" Height="25" 
                        Grid.ColumnSpan="2" Click="btnClose_Click">CloseButton>

            Grid>
            <Border Margin="10" Grid.Column="1" BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Left" Background="LightGray" >
                <Canvas Name="canvas1" Grid.Column="1" ClipToBounds="True" Width="270" Height="280">
                    <TextBlock Canvas.Top="53" Canvas.Left="90" >Original ShapeTextBlock>
                    <Rectangle Canvas.Top="70" Canvas.Left="100" Width="50" Height="70" Stroke="Black" StrokeThickness="2" StrokeDashArray="3,1"/>
                    <Rectangle Name="rect" Canvas.Top="70" Canvas.Left="100" Width="50" Height="70" Fill="LightCoral" Opacity="0.5" StrokeThickness="2">
                        <Rectangle.RenderTransform>
                            <MatrixTransform x:Name="matrixTransform">MatrixTransform>
                        Rectangle.RenderTransform>
                    Rectangle>

                Canvas>
            Border>

        Grid>
    Viewbox>
Window>

后台代码:

#region NameSpaces
using System;
using System.Collections;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Shapes;
using System.Windows.Media;
#endregion

namespace Chapter3
{
	/// 
	/// MainWindow.xaml 的交互逻辑
	/// 
	public partial class MainWindow : Window
	{


		public MainWindow()
		{
			InitializeComponent();


		}

		private void btnApply_Click(object sender, RoutedEventArgs e)
		{
			Matrix m = new Matrix();
			m.M11 = Convert.ToDouble(tbM11.Text);
			m.M12 = Convert.ToDouble(tbM12.Text);
			m.M21 = Convert.ToDouble(tbM21.Text);
			m.M22 = Convert.ToDouble(tbM22.Text);
			m.OffsetX = Convert.ToDouble(tbOffsetX.Text);
			m.OffsetY = Convert.ToDouble(tbOffsetY.Text);matrixTransform.Matrix = m;
		}

		private void btnClose_Click(object sender, RoutedEventArgs e)
		{
			this.Close();
		}
	}
}

效果如下:
WPF绘图通过矩阵运算实现移动_第1张图片

你可能感兴趣的:(WPF学习笔记)