첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
본 블로그는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 블로그 콘텐츠 향상을 위해 쓰여집니다.

728x90
반응형

TestProject.zip
0.01MB

▶ CustomVisual.cs

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

namespace TestProject
{
    /// <summary>
    /// 커스텀 비주얼
    /// </summary>
    public class CustomVisual : DrawingVisual
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - CustomVisual()

        /// <summary>
        /// 생성자
        /// </summary>
        public CustomVisual()
        {
            using(DrawingContext drawingContext = RenderOpen())
            {
                drawingContext.DrawRectangle
                (
                    Brushes.Orange,
                    new Pen(Brushes.DarkGray, 5),
                    new Rect
                    (
                        new Point(10, 10),
                        new Point(90, 90)
                    )
                );

                drawingContext.DrawEllipse
                (
                    Brushes.Beige,
                    new Pen(Brushes.DarkGray, 5),
                    new Point(150, 150),
                    40,
                    40
                );
            }
        }

        #endregion
    }
}

 

728x90

 

▶ CustomElement.cs

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

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

        #region Field

        /// <summary>
        /// 커스텀 드로잉 비주얼
        /// </summary>
        private CustomVisual customDrawingVisual = new CustomVisual();

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Property
        ////////////////////////////////////////////////////////////////////////////////////////// Protected

        #region 비주얼 자식 카운트 - VisualChildrenCount

        /// <summary>
        /// 비주얼 자식 카운트
        /// </summary>
        protected override int VisualChildrenCount
        {
            get
            {
                return 1;
            }
        }

        #endregion

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

        #region 비주얼 자식 구하기 - GetVisualChild(index)

        /// <summary>
        /// 비주얼 자식 구하기
        /// </summary>
        /// <param name="index">인덱스</param>
        /// <returns>비주얼</returns>
        protected override Visual GetVisualChild(int index)
        {
            return this.customDrawingVisual;
        }

        #endregion
    }
}

 

300x250

 

▶ 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="FrameworkElement 클래스 : 커스텀 엘리먼트 만들기"
    FontFamily="나눔고딕코딩"
    FontSize="16">
    <Grid>
        <Border
            HorizontalAlignment="Center"
            VerticalAlignment="Center"
            BorderThickness="5"
            BorderBrush="DarkGray"
            Background="RoyalBlue">
            <local:CustomElement
                Width="200"
                Height="200" />
        </Border>
    </Grid>
</Window>
728x90
반응형
그리드형(광고전용)
Posted by 사용자 icodebroker
TAG , ,

댓글을 달아 주세요