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

728x90
반응형

■ FormattedText 클래스 : DPI 인식 텍스트 그리기

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


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="FormattedText 클래스 : DPI 인식 텍스트 그리기"

    Background="Black"

    FontFamily="나눔고딕코딩"

    FontSize="16">

    <Viewbox Stretch="Uniform">

        <Canvas Name="canvas"

            Width="400"

            Height="300"

            Background="Black">

            <Path Name="path" Canvas.Left="0" Canvas.Top="0"

                Stroke="Maroon"

                Fill="Transparent" />

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

                Width="20"

                Height="20">

                <Ellipse.Fill>

                    <RadialGradientBrush>

                        <RadialGradientBrush.GradientStops>

                            <GradientStop Offset="0" Color="#ffff" />

                            <GradientStop Offset="1" Color="#0fff" />

                        </RadialGradientBrush.GradientStops>

                    </RadialGradientBrush>

                </Ellipse.Fill>

                <Ellipse.RenderTransform>

                    <MatrixTransform />

                </Ellipse.RenderTransform>

                <Ellipse.Triggers>

                    <EventTrigger RoutedEvent="FrameworkElement.Loaded">

                        <EventTrigger.Actions>

                            <BeginStoryboard>

                                <Storyboard Name="storyboard">

                                    <MatrixAnimationUsingPath Name="matrixAnimation"

                                        Duration="0:00:59"

                                        RepeatBehavior="Forever"

                                        Storyboard.TargetProperty="RenderTransform.Matrix" />

                                </Storyboard>

                            </BeginStoryboard>

                        </EventTrigger.Actions>

                    </EventTrigger>

                </Ellipse.Triggers>

            </Ellipse>

        </Canvas>

    </Viewbox>

</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();

 

            Loaded += Window_Loaded;

        }

 

        #endregion

 

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

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

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

 

        #region DPI 변경시 처리하기 - OnDpiChanged(oldDpiScale, newDpiScale)

 

        /// <summary>

        /// DPI 변경시 처리하기

        /// </summary>

        /// <param name="oldDpiScale">구 DPI 스케일</param>

        /// <param name="newDpiScale">신 DPI 스케일</param>

        protected override void OnDpiChanged(DpiScale oldDpiScale, DpiScale newDpiScale)

        {

            DrawText(newDpiScale);

        }

 

        #endregion

 

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

        {            

            DrawText(VisualTreeHelper.GetDpi(this));

        }

 

        #endregion

 

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

 

        #region 텍스트 그리기 - DrawText(dpiScale)

 

        /// <summary>

        /// 텍스트 그리기

        /// </summary>

        /// <param name="dpiScale">DPI 스케일</param>

        private void DrawText(DpiScale dpiScale)

        {

            FormattedText formattedText = new FormattedText

            (

                "SAMPLE",

                new CultureInfo("en-US"),

                FlowDirection.LeftToRight,

                new Typeface

                (

                    new FontFamily("Segoe UI"),

                    FontStyles.Normal,

                    FontWeights.Bold,

                    FontStretches.Normal

                ),

                120,

                Brushes.Red,

                dpiScale.PixelsPerDip

            );

 

            Geometry geometry = new PathGeometry();

 

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

 

            ScaleTransform scaleTransform = new ScaleTransform();

 

            scaleTransform.ScaleX = 0.85;

            scaleTransform.ScaleY = 2.0;

 

            geometry.Transform = scaleTransform;

 

            PathGeometry pathGeometry = new PathGeometry();

 

            pathGeometry = geometry.GetFlattenedPathGeometry();

 

            path.Data = pathGeometry;

 

            this.matrixAnimation.PathGeometry = pathGeometry;

        }

 

        #endregion

    }

}

 

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

728x90
반응형
Posted by 사용자 icodebroker
TAG , , , ,

댓글을 달아 주세요