■ ImageBrush 엘리먼트를 사용하는 방법을 보여준다.
▶ 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 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 |
<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> <LinearGradientBrush x:Key="GrayBlueLinearGradientBrushKey" StartPoint="0 0" EndPoint="1 1"> <LinearGradientBrush.GradientStops> <GradientStop Offset="0" Color="DarkGray" /> <GradientStop Offset="0.5" Color="#ccccff" /> <GradientStop Offset="1" Color="DarkGray" /> </LinearGradientBrush.GradientStops> </LinearGradientBrush> <LinearGradientBrush x:Key="GlassLinearGradientBrushKey" StartPoint="0 0" EndPoint="1 1" Opacity="0.75"> <LinearGradientBrush.GradientStops> <GradientStop Offset="0.2" Color="WhiteSmoke" /> <GradientStop Offset="0.4" Color="Transparent" /> <GradientStop Offset="0.5" Color="WhiteSmoke" /> <GradientStop Offset="0.75" Color="Transparent" /> <GradientStop Offset="0.9" Color="WhiteSmoke" /> <GradientStop Offset="1" Color="Transparent" /> </LinearGradientBrush.GradientStops> </LinearGradientBrush> <Style TargetType="{x:Type RadioButton}"> <Setter Property="Background" Value="{StaticResource GrayBlueLinearGradientBrushKey}" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type RadioButton}"> <Grid Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" ClipToBounds="True"> <Rectangle Name="outerRectangle" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" RadiusX="20" RadiusY="20" StrokeThickness="5" Stroke="{TemplateBinding Background}" Fill="Transparent" /> <Rectangle Name="innerRectangle" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" RadiusX="20" RadiusY="20" StrokeThickness="20" Stroke="Transparent" Fill="{TemplateBinding Background}" /> <Rectangle Name="glassRectangle" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" RadiusX="10" RadiusY="10" StrokeThickness="2" Opacity="0" Fill="{StaticResource GlassLinearGradientBrushKey}" RenderTransformOrigin="0.5 0.5"> <Rectangle.Stroke> <LinearGradientBrush StartPoint="0.5 0" EndPoint="0.5 1"> <LinearGradientBrush.GradientStops> <GradientStop Offset="0.0" Color="LightBlue" /> <GradientStop Offset="1.0" Color="Gray" /> </LinearGradientBrush.GradientStops> </LinearGradientBrush> </Rectangle.Stroke> <Rectangle.RenderTransform> <TransformGroup> <ScaleTransform /> <RotateTransform /> </TransformGroup> </Rectangle.RenderTransform> <Rectangle.BitmapEffect> <BevelBitmapEffect /> </Rectangle.BitmapEffect> </Rectangle> <DockPanel> <ContentPresenter Name="contentPresenter" Margin="20" TextBlock.Foreground="Black" Content="{TemplateBinding Content}" /> </DockPanel> </Grid> <ControlTemplate.Triggers> <MultiTrigger> <MultiTrigger.Conditions> <Condition Property="IsMouseOver" Value="True" /> <Condition Property="IsChecked" Value="False" /> </MultiTrigger.Conditions> <Setter TargetName="outerRectangle" Property="Rectangle.Stroke" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" /> <Setter TargetName="glassRectangle" Property="Rectangle.Opacity" Value="1" /> <Setter TargetName="contentPresenter" Property="ContentPresenter.BitmapEffect"> <Setter.Value> <BlurBitmapEffect Radius="1" /> </Setter.Value> </Setter> </MultiTrigger> <Trigger Property="IsChecked" Value="true"> <Setter TargetName="glassRectangle" Property="Rectangle.Opacity" Value="1" /> </Trigger> <Trigger Property="IsFocused" Value="true"> <Setter TargetName="outerRectangle" Property="Rectangle.Stroke" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" /> <Setter TargetName="glassRectangle" Property="Rectangle.Opacity" Value="1" /> </Trigger> <EventTrigger RoutedEvent="Mouse.MouseEnter"> <EventTrigger.Actions> <BeginStoryboard Name="mouseEnterBeginStoryboard"> <Storyboard> <DoubleAnimation Storyboard.TargetName="glassRectangle" Storyboard.TargetProperty="(Rectangle.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)" Duration="00:00:00.5" By="-0.1" /> <DoubleAnimation Storyboard.TargetName="glassRectangle" Storyboard.TargetProperty="(Rectangle.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)" Duration="00:00:00.5" By="-0.1" /> </Storyboard> </BeginStoryboard> </EventTrigger.Actions> </EventTrigger> <EventTrigger RoutedEvent="Mouse.MouseLeave"> <EventTrigger.Actions> <StopStoryboard BeginStoryboardName="mouseEnterBeginStoryboard" /> </EventTrigger.Actions> </EventTrigger> <EventTrigger RoutedEvent="RadioButton.Checked"> <EventTrigger.Actions> <BeginStoryboard> <Storyboard> <DoubleAnimation Storyboard.TargetName="glassRectangle" Storyboard.TargetProperty="(Rectangle.RenderTransform).(TransformGroup.Children)[1].(RotateTransform.Angle)" Duration="00:00:00.5" By="360" /> </Storyboard> </BeginStoryboard> </EventTrigger.Actions> </EventTrigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> <Style x:Key="AnimatedRectangleStyleKey"> <Setter Property="Rectangle.Fill"> <Setter.Value> <ImageBrush TileMode="FlipXY" Opacity="0.25" ImageSource="IMAGE\purpleblock.jpg"> <ImageBrush.Transform> <RotateTransform Angle="0" /> </ImageBrush.Transform> </ImageBrush> </Setter.Value> </Setter> <Style.Triggers> <EventTrigger RoutedEvent="Rectangle.Loaded"> <EventTrigger.Actions> <BeginStoryboard> <Storyboard SpeedRatio="1.25"> <RectAnimation Storyboard.TargetProperty="(Rectangle.Fill).(ImageBrush.Viewport)" Duration="00:00:20" To="0 0 0.5 0.5" RepeatBehavior="Forever" AutoReverse="True" /> <DoubleAnimation Storyboard.TargetProperty="(Rectangle.Fill).(ImageBrush.Transform).(RotateTransform.Angle)" Duration="00:01:00" To="360" RepeatBehavior="Forever" AccelerationRatio="0.5" DecelerationRatio="0.5" /> </Storyboard> </BeginStoryboard> </EventTrigger.Actions> </EventTrigger> </Style.Triggers> </Style> </Window.Resources> <Grid Margin="10"> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="*" /> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <Rectangle Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="4" Style="{StaticResource AnimatedRectangleStyleKey}" Margin="0 0 0 10" /> <Border Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="4" Margin="0 0 0 10" BorderBrush="Black" BorderThickness="3"> <Border.Background> <LinearGradientBrush StartPoint="0 0.5" EndPoint="1 0.5"> <LinearGradientBrush.GradientStops> <GradientStop Offset="0.0" Color="#993399ff" /> <GradientStop Offset="1.0" Color="#996600ff" /> </LinearGradientBrush.GradientStops> </LinearGradientBrush> </Border.Background> <DockPanel Margin="10"> <RadioButton Name="imageRadioButton1" Margin="5"> <Image Width="50" Height="50" Source="IMAGE\blueberries.jpg" /> </RadioButton> <RadioButton Name="imageRadioButton2" Margin="5"> <Image Width="50" Height="50" Source="IMAGE\sunset.jpg" /> </RadioButton> <RadioButton Name="imageRadioButton3" Margin="5"> <Image Width="50" Height="50" Source="IMAGE\apple.jpg" /> </RadioButton> <RadioButton Name="imageRadioButton4" Margin="5"> <Image Width="50" Height="50" Source="IMAGE\hummingbird.jpg" /> </RadioButton> <TextBlock DockPanel.Dock="Left" Margin="10" TextWrapping="Wrap"> The following controls enable you to customize an ImageBrush. Specify the ImageBrush's properties and click <Bold>Apply</Bold> to view the ImageBrush. </TextBlock> </DockPanel> </Border> <Label Grid.Row="1" Grid.Column="0" HorizontalAlignment="Right" Margin="5"> Stretch </Label> <ComboBox Name="stretchComboBox" Grid.Row="1" Grid.Column="1" Margin="5" VerticalContentAlignment="Center" /> <Label Grid.Row="2" Grid.Column="0" HorizontalAlignment="Right" Margin="5"> AlignmentX </Label> <ComboBox Name="alignmentXComboBox" Grid.Row="2" Grid.Column="1" Margin="5" VerticalContentAlignment="Center" /> <Label Grid.Row="3" Grid.Column="0" HorizontalAlignment="Right" Margin="5"> AlignmentY </Label> <ComboBox Name="alignmentYComboBox" Grid.Row="3" Grid.Column="1" Margin="5" VerticalContentAlignment="Center" /> <Label Grid.Row="4" Grid.Column="0" HorizontalAlignment="Right" Margin="5"> TileMode </Label> <ComboBox Name="tileModeComboBox" Grid.Row="4" Grid.Column="1" Margin="5" VerticalContentAlignment="Center" /> <Label Grid.Row="1" Grid.Column="2" HorizontalAlignment="Right" Margin="5"> Viewport </Label> <TextBox Name="viewportTextBox" Grid.Row="1" Grid.Column="3" Margin="5" VerticalContentAlignment="Center" /> <Label Grid.Row="2" Grid.Column="2" HorizontalAlignment="Right" Margin="5"> ViewportUnits </Label> <ComboBox Name="viewportUnitsComboBox" Grid.Row="2" Grid.Column="3" Margin="5" VerticalContentAlignment="Center" /> <Label Grid.Row="3" Grid.Column="2" HorizontalAlignment="Right" Margin="5"> Viewbox </Label> <TextBox Name="viewboxTextBox" Grid.Row="3" Grid.Column="3" Margin="5" VerticalContentAlignment="Center" /> <Label Grid.Row="4" Grid.Column="2" HorizontalAlignment="Right" Margin="5"> ViewboxUnits </Label> <ComboBox Name="viewboxUnitsComboBox" Grid.Row="4" Grid.Column="3" Margin="5" VerticalContentAlignment="Center" /> <Button Name="applyButton" Grid.Row="5" Grid.Column="1" HorizontalAlignment="Left" Margin="5" Width="100" Height="30"> Apply </Button> <Rectangle Grid.Row="6" Grid.Column="0" Grid.ColumnSpan="4" Margin="0 5 0 0" Stroke="Blue"> <Rectangle.Fill> <ImageBrush x:Name="targetImageBrush" ImageSource="IMAGE\blueberries.jpg" /> </Rectangle.Fill> </Rectangle> </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 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 |
using System; using System.Windows; using System.Windows.Controls; using System.Windows.Media; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 체크 라디오 버튼 /// </summary> private RadioButton checkedRadioButton; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); Loaded += Window_Loaded; this.imageRadioButton1.Checked += imageRadioButton_Checked; this.imageRadioButton2.Checked += imageRadioButton_Checked; this.imageRadioButton3.Checked += imageRadioButton_Checked; this.imageRadioButton4.Checked += imageRadioButton_Checked; this.applyButton.Click += applyButton_Click; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 윈도우 로드시 처리하기 - Window_Loaded(sender, e) /// <summary> /// 윈도우 로드시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Window_Loaded(object sender, RoutedEventArgs e) { #region Stretch 콤보 박스를 설정한다. string[] stretchArray = Enum.GetNames(typeof(Stretch)); foreach(string stretch in stretchArray) { this.stretchComboBox.Items.Add(stretch); } this.stretchComboBox.SelectedItem = this.targetImageBrush.Stretch.ToString(); #endregion #region AlignmentX 콤보 박스를 설정한다. string[] alignmentXArray = Enum.GetNames(typeof(AlignmentX)); foreach(string alignmentX in alignmentXArray) { this.alignmentXComboBox.Items.Add(alignmentX); } this.alignmentXComboBox.SelectedItem = this.targetImageBrush.AlignmentX.ToString(); #endregion #region AlignmentY 콤보 박스를 설정한다. string[] alignmentYArray = Enum.GetNames(typeof (AlignmentY)); foreach(string alignmentY in alignmentYArray) { this.alignmentYComboBox.Items.Add(alignmentY); } this.alignmentYComboBox.SelectedItem = this.targetImageBrush.AlignmentY.ToString(); #endregion #region TileMode 콤보 박스를 설정한다. string[] timeModeArray = Enum.GetNames(typeof(TileMode)); foreach(string tileMode in timeModeArray) { this.tileModeComboBox.Items.Add(tileMode); } this.tileModeComboBox.SelectedItem = this.targetImageBrush.TileMode.ToString(); #endregion #region ViewportUnits/ViewboxUnits 콤보 박스를 설정한다. string[] brushMappingModeArray = Enum.GetNames(typeof(BrushMappingMode)); foreach(string brushMappingMode in brushMappingModeArray) { this.viewportUnitsComboBox.Items.Add(brushMappingMode); this.viewboxUnitsComboBox.Items.Add(brushMappingMode); } this.viewportUnitsComboBox.SelectedItem = this.targetImageBrush.ViewportUnits.ToString(); this.viewboxUnitsComboBox.SelectedItem = this.targetImageBrush.ViewboxUnits.ToString(); #endregion this.viewportTextBox.Text = this.targetImageBrush.Viewport.ToString(); this.viewboxTextBox.Text = this.targetImageBrush.Viewbox.ToString(); this.imageRadioButton1.IsChecked = true; } #endregion #region 이미지 라디오 버튼 체크시 처리하기 - imageRadioButton_Checked(sender, e) /// <summary> /// 이미지 라디오 버튼 체크시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void imageRadioButton_Checked(object sender, RoutedEventArgs e) { this.checkedRadioButton = sender as RadioButton; } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region Apply 버튼 클릭시 처리하기 - applyButton_Click(sender, e) /// <summary> /// Apply 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void applyButton_Click(object sender, RoutedEventArgs e) { try { this.targetImageBrush.ImageSource = (this.checkedRadioButton.Content as Image).Source; this.targetImageBrush.Stretch = (Stretch)Enum.Parse(typeof(Stretch), (string)this.stretchComboBox.SelectedItem); this.targetImageBrush.AlignmentX = (AlignmentX)Enum.Parse(typeof(AlignmentX), (string)this.alignmentXComboBox.SelectedItem); this.targetImageBrush.AlignmentY = (AlignmentY)Enum.Parse(typeof(AlignmentY), (string)this.alignmentYComboBox.SelectedItem); this.targetImageBrush.TileMode = (TileMode)Enum.Parse(typeof(TileMode), (string)this.tileModeComboBox.SelectedItem); this.targetImageBrush.ViewportUnits = (BrushMappingMode)Enum.Parse(typeof(BrushMappingMode), (string)this.viewportUnitsComboBox.SelectedItem); this.targetImageBrush.ViewboxUnits = (BrushMappingMode)Enum.Parse(typeof(BrushMappingMode), (string)this.viewboxUnitsComboBox.SelectedItem); RectConverter rectConverter = new RectConverter(); string viewport = this.viewportTextBox.Text; if(!string.IsNullOrEmpty(viewport)) { this.targetImageBrush.Viewport = (Rect)rectConverter.ConvertFromString(viewport); } else { this.targetImageBrush.Viewport = Rect.Empty; this.viewportTextBox.Text = "Empty"; } string viewbox = this.viewboxTextBox.Text; if(!string.IsNullOrEmpty(viewbox) && viewbox.ToLower() != "(auto)") { this.targetImageBrush.Viewbox = (Rect)rectConverter.ConvertFromString(viewbox); } else { this.viewboxTextBox.Text = "Empty"; this.targetImageBrush.Viewbox = Rect.Empty; } } catch(InvalidOperationException invalidOperationException) { MessageBox.Show("Invalid Viewport or Viewbox. " + invalidOperationException); } catch(FormatException formatException) { MessageBox.Show("Invalid Viewport or Viewbox. " + formatException); } } #endregion } } |