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

■ DrawingBrush 클래스를 사용해 텍스트 드로잉 객체를 사용해 컨트롤 배경 브로시를 설정하는 방법을 보여준다.

TestProject.zip
0.01MB

▶ 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="TestProject"
    FontFamily="나눔고딕코딩"
    FontSize="16">
    <StackPanel
        HorizontalAlignment="Center"
        VerticalAlignment="Center"
        Orientation="Horizontal">
        <Label Name="label"
            Width="200"
            Height="50" />
        <Button Name="button"
            Margin="10 0 0 0"
            Width="200"
            Height="50" />
    </StackPanel>
</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
        ////////////////////////////////////////////////////////////////////////////////////////// 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)
        {
            this.label.Background  = new DrawingBrush(GetTextDrawing("Custom Label" ));
            this.button.Background = new DrawingBrush(GetTextDrawing("Custom Button"));
        }

        #endregion

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

        #region 텍스트 드로잉 구하기 - GetTextDrawing(text)

        /// <summary>
        /// 텍스트 드로잉 구하기
        /// </summary>
        /// <param name="text">텍스트</param>
        /// <returns>텍스트 드로잉</returns>
        private Drawing GetTextDrawing(string text)
        {
            DrawingGroup drawingGroup = new DrawingGroup();

            using(DrawingContext drawingContext = drawingGroup.Open())
            {
                FormattedText formattedText = new FormattedText
                (
                    text,
                    CultureInfo.GetCultureInfo("en-us"),
                    FlowDirection.LeftToRight,
                    new Typeface("Comic Sans MS Bold"),
                    48,
                    Brushes.Black
                );

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

                drawingContext.DrawRoundedRectangle
                (
                    Brushes.PapayaWhip,
                    null,
                    new Rect(new Size(formattedText.Width + 50, formattedText.Height + 5)),
                    5.0,
                    5.0
                );

                drawingContext.DrawGeometry
                (
                    Brushes.Gold,
                    new Pen(Brushes.Maroon, 1.5),
                    textGeometry
                );

                return drawingGroup;
            }
        }

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

댓글을 달아 주세요