첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

■ FormattedText 클래스를 사용하는 방법을 보여준다.

TestProject.zip
0.01MB

▶ CustomElement.cs

using System.Globalization;
using System.Windows;
using System.Windows.Media;

namespace TestProject
{
    /// <summary>
    /// 커스텀 엘리먼트
    /// </summary>
    public class CustomElement : FrameworkElement
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 텍스트
        /// </summary>
        private readonly string text = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor";

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Protected

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

        /// <summary>
        /// 렌더링시 처리하기
        /// </summary>
        /// <param name="drawingContext">드로잉 컨텍스트</param>
        protected override void OnRender(DrawingContext drawingContext)
        {
            FormattedText formattedText = new FormattedText
            (
                this.text,
                CultureInfo.GetCultureInfo("en-us"),
                FlowDirection.LeftToRight,
                new Typeface("Verdana"),
                32,
                Brushes.Black
            );

            formattedText.MaxTextWidth  = ActualWidth  - 10;
            formattedText.MaxTextHeight = ActualHeight - 10;

            formattedText.SetFontSize(36 * (96.0 / 72.0), 0, 5);

            formattedText.SetFontWeight(FontWeights.Bold, 6, 11);

            formattedText.SetForegroundBrush
            (
                new LinearGradientBrush
                (
                    Colors.Orange,
                    Colors.Teal,
                    90.0
                ),
                6,
                11
            );

            formattedText.SetFontStyle(FontStyles.Italic, 28, 28);

            drawingContext.DrawText(formattedText, new Point(10, 10));
        }

        #endregion
    }
}

 

▶ 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="TestProject"
    FontFamily="나눔고딕코딩"
    FontSize="16">
    <Border
        HorizontalAlignment="Center"
        VerticalAlignment="Center"
        BorderThickness="1"
        BorderBrush="Black"
        Width="400"
        Height="400">
        <local:CustomElement />
    </Border>
</Window>
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요