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

TestProject.zip
다운로드

▶ TestElement.cs

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

namespace TestProject
{
    /// <summary>
    /// 테스트 엘리먼트
    /// </summary>
    public class TestElement : FrameworkElement
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Protected

        #region 측정하기 (오버라이딩) - MeasureOverride(availableSize)

        /// <summary>
        /// 측정하기 (오버라이딩)
        /// </summary>
        /// <param name="availableSize">이용 가능한 크기</param>
        /// <returns>희망 크기</returns>
        protected override Size MeasureOverride(Size availableSize)
        {
            return base.MeasureOverride(availableSize);
        }

        #endregion
        #region 배열하기 (오버라이딩) - ArrangeOverride(finalSize)

        /// <summary>
        /// 배열하기 (오버라이딩)
        /// </summary>
        /// <param name="finalSize">최종 크기</param>
        /// <returns>최종 크기</returns>
        protected override Size ArrangeOverride(Size finalSize)
        {
            return base.ArrangeOverride(finalSize);
        }

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

        /// <summary>
        /// 렌더링시 처리하기
        /// </summary>
        /// <param name="drawingContext">드로잉 컨텍스트</param>
        protected override void OnRender(DrawingContext drawingContext)
        {
            string text = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor";

            FormattedText formattedText = new FormattedText
            (
                text,
                CultureInfo.GetCultureInfo("ko-kr"),
                FlowDirection.LeftToRight,
                new Typeface("나눔고딕코딩"),
                32,
                Brushes.Black
            );

            formattedText.MaxTextWidth  = 400;
            formattedText.MaxTextHeight = 400;

            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, 0));
        }

        #endregion
    }
}

 

728x90

 

▶ 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 클래스 : SetForegroundBrush//SetFontWeight/SetFontStyle/SetFontSize 메소드 사용하기"
    FontFamily="나눔고딕코딩"
    FontSize="16">
    <Grid Margin="10">
        <Canvas Name="canvas"
            Background="White" />
    </Grid>
</Window>

 

300x250

 

▶ MainWindow.xaml.cs

using System.Windows;

namespace TestProject
{
    /// <summary>
    /// 메인 윈도우
    /// </summary>
    public partial class MainWindow : Window
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - MainWindow()

        /// <summary>
        /// 생성자
        /// </summary>
        public MainWindow()
        {
            InitializeComponent();

            Loaded += Window_Loaded;
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region 윈도우 로드시 처리하기 - Window_Loaded(sender, e)

        /// <summary>
        /// 윈도우 로드시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            TestElement testElement = new TestElement();

            this.canvas.Children.Add(testElement);
        }

        #endregion
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요