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

■ 외곽선 텍스트 그리기

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


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="600"

    Height="450"

    Title="외곽선 텍스트 그리기"

    Background="Transparent">

    <Grid Name="mainGrid">

    </Grid>

</Window>

 

 

MainWindow.xaml.cs

 

 

using System.Globalization;

using System.Windows;

using System.Windows.Media;

 

namespace TestProject

{

    /// <summary>

    /// 메인 윈도우

    /// </summary>

    public partial class MainWindow : Window

    {

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

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

 

        #region 생성자 - MainWindow()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainWindow()

        {

            InitializeComponent();

        }

 

        #endregion

 

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

        ////////////////////////////////////////////////////////////////////////////////////////// Protected

 

        #region 렌더링시 처리하기 - OnRender(drawingContext)

 

        /// <summary>

        /// 렌더링시 처리하기

        /// </summary>

        /// <param name="drawingContext">그리기 객체</param>

        protected override void OnRender(DrawingContext drawingContext)

        {

            Rect backgroundRectangle = new Rect(0, 0, ActualWidth, ActualHeight);

 

            drawingContext.DrawRectangle(Brushes.White, null, backgroundRectangle);

 

            const double FONT_SIZE = 70;

 

            FontFamily fontFamily = new FontFamily("Segoe");

            Typeface   typeface   = new Typeface("Segoe");

 

            FormattedText formattedText = new FormattedText

            (

                "TEST STRING",

                new CultureInfo("en-us"),

                FlowDirection.LeftToRight,

                typeface,

                FONT_SIZE,

                Brushes.Black

            );

 

            formattedText.SetFontWeight(FontWeights.Bold);

 

            formattedText.TextAlignment = TextAlignment.Center;

 

            Point originPoint = new Point

            (

                this.mainGrid.ActualWidth / 2,

                (this.mainGrid.ActualHeight - formattedText.Height) / 2

            );

 

            Geometry geometry = formattedText.BuildGeometry(originPoint);

 

            Pen pen = new Pen(Brushes.Red, 2);

 

            drawingContext.DrawGeometry(Brushes.Yellow, pen, geometry);

        }

 

        #endregion

    }

}

 

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

Posted by 사용자 icodebroker

댓글을 달아 주세요