첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
본 블로그는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 블로그 콘텐츠 향상을 위해 쓰여집니다.

728x90
반응형
728x170

TestProject.zip
0.02MB

▶ MainPage.xaml

<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>

 

728x90

 

▶ MainPage.xaml.cs

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
    }
}
728x90
반응형
그리드형
Posted by 사용자 icodebroker

댓글을 달아 주세요