■ 외곽선 텍스트 사용하기

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


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"

    Width="800"

    Height="600"

    Title="외곽선 텍스트 사용하기"

    FontFamily="나눔고딕코딩"

    FontSize="16">

    <Grid>

        <Label Name="label"

            HorizontalAlignment="Center"

            VerticalAlignment="Center"

            FontSize="48"

            FontWeight="Bold"

            Foreground="White"

            Content="외곽선 텍스트 입니다." />

    </Grid>

</Window>

 

 

MainWindow.xaml.cs

 

 

using System.Windows;

using System.Windows.Controls;

using System.Windows.Media;

using System.Windows.Shapes;

 

namespace TestProject

{

    /// <summary>

    /// 메인 윈도우

    /// </summary>

    public partial class MainWindow : Window

    {

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

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

 

        #region 생성자 - MainWindow()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainWindow()

        {

            InitializeComponent();

 

            ApplyOutlineText(this.label, Brushes.Gray);

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Private

 

        #region 윤곽선 텍스트 적용하기 - ApplyOutlineText(label, outlineBrush)

 

        /// <summary>

        /// 윤곽선 텍스트 적용하기

        /// </summary>

        /// <param name="label">레이블</param>

        /// <param name="outlineBrush">외곽선 브러시</param>

        private void ApplyOutlineText(Label label, Brush outlineBrush)

        {

            FormattedText formattedText = new FormattedText

            (

                label.Content.ToString(),

                System.Threading.Thread.CurrentThread.CurrentCulture,

                FlowDirection.LeftToRight,

                new Typeface(label.FontFamily, label.FontStyle, label.FontWeight, label.FontStretch),

                label.FontSize,

                Brushes.Black

            );

 

            formattedText.TextAlignment = TextAlignment.Center;

 

            Geometry geometry = formattedText.BuildGeometry(new Point(0, 0));

 

            PathGeometry pathGeometry = geometry.GetFlattenedPathGeometry();

 

            Path path = new Path();

 

            path.Stroke          = outlineBrush;

            path.StrokeThickness = 2;

            path.Fill            = label.Foreground;

            path.Stretch         = Stretch.Fill;

 

            Canvas canvas = new Canvas();

 

            canvas.Children.Add(path);

 

            label.Content = canvas;

 

            Canvas.SetTop (path, (canvas.ActualHeight - formattedText.Extent) / 2);

            Canvas.SetLeft(path, (canvas.ActualWidth  - formattedText.Width ) / 2);

 

            path.Data = pathGeometry;

        }

 

        #endregion

    }

}

 

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

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요