■ RadialGradientBrush 엘리먼트를 사용하는 방법을 보여준다.
▶ MainPage.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 |
<Page x:Class="TestProject.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:media="using:Microsoft.UI.Xaml.Media" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" FontFamily="나눔고딕코딩" FontSize="16"> <Grid> <Grid HorizontalAlignment="Center" VerticalAlignment="Center"> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="50" /> <ColumnDefinition Width="Auto" /> </Grid.ColumnDefinitions> <StackPanel Grid.Column="0" HorizontalAlignment="Center" VerticalAlignment="Center" Orientation="Horizontal"> <Rectangle Name="rectangle" Width="200" Height="200"> <Rectangle.Fill> <media:RadialGradientBrush x:Name="radialGradientBrush" MappingMode="RelativeToBoundingBox" Center="0.25 0.25" RadiusX=".5" RadiusY=".5" GradientOrigin="0.5 0.25" SpreadMethod="Pad"> <GradientStop Offset="0.0" Color="Yellow" /> <GradientStop Offset="1" Color="Blue" /> </media:RadialGradientBrush> </Rectangle.Fill> </Rectangle> </StackPanel> <Grid Grid.Column="2"> <Grid.RowDefinitions> <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" /> </Grid.ColumnDefinitions> <ComboBox Name="mappingModeComboBox" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" Header="MappingMode" SelectedValue="RelativeToBoundingBox"> <x:String>RelativeToBoundingBox</x:String> <x:String>Absolute</x:String> </ComboBox> <Slider Name="centerXSlider" Grid.Row="1" Grid.Column="0" Margin="0 10 0 0" Header="Center.X" SmallChange="0.05" ValueChanged="slider_ValueChanged" /> <Slider Name="centerYSlider" Grid.Row="1" Grid.Column="1" Margin="10 10 0 0" Header="Center.Y" SmallChange="0.05" ValueChanged="slider_ValueChanged" /> <Slider Name="radiusXSlider" Grid.Row="2" Grid.Column="0" Margin="0 10 0 0" Header="RadiusX" SmallChange="0.05" ValueChanged="slider_ValueChanged" /> <Slider Name="radiusYSlider" Grid.Row="2" Grid.Column="1" Margin="10 10 0 0" Header="RadiusY" SmallChange="0.05" ValueChanged="slider_ValueChanged" /> <Slider Name="gradientOriginXSlider" Grid.Row="3" Grid.Column="0" Margin="0 10 0 0" Header="GradientOrigin.X" SmallChange="0.05" ValueChanged="slider_ValueChanged" /> <Slider Name="gradientOriginYSlider" Grid.Row="3" Grid.Column="1" Margin="10 10 0 0" Header="GradientOrigin.Y" SmallChange="0.05" ValueChanged="slider_ValueChanged" /> <ComboBox Name="spreadMethodComboBox" Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="2" Margin="0 10 0 0" Header="SpreadMethod" SelectedValue="Pad"> <x:String>Pad</x:String> <x:String>Reflect</x:String> <x:String>Repeat</x:String> </ComboBox> </Grid> </Grid> </Grid> </Page> |
▶ MainPage.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 |
using System; using System.Numerics; using Windows.Foundation; using Windows.Graphics.Display; using Windows.UI.ViewManagement; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Controls.Primitives; using Windows.UI.Xaml.Media; namespace TestProject { /// <summary> /// 메인 페이지 /// </summary> public sealed partial class MainPage : Page { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainPage() /// <summary> /// 생성자 /// </summary> public MainPage() { InitializeComponent(); #region 윈도우 크기를 설정한다. double width = 800d; double height = 600d; double dpi = (double)DisplayInformation.GetForCurrentView().LogicalDpi; ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.PreferredLaunchViewSize; Size windowSize = new Size(width * 96d / dpi, height * 96d / dpi); ApplicationView.PreferredLaunchViewSize = windowSize; Window.Current.Activate(); ApplicationView.GetForCurrentView().TryResizeView(windowSize); #endregion #region 윈도우 제목을 설정한다. ApplicationView.GetForCurrentView().Title = "RadialGradientBrush 엘리먼트 사용하기"; #endregion Loaded += Page_Loaded; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 페이지 로드시 처리하기 - Page_Loaded(sender, e) /// <summary> /// 페이지 로드시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Page_Loaded(object sender, RoutedEventArgs e) { this.mappingModeComboBox.SelectionChanged += mappingModeComboBox_SelectionChanged; this.spreadMethodComboBox.SelectionChanged += spreadMethodComboBox_SelectionChanged; InitializeSlider(); } #endregion #region 스프레드 메소드 콤보 박스 선택 변경시 처리하기 - spreadMethodComboBox_SelectionChanged(sender, e) /// <summary> /// 스프레드 메소드 콤보 박스 선택 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void spreadMethodComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { this.radialGradientBrush.SpreadMethod = Enum.Parse<GradientSpreadMethod>(this.spreadMethodComboBox.SelectedValue.ToString()); } #endregion #region 매핑 모드 콤보 박스 선택 변경시 처리하기 - mappingModeComboBox_SelectionChanged(sender, e) /// <summary> /// 매핑 모드 콤보 박스 선택 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void mappingModeComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { this.radialGradientBrush.MappingMode = Enum.Parse<BrushMappingMode>(this.mappingModeComboBox.SelectedValue.ToString()); InitializeSlider(); } #endregion #region 슬라이더 값 변경시 처리하기 - slider_ValueChanged(sender, e) /// <summary> /// 슬라이더 값 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void slider_ValueChanged(object sender, RangeBaseValueChangedEventArgs e) { this.radialGradientBrush.Center = new Point(this.centerXSlider.Value, this.centerYSlider.Value); this.radialGradientBrush.RadiusX = this.radiusXSlider.Value; this.radialGradientBrush.RadiusY = this.radiusYSlider.Value; this.radialGradientBrush.GradientOrigin = new Point(this.gradientOriginXSlider.Value, this.gradientOriginYSlider.Value); } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 슬라이더 초기화하기 - InitializeSlider() /// <summary> /// 슬라이더 초기화하기 /// </summary> private void InitializeSlider() { Size rectangleSize = this.rectangle.ActualSize.ToSize(); if(this.radialGradientBrush.MappingMode == BrushMappingMode.Absolute) { double width = rectangleSize.Width; double halfWidth = rectangleSize.Width / 2; double stepFrequencyX = rectangleSize.Width / 50; double stepFrequencyY = rectangleSize.Height / 50; this.centerXSlider.Maximum = width; this.centerXSlider.Value = halfWidth; this.centerXSlider.StepFrequency = stepFrequencyX; this.centerYSlider.Maximum = width; this.centerYSlider.Value = halfWidth; this.centerYSlider.StepFrequency = stepFrequencyY; this.radiusXSlider.Maximum = width; this.radiusXSlider.Value = halfWidth; this.radiusXSlider.StepFrequency = stepFrequencyX; this.radiusYSlider.Maximum = width; this.radiusYSlider.Value = halfWidth; this.radiusYSlider.StepFrequency = stepFrequencyY; this.gradientOriginXSlider.Maximum = width; this.gradientOriginXSlider.Value = halfWidth; this.gradientOriginXSlider.StepFrequency = stepFrequencyX; this.gradientOriginYSlider.Maximum = width; this.gradientOriginYSlider.Value = halfWidth; this.gradientOriginYSlider.StepFrequency = stepFrequencyY; } else { this.centerXSlider.Maximum = 1.0; this.centerXSlider.Value = 0.5; this.centerXSlider.StepFrequency = 0.02; this.centerYSlider.Maximum = 1.0; this.centerYSlider.Value = 0.5; this.centerYSlider.StepFrequency = 0.02; this.radiusXSlider.Maximum = 1.0; this.radiusXSlider.Value = 0.5; this.radiusXSlider.StepFrequency = 0.02; this.radiusYSlider.Maximum = 1.0; this.radiusYSlider.Value = 0.5; this.radiusYSlider.StepFrequency = 0.02; this.gradientOriginXSlider.Maximum = 1.0; this.gradientOriginXSlider.Value = 0.5; this.gradientOriginXSlider.StepFrequency = 0.02; this.gradientOriginYSlider.Maximum = 1.0; this.gradientOriginYSlider.Value = 0.5; this.gradientOriginYSlider.StepFrequency = 0.02; } } #endregion } } |