■ MatrixTransform 엘리먼트를 사용해 대화형 매트릭스 변환을 만드는 방법을 보여준다.
▶ MainWindow.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="800" Height="600" Title="TestProject" FontFamily="나눔고딕코딩" FontSize="16"> <Window.Resources> <DrawingBrush x:Key="BlueGridDrawingBrushKey" ViewportUnits="Absolute" Viewport="0 0 10 10" TileMode="Tile"> <DrawingBrush.Drawing> <DrawingGroup> <DrawingGroup.Children> <GeometryDrawing Brush="White"> <GeometryDrawing.Geometry> <RectangleGeometry Rect="0 0 1 1" /> </GeometryDrawing.Geometry> </GeometryDrawing> <GeometryDrawing Brush="#ccccff" Geometry="M 0 0 L 1 0 1 0.1 0 0.1 Z" /> <GeometryDrawing Brush="#ccccff" Geometry="M 0 0 L 0 1 0.1 1 0.1 0 Z" /> </DrawingGroup.Children> </DrawingGroup> </DrawingBrush.Drawing> </DrawingBrush> <DrawingBrush x:Key="GreenGridDrawingBrushKey" ViewportUnits="Absolute" Viewport="0 0 10 10" TileMode="Tile"> <DrawingBrush.Drawing> <DrawingGroup> <DrawingGroup.Children> <GeometryDrawing Brush="White"> <GeometryDrawing.Geometry> <RectangleGeometry Rect="0 0 1 1" /> </GeometryDrawing.Geometry> </GeometryDrawing> <GeometryDrawing Brush="Lime" Geometry="M 0 0 L 1 0 1 0.1 0 0.1 Z" /> <GeometryDrawing Brush="Lime" Geometry="M 0 0 L 0 1 0.1 1 0.1 0 Z" /> </DrawingGroup.Children> </DrawingGroup> </DrawingBrush.Drawing> </DrawingBrush> <Style TargetType="{x:Type TextBlock}"> <Setter Property="TextWrapping" Value="Wrap" /> </Style> </Window.Resources> <Grid Margin="10"> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <TextBlock Grid.Row="0"> <Bold>Interactive MatrixTransform Example</Bold> </TextBlock> <Grid Grid.Row="1" Margin="0 10 0 0"> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="Auto" /> </Grid.ColumnDefinitions> <Label Grid.Row="0" Grid.Column="0">M11</Label> <TextBox Name="m11TextBox" Grid.Row="0" Grid.Column="1" Width="50" Height="25" VerticalContentAlignment="Center" HorizontalContentAlignment="Right"> 1.0 </TextBox> <Label Grid.Row="0" Grid.Column="2" Margin="10 0 0 0"> M21 </Label> <TextBox Name="m21TextBox" Grid.Row="0" Grid.Column="3" Width="50" Height="25" VerticalContentAlignment="Center" HorizontalContentAlignment="Right"> 0.0 </TextBox> <Label Grid.Row="1" Grid.Column="0" Margin="0 10 0 0"> M12 </Label> <TextBox Name="m12TextBox" Grid.Row="1" Grid.Column="1" Margin="0 10 0 0" Width="50" Height="25" VerticalContentAlignment="Center" HorizontalContentAlignment="Right"> 0.0 </TextBox> <Label Grid.Row="1" Grid.Column="2" Margin="10 10 0 0">M22</Label> <TextBox Name="m22TextBox" Grid.Row="1" Grid.Column="3" Margin="0 10 0 0" Width="50" Height="25" VerticalContentAlignment="Center" HorizontalContentAlignment="Right"> 1.0 </TextBox> <Label Grid.Row="2" Grid.Column="0" Margin="0 10 0 0"> OffsetX </Label> <TextBox Name="offsetXTextBox" Grid.Row="2" Grid.Column="1" Margin="0 10 0 0" Width="50" Height="25" VerticalContentAlignment="Center" HorizontalContentAlignment="Right"> 0.0 </TextBox> <Label Grid.Row="2" Grid.Column="2" Margin="10 10 0 0"> OffsetY </Label> <TextBox Name="offsetYTextBox" Grid.Row="2" Grid.Column="3" Margin="0 10 0 0" Width="50" Height="25" VerticalContentAlignment="Center" HorizontalContentAlignment="Right"> 0.0 </TextBox> <Button Name="applyButton" Grid.Row="3" Grid.Column="0" Margin="0 10 0 0" Width="100" Height="30"> Apply </Button> <Grid Grid.Column="4" Grid.Row="0" Grid.RowSpan="4" Margin="30 0"> <Grid.Resources> <Style TargetType="{x:Type TextBlock}"> <Setter Property="HorizontalAlignment" Value="Stretch" /> <Setter Property="FontWeight" Value="Bold" /> </Style> </Grid.Resources> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="Auto" /> </Grid.ColumnDefinitions> <Rectangle Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" HorizontalAlignment="Left" Width="10" Height="1" Fill="Black" /> <Rectangle Grid.Row="0" Grid.RowSpan="6" Grid.Column="0" Width="1" Fill="Black" /> <Rectangle Grid.Row="5" Grid.Column="0" Grid.ColumnSpan="2" HorizontalAlignment="Left" Width="10" Height="1" Fill="Black" /> <TextBlock Grid.Row="1" Grid.Column="1" Margin="10"> <TextBlock.Text> <Binding ElementName="m11TextBox" Path="Text" /> </TextBlock.Text> </TextBlock> <TextBlock Grid.Row="1" Grid.Column="2" Margin="10"> <TextBlock.Text> <Binding ElementName="m21TextBox" Path="Text" /> </TextBlock.Text> </TextBlock> <TextBlock Grid.Row="1" Grid.Column="3" Margin="10"> 0.0 </TextBlock> <TextBlock Grid.Row="2" Grid.Column="1" Margin="10"> <TextBlock.Text> <Binding ElementName="m12TextBox" Path="Text" /> </TextBlock.Text> </TextBlock> <TextBlock Grid.Row="2" Grid.Column="2" Margin="10"> <TextBlock.Text> <Binding ElementName="m22TextBox" Path="Text" /> </TextBlock.Text> </TextBlock> <TextBlock Grid.Row="2" Grid.Column="3" Margin="10"> 0.0 </TextBlock> <TextBlock Grid.Row="3" Grid.Column="1" Margin="10"> <TextBlock.Text> <Binding ElementName="offsetXTextBox" Path="Text" /> </TextBlock.Text> </TextBlock> <TextBlock Grid.Row="3" Grid.Column="2" Margin="10"> <TextBlock.Text> <Binding ElementName="offsetYTextBox" Path="Text" /> </TextBlock.Text> </TextBlock> <TextBlock Grid.Row="3" Grid.Column="3" Margin="10"> 1.0 </TextBlock> <Rectangle Grid.Row="0" Grid.Column="3" Grid.ColumnSpan="2" HorizontalAlignment="Right" Width="10" Height="1" Fill="Black" /> <Rectangle Grid.Row="0" Grid.RowSpan="6" Grid.Column="4" Width="1" Fill="Black" /> <Rectangle Grid.Row="5" Grid.Column="3" Grid.ColumnSpan="2" HorizontalAlignment="Right" Width="10" Height="1" Fill="Black" /> </Grid> </Grid> <Border Grid.Row="2" Margin="0 10 0 0" BorderThickness="2" BorderBrush="Black"> <Canvas Background="{StaticResource BlueGridDrawingBrushKey}"> <TextBlock Canvas.Left="80" Canvas.Top="80"> Before Transform </TextBlock> <Rectangle Canvas.Left="100" Canvas.Top="100" Width="100" Height="100" StrokeThickness="2" Stroke="Black" StrokeDashArray="2 1" Fill="{StaticResource BlueGridDrawingBrushKey}" Opacity="0.50" /> <Rectangle Name="transformedRectangle" Canvas.Left="100" Canvas.Top="100" Width="100" Height="100" StrokeThickness="2" Stroke="Black" Fill="{StaticResource GreenGridDrawingBrushKey}"> <Rectangle.RenderTransform> <MatrixTransform x:Name="targetMatrixTransform" /> </Rectangle.RenderTransform> </Rectangle> </Canvas> </Border> </Grid> </Window> |
▶ MainWindow.xaml.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
using System; using System.Windows; using System.Windows.Media; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); this.applyButton.Click += applyButton_Click; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region Apply 버튼 클릭시 처리하기 - applyButton_Click(sender, e) /// <summary> /// Apply 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void applyButton_Click(object sender, EventArgs e) { UpdateTargetMatrixTransform(); } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 타겟 매트릭스 변환 업데이트하기 - UpdateTargetMatrixTransform() /// <summary> /// 타겟 매트릭스 변환 업데이트하기 /// </summary> private void UpdateTargetMatrixTransform() { Matrix matrix = new Matrix { M11 = double.Parse(this.m11TextBox.Text ), M12 = double.Parse(this.m12TextBox.Text ), M21 = double.Parse(this.m21TextBox.Text ), M22 = double.Parse(this.m22TextBox.Text ), OffsetX = double.Parse(this.offsetXTextBox.Text), OffsetY = double.Parse(this.offsetYTextBox.Text) }; this.targetMatrixTransform.Matrix = matrix; } #endregion } } |