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

■ DrawingContext 클래스 : PushClip 메소드를 사용해 클리핑 영역 설정하기

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


TestProject.zip


CustomEllipse.cs

 

 

using System.Globalization;

using System.Windows;

using System.Windows.Media;

 

namespace TestProject

{

    /// <summary>

    /// 커스텀 타원

    /// </summary>

    public class CustomEllipse : FrameworkElement

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field

        ////////////////////////////////////////////////////////////////////////////////////////// Private

 

        #region Field

 

        /// <summary>

        /// 포맷 텍스트

        /// </summary>

        private FormattedText formattedText;

 

        #endregion

 

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

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

 

        #region 생성자 - CustomEllipse()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public CustomEllipse()

        {

            this.formattedText = new FormattedText

            (

                "Hello, ellipse!",

                CultureInfo.CurrentCulture,

                FlowDirection,

                new Typeface("Times New Roman Italic"),

                48,

                Brushes.White

            );

        }

 

        #endregion

 

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

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

 

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

 

        /// <summary>

        /// 렌더링시 처리하기

        /// </summary>

        /// <param name="drawingContext">그리기 컨텍스트</param>

        protected override void OnRender(DrawingContext drawingContext)

        {

            drawingContext.DrawEllipse

            (

                Brushes.Blue                                          ,

                new Pen(Brushes.Red, 24)                              ,

                new Point(RenderSize.Width / 2, RenderSize.Height / 2),

                RenderSize.Width  / 2                                 ,

                RenderSize.Height / 2

            );

 

            drawingContext.PushClip(new RectangleGeometry(new Rect(new Point(0, 0), RenderSize)));

 

            Point textPoint = new Point

            (

                (RenderSize.Width  - this.formattedText.Width ) / 2,

                (RenderSize.Height - this.formattedText.Height) / 2

            );

 

            drawingContext.DrawText(this.formattedText, textPoint);

        }

 

        #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="DrawingContext 클래스 : PushClip 메소드를 사용해 클리핑 영역 설정하기"

    FontFamily="나눔고딕코딩"

    FontSize="16">

    <Grid>

        <local:CustomEllipse

            Margin="10" />

    </Grid>

</Window>

 

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

Posted by 사용자 icodebroker

댓글을 달아 주세요