첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
유용한 소스 코드가 있으면 icodebroker@naver.com으로 보내주시면 감사합니다.
블로그 자료는 자유롭게 사용하세요.

■ LinearGradientBrush 클래스 : 속성 값 변경하기

------------------------------------------------------------------------------------------------------------------------


TestProject.zip



MainWindow.xaml

 

 

<Window x:Class="TestProject.MainWindow"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:local="clr-namespace:TestProject"

    Width="800"

    Height="600"

    Title="LinearGradientBrush 클래스 : 속성 값 변경하기"

    FontFamily="나눔고딕코딩"

    FontSize="16">

    <Window.Resources>

        <Style x:Key="MarkerEllipseStyleKey" TargetType="{x:Type Ellipse}">

            <Setter Property="Width" Value="20" />

            <Setter Property="Height" Value="20" />

            <Setter Property="Stroke"          Value="Black" />

            <Setter Property="StrokeThickness" Value="1"     />

            <Setter Property="Fill">

                <Setter.Value>

                    <SolidColorBrush

                        Color="White"

                        Opacity="0.5" />

                </Setter.Value>

            </Setter>

            <Style.Triggers>

                <EventTrigger RoutedEvent="Ellipse.MouseEnter">

                    <BeginStoryboard Name="ellipseMouseEnterBeginStoryboard">

                        <Storyboard>

                            <ColorAnimation

                                Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)"

                                AutoReverse="True"

                                RepeatBehavior="Forever"

                                From="Black"

                                To="White"

                                Duration="0:0:1" />

                            <ColorAnimation

                                Storyboard.TargetProperty="(Shape.Stroke).(SolidColorBrush.Color)"

                                AutoReverse="True"

                                RepeatBehavior="Forever"

                                From="White"

                                To="Black"

                                Duration="0:0:1" />

                        </Storyboard>

                    </BeginStoryboard>

                </EventTrigger>

                <EventTrigger RoutedEvent="Ellipse.MouseLeave">

                    <RemoveStoryboard BeginStoryboardName="ellipseMouseEnterBeginStoryboard" />

                </EventTrigger>

            </Style.Triggers>

        </Style>

        <Style x:Key="EntryLabelStyleKey" TargetType="{x:Type TextBlock}">

          <Setter Property="VerticalAlignment"   Value="Center" />

          <Setter Property="HorizontalAlignment" Value="Right"   />

          <Setter Property="Margin"              Value="0 0 5 0" />

        </Style>

        <local:EnumerationValuesToStringArrayConverter x:Key="EnumerationValuesToStringArrayConverterKey" />

        <local:DoubleToStringConverter x:Key="DoubleToStringConverterKey" />

    </Window.Resources>

    <Grid>

        <Grid.RowDefinitions>

            <RowDefinition Height="Auto" />

            <RowDefinition Height="Auto" />

            <RowDefinition Height="Auto" />

            <RowDefinition Height="Auto" />

            <RowDefinition Height="Auto" />

            <RowDefinition Height="*"    />

        </Grid.RowDefinitions>

        <TextBlock Grid.Row="0"

            Margin="10"

            FontWeight="Bold"

            Text="브러시 설정" />

        <Border Grid.Row="1"

            Margin="10 0 10 0"

            Padding="10"

            Background="LightGray">

            <Grid>

                <Grid.ColumnDefinitions>

                    <ColumnDefinition Width="Auto" />

                    <ColumnDefinition Width="Auto" />

                    <ColumnDefinition Width="10"   />

                    <ColumnDefinition Width="Auto" />

                    <ColumnDefinition Width="Auto" />

                </Grid.ColumnDefinitions>

                <Grid.RowDefinitions>

                    <RowDefinition Height="Auto" />

                    <RowDefinition Height="5"    />

                    <RowDefinition Height="Auto" />

                    <RowDefinition Height="5"    />

                    <RowDefinition Height="Auto" />

                    <RowDefinition Height="5"    />

                    <RowDefinition Height="Auto" />

                </Grid.RowDefinitions>

                <TextBlock Grid.Row="0" Grid.Column="0"

                    Style="{StaticResource EntryLabelStyleKey}"

                    Text="시작점" />

                <TextBox Name="startPointTextBox" Grid.Row="0" Grid.Column="1"

                    Text="0.0000,0.0000" />

                <TextBlock Grid.Row="0" Grid.Column="3"

                    Style="{StaticResource EntryLabelStyleKey}"

                    Text="종료점" />

                <TextBox Name="endPointTextBox" Grid.Row="0" Grid.Column="4"

                    Text="1.0000, 1.0000" />

                <TextBlock Grid.Row="2" Grid.Column="0"

                    Style="{StaticResource EntryLabelStyleKey}"

                    Text="매핑 모드" />

                <ComboBox Name="mappingModeComboBox" Grid.Row="2" Grid.Column="1"

                    Padding="5"

                    ItemsSource="{Binding ElementName='linearGradientBrush',

                        Path='MappingMode',

                        Converter={StaticResource EnumerationValuesToStringArrayConverterKey}}"

                    SelectedItem="{Binding ElementName='linearGradientBrush', Path='MappingMode'}"

                    SelectedIndex="1" />

                <TextBlock Grid.Row="2" Grid.Column="3"

                    Style="{StaticResource EntryLabelStyleKey}"

                    Text="스프레드 모드" />

                <ComboBox Grid.Row="2" Grid.Column="4"

                    Padding="5"

                    ItemsSource="{Binding ElementName='linearGradientBrush',

                        Path='SpreadMethod',

                        Converter={StaticResource EnumerationValuesToStringArrayConverterKey}}"

                    SelectedItem="{Binding ElementName='linearGradientBrush', Path='SpreadMethod'}"

                    SelectedIndex="0" />

                <TextBlock Grid.Row="4" Grid.Column="0"

                    Style="{StaticResource EntryLabelStyleKey}"

                    Text="색상 보간 모드" />

                <ComboBox Grid.Row="4" Grid.Column="1"

                    Padding="5"

                    ItemsSource="{Binding ElementName='linearGradientBrush',

                        Path='ColorInterpolationMode',

                        Converter={StaticResource EnumerationValuesToStringArrayConverterKey}}"

                    SelectedItem="{Binding ElementName='linearGradientBrush', Path='ColorInterpolationMode'}"

                    SelectedIndex="1" />

                <TextBlock Grid.Row="6" Grid.Column="0"

                    Style="{StaticResource EntryLabelStyleKey}"

                    Text="불투명도" />

                <Slider Grid.Row="6" Grid.Column="1" Grid.ColumnSpan="4"

                    Minimum="0"

                    Maximum="1"

                    Value="{Binding ElementName='linearGradientBrush', Path='Opacity'}" />

            </Grid>

        </Border>

        <TextBlock Grid.Row="2"

            Margin="10"

            FontWeight="Bold"

            Text="그라디언트 중단" />

        <Border Grid.Row="3"

            Margin="10 0 10 0"

            Padding="10"

            Background="LightGray">

            <Grid>

                <Grid.ColumnDefinitions>

                    <ColumnDefinition Width="Auto" />

                    <ColumnDefinition Width="Auto" />

                    <ColumnDefinition Width="Auto" />

                    <ColumnDefinition Width="5"    />

                    <ColumnDefinition Width="Auto" />

                    <ColumnDefinition Width="Auto" />

                    <ColumnDefinition Width="Auto" />

                    <ColumnDefinition Width="Auto" />

                </Grid.ColumnDefinitions>

                <Grid.RowDefinitions>

                    <RowDefinition Height="Auto" />

                    <RowDefinition Height="5"    />

                    <RowDefinition Height="Auto" />

                    <RowDefinition Height="5"    />

                    <RowDefinition Height="Auto" />

                </Grid.RowDefinitions>

                <TextBlock Grid.Row="0" Grid.Column="0"

                    Style="{StaticResource EntryLabelStyleKey}"

                    FontWeight="Bold"

                    Text="중단 1 :" />

                <TextBlock Grid.Row="0" Grid.Column="1"

                    Style="{StaticResource EntryLabelStyleKey}"

                    Text="색상" />

                <TextBox Grid.Row="0" Grid.Column="2"

                    Text="{Binding ElementName='GradientStop1', Path='Color'}" />

                <TextBlock Grid.Row="0" Grid.Column="4"

                    Style="{StaticResource EntryLabelStyleKey}"

                    Text="오프셋" />

                <Slider Grid.Row="0" Grid.Column="5"

                    Width="100"

                    Minimum="0"

                    Maximum="1"

                    Value="{Binding ElementName='GradientStop1', Path='Offset'}" />

                <TextBlock Grid.Row="0" Grid.Column="6"

                    Style="{StaticResource EntryLabelStyleKey}"

                    Margin="20 0 0 0"

                    Text="{Binding ElementName='GradientStop1',

                        Path='Offset',

                        Converter='{StaticResource DoubleToStringConverterKey}'}" />

                <TextBlock Grid.Row="2" Grid.Column="0"

                    Style="{StaticResource EntryLabelStyleKey}"

                    FontWeight="Bold"

                    Text="중단 2 :" />

                <TextBlock Grid.Row="2" Grid.Column="1"

                    Style="{StaticResource EntryLabelStyleKey}"

                    Text="색상" />

                <TextBox Grid.Row="2" Grid.Column="2"

                    Text="{Binding ElementName='GradientStop2', Path='Color'}" />

                <TextBlock Grid.Row="2" Grid.Column="4"

                    Style="{StaticResource EntryLabelStyleKey}"

                    Text="오프셋" />

                <Slider Grid.Row="2" Grid.Column="5"

                    Minimum="0"

                    Maximum="1"

                    Width="100"

                    Value="{Binding ElementName='GradientStop2', Path='Offset'}" />

                <TextBlock Grid.Row="2" Grid.Column="6"

                    Style="{StaticResource EntryLabelStyleKey}"

                    Margin="20 0 0 0"

                    Text="{Binding ElementName='GradientStop2',

                        Path='Offset',

                        Converter='{StaticResource DoubleToStringConverterKey}'}" />

                <TextBlock Grid.Row="4" Grid.Column="0"

                    Style="{StaticResource EntryLabelStyleKey}"

                    FontWeight="Bold"

                    Text="중단 3 :" />

                <TextBlock Grid.Row="4" Grid.Column="1"

                    Style="{StaticResource EntryLabelStyleKey}"

                    Text="색상" />

                <TextBox Grid.Row="4" Grid.Column="2"

                    Text="{Binding ElementName='GradientStop3', Path='Color'}" />

                <TextBlock Grid.Row="4" Grid.Column="4"

                    Style="{StaticResource EntryLabelStyleKey}"

                    Text="오프셋" />

                <Slider Grid.Row="4" Grid.Column="5"

                    Width="100"

                    Minimum="0"

                    Maximum="1"

                    Value="{Binding ElementName='GradientStop3', Path='Offset'}" />

                <TextBlock Grid.Row="4" Grid.Column="6"

                    Style="{StaticResource EntryLabelStyleKey}"

                    Margin="20 0 0 0"

                    Text="{Binding ElementName='GradientStop3',

                       Path='Offset',

                       Converter='{StaticResource DoubleToStringConverterKey}'}" />

            </Grid>

        </Border>

        <Expander Grid.Row="4"

            Header="마크업"

            Margin="10"

            BorderBrush="Black"

            BorderThickness="1">

            <Border

                BorderBrush="Gray"

                BorderThickness="1"

                Padding="5"

                Background="LightGray">

                <TextBlock Name="markupOutputTextBlock" />

            </Border>

        </Expander>

        <Border Name="gradientDisplayBorder" Grid.Row="5"

            Width="250"

            Height="180">

            <Border.Background>

                <LinearGradientBrush x:Name="linearGradientBrush">

                    <GradientStop x:Name="GradientStop1" Offset="0"   Color="Blue"   />

                    <GradientStop x:Name="GradientStop2" Offset="0.5" Color="Purple" />

                    <GradientStop x:Name="GradientStop3" Offset="1"   Color="Red"    />

                </LinearGradientBrush>

            </Border.Background>

            <Canvas

                HorizontalAlignment="Stretch"

                VerticalAlignment="Stretch">

                <Line

                    Stroke="Black"

                    StrokeThickness="3"

                    StrokeDashArray="2 1"

                    X1="{Binding ElementName=startPointMarkerTranslateTransform, Path=X}"

                    Y1="{Binding ElementName=startPointMarkerTranslateTransform, Path=Y}"

                    X2="{Binding ElementName=endPointMarkerTranslateTransform , Path=X}"

                    Y2="{Binding ElementName=endPointMarkerTranslateTransform , Path=Y}" />

                <Ellipse Name="startPointMarkerEllipse" Canvas.Left="-10" Canvas.Top="-10"

                    Style="{StaticResource MarkerEllipseStyleKey}">

                    <Ellipse.RenderTransform>

                        <TranslateTransform x:Name="startPointMarkerTranslateTransform"

                            X="0"

                            Y="0" />

                    </Ellipse.RenderTransform>

                </Ellipse>

                <Ellipse Name="endPointMarkerEllipse" Canvas.Left="-10" Canvas.Top="-10"

                    Style="{StaticResource MarkerEllipseStyleKey}">

                    <Ellipse.RenderTransform>

                        <TranslateTransform x:Name="endPointMarkerTranslateTransform"

                            X="0"

                            Y="0" />

                    </Ellipse.RenderTransform>

                </Ellipse>

                <Label Content="시작점">

                    <Label.RenderTransform>

                        <TranslateTransform

                            X="{Binding ElementName=startPointMarkerTranslateTransform, Path=X}"

                            Y="{Binding ElementName=startPointMarkerTranslateTransform, Path=Y}" />

                    </Label.RenderTransform>

                </Label>

                <Label Content="종료점">

                    <Label.RenderTransform>

                        <TranslateTransform

                            X="{Binding ElementName=endPointMarkerTranslateTransform, Path=X}"

                            Y="{Binding ElementName=endPointMarkerTranslateTransform, Path=Y}" />

                    </Label.RenderTransform>

                </Label>

            </Canvas>

        </Border>

    </Grid>

</Window>

 

 

MainWindow.xaml.cs

 

 

using System;

using System.Text;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Shapes;

 

namespace TestProject

{

    /// <summary>

    /// 메인 윈도우

    /// </summary>

    public partial class MainWindow : Window

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field

        ////////////////////////////////////////////////////////////////////////////////////////// Static

        //////////////////////////////////////////////////////////////////////////////// Public

 

        #region 선택 마커 속성 - SelectedMarkerProperty

 

        /// <summary>

        /// 선택 마커 속성

        /// </summary>

        public static readonly DependencyProperty SelectedMarkerProperty = DependencyProperty.Register

        (

            "SelectedMarker",

            typeof(Shape),

            typeof(MainWindow),

            new PropertyMetadata(null)

        );

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 생성자 - MainWindow()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainWindow()

        {

            InitializeComponent();

 

            Loaded += Window_Loaded;

        }

 

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

        {

            this.startPointTextBox.KeyUp                   += startPointTextBox_KeyUp;

            this.endPointTextBox.KeyUp                     += endPointTextBox_KeyUp;

            this.mappingModeComboBox.SelectionChanged      += mappingModeComboBox_SelectionChanged;

            this.gradientDisplayBorder.SizeChanged         += gradientDisplayBorder_SizeChanged;

            this.gradientDisplayBorder.MouseLeftButtonDown += gradientDisplayBorder_MouseLeftButtonDown;

            this.gradientDisplayBorder.MouseMove           += gradientDisplayBorder_MouseMove;

            this.gradientDisplayBorder.MouseLeftButtonUp   += gradientDisplayBorder_MouseLeftButtonUp;

            this.linearGradientBrush.Changed               += linearGradientBrushChanged;

 

            startPointTextBox_KeyUp(this.startPointTextBox, null);

 

            endPointTextBox_KeyUp(this.endPointTextBox, null);

 

            linearGradientBrushChanged(this.linearGradientBrush, null);

        }

 

        #endregion

        #region 시작점 텍스트 박스 키 UP 처리하기 - startPointTextBox_KeyUp(sender, e)

 

        /// <summary>

        /// 시작점 텍스트 박스 키 UP 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void startPointTextBox_KeyUp(object sender, KeyEventArgs e)

        {

            TextBox textBox = sender as TextBox;

 

            try

            {

                Point point = Point.Parse(textBox.Text);

 

                if(this.linearGradientBrush.MappingMode == BrushMappingMode.RelativeToBoundingBox)

                {

                    this.startPointMarkerTranslateTransform.X = point.X * this.gradientDisplayBorder.ActualWidth;

                    this.startPointMarkerTranslateTransform.Y = point.Y * this.gradientDisplayBorder.ActualHeight;

                }

                else

                {

                    this.startPointMarkerTranslateTransform.X = point.X;

                    this.startPointMarkerTranslateTransform.Y = point.Y;

                }

            }

            catch(InvalidOperationException)

            {

            }

            catch(FormatException)

            {

            }

        }

 

        #endregion

        #region 중단점 텍스트 박스 키 UP 처리하기 - endPointTextBox_KeyUp(sender, e)

 

        /// <summary>

        /// 중단점 텍스트 박스 키 UP 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void endPointTextBox_KeyUp(object sender, KeyEventArgs e)

        {

            TextBox textBox = sender as TextBox;

 

            try

            {

                Point point = Point.Parse(textBox.Text);

 

                if(this.linearGradientBrush.MappingMode == BrushMappingMode.RelativeToBoundingBox)

                {

                    this.endPointMarkerTranslateTransform.X = point.X * this.gradientDisplayBorder.ActualWidth;

                    this.endPointMarkerTranslateTransform.Y = point.Y * this.gradientDisplayBorder.ActualHeight;

                }

                else

                {

                    this.endPointMarkerTranslateTransform.X = point.X;

                    this.endPointMarkerTranslateTransform.Y = point.Y;

                }

            }

            catch(InvalidOperationException)

            {

            }

            catch(FormatException)

            {

            }

        }

 

        #endregion

        #region 매핑 코드 콤보 박스 선택 변경시 처리하기 - mappingModeComboBox_SelectionChanged(sender, e)

 

        /// <summary>

        /// 매핑 코드 콤보 박스 선택 변경시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void mappingModeComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)

        {

            Point previousStartPoint = this.linearGradientBrush.StartPoint;

            Point newStartPoint      = new Point();

            Point previousEndPoint   = this.linearGradientBrush.EndPoint;

            Point newEndPoint        = new Point();

 

            if(this.linearGradientBrush.MappingMode == BrushMappingMode.RelativeToBoundingBox)

            {

                newStartPoint.X = previousStartPoint.X / this.gradientDisplayBorder.ActualWidth;

                newStartPoint.Y = previousStartPoint.Y / this.gradientDisplayBorder.ActualHeight;

 

                this.linearGradientBrush.StartPoint = newStartPoint;

 

                newEndPoint.X = previousEndPoint.X / this.gradientDisplayBorder.ActualWidth;

                newEndPoint.Y = previousEndPoint.Y / this.gradientDisplayBorder.ActualHeight;

 

                this.linearGradientBrush.EndPoint = newEndPoint;

            }

            else

            {

                newStartPoint.X = previousStartPoint.X * this.gradientDisplayBorder.ActualWidth;

                newStartPoint.Y = previousStartPoint.Y * this.gradientDisplayBorder.ActualHeight;

 

                this.linearGradientBrush.StartPoint = newStartPoint;

 

                newEndPoint.X = previousEndPoint.X * this.gradientDisplayBorder.ActualWidth;

                newEndPoint.Y = previousEndPoint.Y * this.gradientDisplayBorder.ActualHeight;

 

                this.linearGradientBrush.EndPoint = newEndPoint;

            }

 

            this.startPointTextBox.Text = newStartPoint.X.ToString("F4") + "," + newStartPoint.Y.ToString("F4");

            this.endPointTextBox.Text   = newEndPoint.X.ToString("F4")   + "," + newEndPoint.Y.ToString("F4");

        }

 

        #endregion

        #region 그라디언트 표시 보더 크기 변경시 처리하기 - gradientDisplayBorder_SizeChanged(sender, e)

 

        /// <summary>

        /// 그라디언트 표시 보더 크기 변경시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void gradientDisplayBorder_SizeChanged(object sender, SizeChangedEventArgs e)

        {

            if(this.linearGradientBrush.MappingMode == BrushMappingMode.RelativeToBoundingBox)

            {

                this.startPointMarkerTranslateTransform.X = this.linearGradientBrush.StartPoint.X * e.NewSize.Width;

                this.startPointMarkerTranslateTransform.Y = this.linearGradientBrush.StartPoint.Y * e.NewSize.Height;

 

                this.endPointMarkerTranslateTransform.X = this.linearGradientBrush.EndPoint.X * e.NewSize.Width;

                this.endPointMarkerTranslateTransform.Y = this.linearGradientBrush.EndPoint.Y * e.NewSize.Height;

            }

        }

 

        #endregion

        #region 그라디언트 표시 보더 마우스 왼쪽 버튼 DOWN 처리하기 - gradientDisplayBorder_MouseLeftButtonDown(sender, e)

 

        /// <summary>

        /// 그라디언트 표시 보더 마우스 왼쪽 버튼 DOWN 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void gradientDisplayBorder_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)

        {

            if(e.OriginalSource is Shape)

            {

                SetValue(SelectedMarkerProperty, e.OriginalSource as Shape);

            }

            else

            {

                SetValue(SelectedMarkerProperty, null);

            }

        }

 

        #endregion

        #region 그라디언트 표시 보더 마우스 이동시 처리하기 - gradientDisplayBorder_MouseMove(sender, e)

 

        /// <summary>

        /// 그라디언트 표시 보더 마우스 이동시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void gradientDisplayBorder_MouseMove(object sender, MouseEventArgs e)

        {

            Point mousePoint = e.GetPosition(this.gradientDisplayBorder);

 

            Shape shape = (Shape)GetValue(SelectedMarkerProperty);

            

            if(shape == this.endPointMarkerEllipse || shape == this.startPointMarkerEllipse)

            {

                TranslateTransform translationTransform = (TranslateTransform)shape.RenderTransform;

 

                translationTransform.X = mousePoint.X;

                translationTransform.Y = mousePoint.Y;

 

                Mouse.Synchronize();

 

                Point point;

 

                if(this.linearGradientBrush.MappingMode == BrushMappingMode.RelativeToBoundingBox)

                {

                    point = new Point

                    (

                        mousePoint.X / gradientDisplayBorder.ActualWidth,

                        mousePoint.Y / gradientDisplayBorder.ActualHeight

                    );

                }

                else

                {

                    point = mousePoint;

                }

 

                if(shape == this.startPointMarkerEllipse)

                {

                    this.linearGradientBrush.StartPoint = point;

 

                    this.startPointTextBox.Text = point.X.ToString("F4") + "," + point.Y.ToString("F4");

                }

                else

                {

                    this.linearGradientBrush.EndPoint = point;

 

                    this.endPointTextBox.Text = point.X.ToString("F4") + "," + point.Y.ToString("F4");

                }

            }

        }

 

        #endregion

        #region 그라디언트 표시 보더 마우스 왼쪽 버튼 UP 처리하기 - gradientDisplayBorder_MouseLeftButtonUp(sender, e)

 

        /// <summary>

        /// 그라디언트 표시 보더 마우스 왼쪽 버튼 UP 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void gradientDisplayBorder_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)

        {

            Point mousePoint = e.GetPosition(this.gradientDisplayBorder);

 

            Shape shape = (Shape)GetValue(SelectedMarkerProperty);

 

            if(shape == this.endPointMarkerEllipse || shape == this.startPointMarkerEllipse)

            {

                TranslateTransform translationTransform = shape.RenderTransform as TranslateTransform;

 

                translationTransform.X = mousePoint.X;

                translationTransform.Y = mousePoint.Y;

 

                SetValue(SelectedMarkerProperty, null);

 

                Mouse.Synchronize();

 

                Point point;

 

                if(this.linearGradientBrush.MappingMode == BrushMappingMode.RelativeToBoundingBox)

                {

                    point = new Point

                    (

                        mousePoint.X / gradientDisplayBorder.ActualWidth,

                        mousePoint.Y / gradientDisplayBorder.ActualHeight

                    );

                }

                else

                {

                    point = mousePoint;

                }

 

                if(shape == this.startPointMarkerEllipse)

                {

                    this.linearGradientBrush.StartPoint = point;

 

                    this.startPointTextBox.Text = point.X.ToString("F4") + "," + point.Y.ToString("F4");

                }

                else

                {

                    this.linearGradientBrush.EndPoint = point;

 

                    this.endPointTextBox.Text = point.X.ToString("F4") + "," + point.Y.ToString("F4");

                }

            }

        }

 

        #endregion

        #region 선형 그라디언트 브러시 변경시 처리하기 - linearGradientBrushChanged(sender, e)

 

        /// <summary>

        /// 선형 그라디언트 브러시 변경시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void linearGradientBrushChanged(object sender, EventArgs e)

        {

            if(this.gradientDisplayBorder != null)

            {

                this.markupOutputTextBlock.Text = GetLinearGradientBrushMarkup(this.linearGradientBrush);

            }

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////// Function

 

        #region 선형 그라디언트 브러시 마크업 구하기 - GetLinearGradientBrushMarkup(brush)

 

        /// <summary>

        /// 선형 그라디언트 브러시 마크업 구하기

        /// </summary>

        /// <param name="brush">브러시</param>

        /// <returns>선형 그라디언트 브러시 마크업</returns>

        private static string GetLinearGradientBrushMarkup(LinearGradientBrush brush)

        {

            StringBuilder stringBuilder = new StringBuilder();

 

            stringBuilder.AppendLine($"<LinearGradientBrush");

            stringBuilder.AppendLine($"    StartPoint=\"{brush.StartPoint}\"");

            stringBuilder.AppendLine($"    EndPoint=\"{brush.EndPoint}\"");

            stringBuilder.AppendLine($"    MappingMode=\"{brush.MappingMode}\"");

            stringBuilder.AppendLine($"    SpreadMethod=\"{brush.SpreadMethod}\"");

            stringBuilder.AppendLine($"    ColorInterpolationMode=\"{brush.ColorInterpolationMode}\"");

            stringBuilder.AppendLine($"    Opacity=\"{brush.Opacity}\">");

 

            foreach(GradientStop stop in brush.GradientStops)

            {

                stringBuilder.AppendLine($"    <GradientStop Offset=\"{stop.Offset.ToString("F4")}\" Color=\"{stop.Color}\" />");

            }

 

            stringBuilder.Append("</LinearGradientBrush>");

 

            return stringBuilder.ToString();

        }

 

        #endregion

    }

}

 

------------------------------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker

댓글을 달아 주세요