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

TestProject.zip
0.01MB

▶ CanvasExtension.cs

using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;

namespace TestProject
{
    /// <summary>
    /// 캔버스 확장
    /// </summary>
    public static class CanvasExtension
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Public

        #region 중심점 구하기 - GetCenterPoint(shape)

        /// <summary>
        /// 중심점 구하기
        /// </summary>
        /// <param name="shape">도형</param>
        /// <returns>중심점</returns>
        public static Point GetCenterPoint(this Shape shape)
        {
            double centerX = (Canvas.GetLeft(shape) + Canvas.GetRight (shape)) / 2.0;
            double centerY = (Canvas.GetTop (shape) + Canvas.GetBottom(shape)) / 2.0;

            return new Point(centerX, centerY);
        }

        #endregion
        #region 테두리 사각형 구하기 - GetBoundRectangle(shape)

        /// <summary>
        /// 테두리 사각형 구하기
        /// </summary>
        /// <param name="shape">도형</param>
        /// <returns>테두리 사각형</returns>
        public static Rect GetBoundRectangle(this Shape shape)
        {
            return new Rect(Canvas.GetLeft(shape), Canvas.GetTop(shape), shape.Width, shape.Height);
        }

        #endregion
        #region 테두리 일치시키기 - MatchBounds(shape1, shape2)

        /// <summary>
        /// 테두리 일치시키기
        /// </summary>
        /// <param name="shape1">도형 1</param>
        /// <param name="shape2">도형 2</param>
        public static void MatchBounds(this Shape shape1, Shape shape2)
        {
            shape1.SetBounds(shape2.GetBoundRectangle());
        }

        #endregion

        #region 사각형 그리기 - DrawRectangle(canvas, left, top, width, height, fillBrush, strokeBrush, strokeThickness, strokeDashCollection)

        /// <summary>
        /// 사각형 그리기
        /// </summary>
        /// <param name="canvas">캔버스</param>
        /// <param name="left">왼쪽 좌표</param>
        /// <param name="top">위쪽 좌표</param>
        /// <param name="width">너비</param>
        /// <param name="height">높이</param>
        /// <param name="fillBrush">채우기 브러시</param>
        /// <param name="strokeBrush">스트로크 브러시</param>
        /// <param name="strokeThickness">스트로크 두께</param>
        /// <param name="strokeDashCollection">스트로크 대시 컬렉션</param>
        /// <returns>사각형</returns>
        public static Rectangle DrawRectangle
        (
            this Canvas      canvas,
            double           left,
            double           top,
            double           width,
            double           height,
            Brush            fillBrush,
            Brush            strokeBrush,
            double           strokeThickness,
            DoubleCollection strokeDashCollection
        )
        {
            Rectangle rectangle = new Rectangle();

            rectangle.SetValue(left, top, width, height, fillBrush, strokeBrush, strokeThickness, strokeDashCollection);

            canvas.Children.Add(rectangle);

            return rectangle;
        }

        #endregion
        #region 사각형 그리기 - DrawRectangle(canvas, left, top, width, height, fillBrush, strokeBrush, strokeThickness)

        /// <summary>
        /// 사각형 그리기
        /// </summary>
        /// <param name="canvas">캔버스</param>
        /// <param name="left">왼쪽 좌표</param>
        /// <param name="top">위쪽 좌표</param>
        /// <param name="width">너비</param>
        /// <param name="height">높이</param>
        /// <param name="fillBrush">채우기 브러시</param>
        /// <param name="strokeBrush">스트로크 브러시</param>
        /// <param name="strokeThickness">스트로크 두께</param>
        /// <param name="strokeDashCollection">스트로크 대시 컬렉션</param>
        /// <returns>사각형</returns>
        public static Rectangle DrawRectangle
        (
            this Canvas canvas,
            double      left,
            double      top,
            double      width,
            double      height,
            Brush       fillBrush,
            Brush       strokeBrush,
            double      strokeThickness
        )
        {
            return canvas.DrawRectangle(left, top, width, height, fillBrush, strokeBrush, strokeThickness, null);
        }

        #endregion
        #region 사각형 그리기 - DrawRectangle(canvas, left, top, width, height, fillBrush, strokeBrush)

        /// <summary>
        /// 사각형 그리기
        /// </summary>
        /// <param name="canvas">캔버스</param>
        /// <param name="left">왼쪽 좌표</param>
        /// <param name="top">위쪽 좌표</param>
        /// <param name="width">너비</param>
        /// <param name="height">높이</param>
        /// <param name="fillBrush">채우기 브러시</param>
        /// <param name="strokeBrush">스트로크 브러시</param>
        /// <returns>사각형</returns>
        public static Rectangle DrawRectangle
        (
            this Canvas canvas,
            double      left,
            double      top,
            double      width,
            double      height,
            Brush       fillBrush,
            Brush       strokeBrush
        )
        {
            return canvas.DrawRectangle(left, top, width, height, fillBrush, strokeBrush, 1, null);
        }

        #endregion

        #region 타원 그리기 - DrawEllipse(canvas, left, top, width, height, fillBrush, strokeBrush, strokeThickness, strokeDashCollection)

        /// <summary>
        /// 타원 그리기
        /// </summary>
        /// <param name="canvas">캔버스</param>
        /// <param name="left">왼쪽 좌표</param>
        /// <param name="top">위쪽 좌표</param>
        /// <param name="width">너비</param>
        /// <param name="height">높이</param>
        /// <param name="fillBrush">채우기 브러시</param>
        /// <param name="strokeBrush">스트로크 브러시</param>
        /// <param name="strokeThickness">스트로크 두께</param>
        /// <param name="strokeDashCollection">스트로크 대시 컬렉션</param>
        /// <returns>타원</returns>
        public static Ellipse DrawEllipse
        (
            this Canvas      canvas,
            double           left,
            double           top,
            double           width,
            double           height,
            Brush            fillBrush,
            Brush            strokeBrush,
            double           strokeThickness,
            DoubleCollection strokeDashCollection
        )
        {
            Ellipse ellipse = new Ellipse();

            ellipse.SetValue(left, top, width, height, fillBrush, strokeBrush, strokeThickness, strokeDashCollection);

            canvas.Children.Add(ellipse);

            return ellipse;
        }

        #endregion
        #region 타원 그리기 - DrawEllipse(canvas, left, top, width, height, fillBrush, strokeBrush, strokeThickness)

        /// <summary>
        /// 타원 그리기
        /// </summary>
        /// <param name="canvas">캔버스</param>
        /// <param name="left">왼쪽 좌표</param>
        /// <param name="top">위쪽 좌표</param>
        /// <param name="width">너비</param>
        /// <param name="height">높이</param>
        /// <param name="fillBrush">채우기 브러시</param>
        /// <param name="strokeBrush">스트로크 브러시</param>
        /// <param name="strokeThickness">스트로크 두께</param>
        /// <returns>타원</returns>
        public static Ellipse DrawEllipse
        (
            this Canvas canvas,
            double      left,
            double      top,
            double      width,
            double      height,
            Brush       fillBrush,
            Brush       strokeBrush,
            double      strokeThickness
        )
        {
            return canvas.DrawEllipse(left, top, width, height, fillBrush, strokeBrush, strokeThickness, null);
        }

        #endregion
        #region 타원 그리기 - DrawEllipse(canvas, left, top, width, height, fillBrush, strokeBrush)

        /// <summary>
        /// 타원 그리기
        /// </summary>
        /// <param name="canvas">캔버스</param>
        /// <param name="left">왼쪽 좌표</param>
        /// <param name="top">위쪽 좌표</param>
        /// <param name="width">너비</param>
        /// <param name="height">높이</param>
        /// <param name="fillBrush">채우기 브러시</param>
        /// <param name="strokeBrush">스트로크 브러시</param>
        /// <returns>타원</returns>
        public static Ellipse DrawEllipse(this Canvas canvas, double left, double top, double width, double height, Brush fillBrush, Brush strokeBrush)
        {
            return canvas.DrawEllipse(left, top, width, height, fillBrush, strokeBrush, 1, null);
        }

        #endregion

        #region 다각형 그리기 - DrawPolygon(canvas, pointCollection, fillBrush, strokeBrush, strokeThickness, strokeDashCollection)

        /// <summary>
        /// 다각형 그리기
        /// </summary>
        /// <param name="canvas">캔버스</param>
        /// <param name="pointCollection">포인트 컬렉션</param>
        /// <param name="fillBrush">채우기 브러시</param>
        /// <param name="strokeBrush">스트로크 브러시</param>
        /// <param name="strokeThickness">스트로크 두께</param>
        /// <param name="strokeDashCollection">스트로크 대시 컬렉션</param>
        /// <returns>다각형</returns>
        public static Polygon DrawPolygon
        (
            this Canvas      canvas,
            PointCollection  pointCollection,
            Brush            fillBrush,
            Brush            strokeBrush,
            double           strokeThickness,
            DoubleCollection strokeDashCollection
        )
        {
            Polygon polygon = new Polygon();

            polygon.Points = pointCollection;

            polygon.SetFillAndStroke(fillBrush, strokeBrush, strokeThickness, strokeDashCollection);

            canvas.Children.Add(polygon);

            return polygon;
        }

        #endregion
        #region 다각형 그리기 - DrawPolygon(canvas, pointCollection, fillBrush, strokeBrush, strokeThickness)

        /// <summary>
        /// 다각형 그리기
        /// </summary>
        /// <param name="canvas">캔버스</param>
        /// <param name="pointCollection">포인트 컬렉션</param>
        /// <param name="fillBrush">채우기 브러시</param>
        /// <param name="strokeBrush">스트로크 브러시</param>
        /// <param name="strokeThickness">스트로크 두께</param>
        /// <returns>다각형</returns>
        public static Polygon DrawPolygon
        (
            this Canvas     canvas,
            PointCollection pointCollection,
            Brush           fillBrush,
            Brush           strokeBrush,
            double          strokeThickness
        )
        {
            return canvas.DrawPolygon(pointCollection, fillBrush, strokeBrush, strokeThickness, null);
        }

        #endregion
        #region 다각형 그리기 - DrawPolygon(canvas, pointCollection, fillBrush, strokeBrush)

        /// <summary>
        /// 다각형 그리기
        /// </summary>
        /// <param name="canvas">캔버스</param>
        /// <param name="pointCollection">포인트 컬렉션</param>
        /// <param name="fillBrush">채우기 브러시</param>
        /// <param name="strokeBrush">스트로크 브러시</param>
        /// <returns>다각형</returns>
        public static Polygon DrawPolygon(this Canvas canvas, PointCollection pointCollection, Brush fillBrush, Brush strokeBrush)
        {
            return canvas.DrawPolygon(pointCollection, fillBrush, strokeBrush, 1, null);
        }

        #endregion

        #region 다각선 그리기 - DrawPolyline(canvas, pointCollection, fillBrush, strokeBrush, strokeThickness, strokeDashCollection)

        /// <summary>
        /// 다각선 그리기
        /// </summary>
        /// <param name="canvas">캔버스</param>
        /// <param name="pointCollection">포인트 컬렉션</param>
        /// <param name="fillBrush">채우기 브러시</param>
        /// <param name="strokeBrush">스트로크 브러시</param>
        /// <param name="strokeThickness">스트로크 두께</param>
        /// <param name="strokeDashCollection">스트로크 대시 컬렉션</param>
        /// <returns>다각선</returns>
        public static Polyline DrawPolyline
        (
            this Canvas      canvas,
            PointCollection  pointCollection,
            Brush            fillBrush,
            Brush            strokeBrush,
            double           strokeThickness,
            DoubleCollection strokeDashCollection
        )
        {
            Polyline polyline = new Polyline();

            polyline.Points = pointCollection;

            polyline.SetFillAndStroke(fillBrush, strokeBrush, strokeThickness, strokeDashCollection);

            canvas.Children.Add(polyline);

            return polyline;
        }

        #endregion
        #region 다각선 그리기 - DrawPolyline(canvas, pointCollection, fillBrush, strokeBrush, strokeThickness)

        /// <summary>
        /// 다각선 그리기
        /// </summary>
        /// <param name="canvas">캔버스</param>
        /// <param name="pointCollection">포인트 컬렉션</param>
        /// <param name="fillBrush">채우기 브러시</param>
        /// <param name="strokeBrush">스트로크 브러시</param>
        /// <param name="strokeThickness">스트로크 두께</param>
        /// <returns>다각선</returns>
        public static Polyline DrawPolyline
        (
            this Canvas     canvas,
            PointCollection pointCollection,
            Brush           fillBrush,
            Brush           strokeBrush,
            double          strokeThickness
        )
        {
            return canvas.DrawPolyline(pointCollection, fillBrush, strokeBrush, strokeThickness, null);
        }

        #endregion
        #region 다각선 그리기 - DrawPolyline(canvas, pointCollection, fillBrush, strokeBrush)

        /// <summary>
        /// 다각선 그리기
        /// </summary>
        /// <param name="canvas">캔버스</param>
        /// <param name="pointCollection">포인트 컬렉션</param>
        /// <param name="fillBrush">채우기 브러시</param>
        /// <param name="strokeBrush">스트로크 브러시</param>
        /// <returns>다각선</returns>
        public static Polyline DrawPolyline(this Canvas canvas, PointCollection pointCollection, Brush fillBrush, Brush strokeBrush)
        {
            return canvas.DrawPolyline(pointCollection, fillBrush, strokeBrush, 1, null);
        }

        #endregion

        #region 레이블 그리기 - DrawLabel(canvas, left, top, width, height, text, fontFamily, fontSize, fontWeight, fontStyle, borderBrush, borderThickness,
            backgroundBrush, foregroundBrush, horizontalAlignment, verticalAlignment)

        /// <summary>
        /// 레이블 그리기
        /// </summary>
        /// <param name="canvas">캔버스</param>
        /// <param name="left">왼쪽 좌표</param>
        /// <param name="top">위쪽 좌표</param>
        /// <param name="width">너비</param>
        /// <param name="height">높이</param>
        /// <param name="text">텍스트</param>
        /// <param name="fontFamily">폰트 패밀리</param>
        /// <param name="fontSize">폰트 크기</param>
        /// <param name="fontWeight">폰트 가중치</param>
        /// <param name="fontStyle">폰트 스타일</param>
        /// <param name="borderBrush">테두리 브러시</param>
        /// <param name="borderThickness">테두리 두께</param>
        /// <param name="backgroundBrush">배경 브러시</param>
        /// <param name="foregroundBrush">전경 브러시</param>
        /// <param name="horizontalAlignment">수평 정렬</param>
        /// <param name="verticalAlignment">수직 정렬</param>
        /// <returns>레이블</returns>
        public static Label DrawLabel
        (
            this Canvas         canvas,
            double              left,
            double              top,
            double              width,
            double              height,
            string              text,
            string              fontFamily,
            double              fontSize,
            FontWeight          fontWeight,
            FontStyle           fontStyle,
            Brush               borderBrush,
            Thickness           borderThickness,
            Brush               backgroundBrush,
            Brush               foregroundBrush,
            HorizontalAlignment horizontalAlignment,
            VerticalAlignment   verticalAlignment
        )
        {
            Label label = new Label();

            Canvas.SetLeft(label, left);
            Canvas.SetTop(label , top );

            label.Width                      = width;
            label.Height                     = height;
            label.BorderBrush                = borderBrush;
            label.BorderThickness            = borderThickness;
            label.Background                 = backgroundBrush;
            label.Foreground                 = foregroundBrush;
            label.HorizontalContentAlignment = horizontalAlignment;
            label.VerticalContentAlignment   = verticalAlignment;
            label.FontFamily                 = new FontFamily(fontFamily);
            label.FontSize                   = fontSize;
            label.FontWeight                 = fontWeight;
            label.FontStyle                  = fontStyle;
            label.Content                    = text;

            canvas.Children.Add(label);

            return label;
        }

        #endregion
        #region 레이블 그리기 - DrawLabel(canvas, left, top, width, height, text, fontFamily, fontSize, fontWeight, fontStyle, borderBrush, borderThickness,
            backgroundBrush, foregroundBrush, horizontalAlignment, verticalAlignment)

        /// <summary>
        /// 레이블 그리기
        /// </summary>
        /// <param name="canvas">캔버스</param>
        /// <param name="left">왼쪽 좌표</param>
        /// <param name="top">위쪽 좌표</param>
        /// <param name="width">너비</param>
        /// <param name="height">높이</param>
        /// <param name="text">텍스트</param>
        /// <param name="fontFamily">폰트 패밀리</param>
        /// <param name="fontSize">폰트 크기</param>
        /// <param name="fontWeight">폰트 가중치</param>
        /// <param name="fontStyle">폰트 스타일</param>
        /// <param name="borderBrush">테두리 브러시</param>
        /// <param name="borderThickness">테두리 두께</param>
        /// <param name="backgroundBrush">배경 브러시</param>
        /// <param name="foregroundBrush">전경 브러시</param>
        /// <param name="horizontalAlignment">수평 정렬</param>
        /// <param name="verticalAlignment">수직 정렬</param>
        /// <returns>레이블</returns>
        public static Label DrawLabel
        (
            this Canvas         canvas,
            double              left,
            double              top,
            double              width,
            double              height,
            string              text,
            string              fontFamily,
            double              fontSize,
            FontWeight          fontWeight,
            FontStyle           fontStyle,
            Brush               borderBrush,
            double              borderThickness,
            Brush               backgroundBrush,
            Brush               foregroundBrush,
            HorizontalAlignment horizontalAlignment,
            VerticalAlignment   verticalAlignment
        )
        {
            return canvas.DrawLabel
            (
                left,
                top,
                width,
                height,
                text,
                fontFamily,
                fontSize,
                fontWeight,
                fontStyle,
                borderBrush,
                new Thickness(borderThickness),
                backgroundBrush,
                foregroundBrush,
                horizontalAlignment,
                verticalAlignment
            );
        }

        #endregion
        #region 레이블 그리기 - DrawLabel(canvas, left, top, width, height, text, fontFamily, fontSize, fontWeight, fontStyle, borderBrush, borderThickness,
            backgroundBrush, foregroundBrush, horizontalAlignment, verticalAlignment)

        /// <summary>
        /// 레이블 그리기
        /// </summary>
        /// <param name="canvas">캔버스</param>
        /// <param name="left">왼쪽 좌표</param>
        /// <param name="top">위쪽 좌표</param>
        /// <param name="width">너비</param>
        /// <param name="height">높이</param>
        /// <param name="text">텍스트</param>
        /// <param name="fontFamily">폰트 패밀리</param>
        /// <param name="fontSize">폰트 크기</param>
        /// <param name="fontWeight">폰트 가중치</param>
        /// <param name="fontStyle">폰트 스타일</param>
        /// <param name="borderBrush">테두리 브러시</param>
        /// <param name="borderThickness">테두리 두께</param>
        /// <param name="backgroundBrush">배경 브러시</param>
        /// <param name="foregroundBrush">전경 브러시</param>
        /// <returns>레이블</returns>
        public static Label DrawLabel
        (
            this Canvas canvas,
            double      left,
            double      top,
            double      width,
            double      height,
            string      text,
            string      fontFamily,
            double      fontSize,
            FontWeight  fontWeight,
            FontStyle   fontStyle,
            Brush       borderBrush,
            double      borderThickness,
            Brush       backgroundBrush,
            Brush       foregroundBrush
        )
        {
            return canvas.DrawLabel
            (
                left,
                top,
                width,
                height,
                text,
                fontFamily,
                fontSize,
                fontWeight,
                fontStyle,
                borderBrush,
                new Thickness(borderThickness),
                backgroundBrush,
                foregroundBrush, 
                HorizontalAlignment.Center,
                VerticalAlignment.Center
            );
        }

        #endregion

        #region 선 그리기 - DrawLine(canvas, x1, y1, x2, y2, strokeBrush, strokeThickness, strokeDashCollection)

        /// <summary>
        /// 선 그리기
        /// </summary>
        /// <param name="canvas">캔버스</param>
        /// <param name="x1">X1</param>
        /// <param name="y1">Y1</param>
        /// <param name="x2">X2</param>
        /// <param name="y2">Y2</param>
        /// <param name="strokeBrush">스트로크 브로시</param>
        /// <param name="strokeThickness">스트로크 두께</param>
        /// <param name="strokeDashCollection">스트로크 대시 컬렉션</param>
        /// <returns>선</returns>
        public static Line DrawLine
        (
            this Canvas      canvas,
            double           x1,
            double           y1,
            double           x2,
            double           y2,
            Brush            strokeBrush,
            double           strokeThickness,
            DoubleCollection strokeDashCollection
        )
        {
            Line line = new Line();

            line.X1              = x1;
            line.Y1              = y1;
            line.X2              = x2;
            line.Y2              = y2;
            line.Stroke          = strokeBrush;
            line.StrokeThickness = strokeThickness;

            if(strokeDashCollection != null)
            {
                line.StrokeDashArray = strokeDashCollection;
            }

            canvas.Children.Add(line);

            return line;
        }

        #endregion
        #region 선 그리기 - DrawLine(canvas, x1, y1, x2, y2, strokeBrush, strokeThickness)

        /// <summary>
        /// 선 그리기
        /// </summary>
        /// <param name="canvas">캔버스</param>
        /// <param name="x1">X1</param>
        /// <param name="y1">Y1</param>
        /// <param name="x2">X2</param>
        /// <param name="y2">Y2</param>
        /// <param name="strokeBrush">스트로크 브로시</param>
        /// <param name="strokeThickness">스트로크 두께</param>
        /// <returns>선</returns>
        public static Line DrawLine(this Canvas canvas, double x1, double y1, double x2, double y2, Brush strokeBrush, double strokeThickness)
        {
            return canvas.DrawLine(x1, y1, x2, y2, strokeBrush, strokeThickness, null);
        }

        #endregion
        #region 선 그리기 - DrawLine(canvas, x1, y1, x2, y2, strokeBrush)

        /// <summary>
        /// 선 그리기
        /// </summary>
        /// <param name="canvas">캔버스</param>
        /// <param name="x1">X1</param>
        /// <param name="y1">Y1</param>
        /// <param name="x2">X2</param>
        /// <param name="y2">Y2</param>
        /// <param name="strokeBrush">스트로크 브로시</param>
        /// <returns>선</returns>
        public static Line DrawLine(this Canvas canvas, double x1, double y1, double x2, double y2, Brush strokeBrush)
        {
            return canvas.DrawLine(x1, y1, x2, y2, strokeBrush, 1, null);
        }

        #endregion

        #region 점 그리기 - DrawDot(canvas, centerX, centerY, radius, fillBrush, strokeBrush, strokeThickness, strokeDashCollection)

        /// <summary>
        /// 점 그리기
        /// </summary>
        /// <param name="canvas">캔버스</param>
        /// <param name="centerX">중심 X</param>
        /// <param name="centerY">중심 Y</param>
        /// <param name="radius">반지름</param>
        /// <param name="fillBrush">채우기 브러시</param>
        /// <param name="strokeBrush">스트로크 브러시</param>
        /// <param name="strokeThickness">스트로크 두께</param>
        /// <param name="strokeDashCollection">스트로크 대시 컬렉션</param>
        /// <returns>타원</returns>
        public static Ellipse DrawDot
        (
            this Canvas      canvas,
            double           centerX,
            double           centerY,
            double           radius,
            Brush            fillBrush,
            Brush            strokeBrush,
            double           strokeThickness,
            DoubleCollection strokeDashCollection
        )
        {
            return canvas.DrawEllipse
            (
                centerX - radius,
                centerY - radius,
                2.0 * radius,
                2.0 * radius,
                fillBrush,
                strokeBrush,
                strokeThickness,
                strokeDashCollection
            );
        }

        #endregion
        #region 점 그리기 - DrawDot(canvas, label, centerX, centerY, radius, fillBrush, strokeBrush, strokeThickness, strokeDashCollection,
            text, fontFamily, fontSize, fontWeight, fontStyle, borderBrush, borderThickness, backgroundBrush, foregroundBrush)

        /// <summary>
        /// 점 그리기
        /// </summary>
        /// <param name="canvas">캔버스</param>
        /// <param name="label">레이블</param>
        /// <param name="centerX">중심 X</param>
        /// <param name="centerY">중심 Y</param>
        /// <param name="radius">반지름</param>
        /// <param name="fillBrush">채우기 브러시</param>
        /// <param name="strokeBrush">스트로크 브러시</param>
        /// <param name="strokeThickness">스트로크 두께</param>
        /// <param name="strokeDashCollection">스트로크 대시 컬렉션</param>
        /// <param name="text">텍스트</param>
        /// <param name="fontFamily">폰트 패밀리</param>
        /// <param name="fontSize">폰트 크기</param>
        /// <param name="fontWeight">폰트 가중치</param>
        /// <param name="fontStyle">폰트 스타일</param>
        /// <param name="borderBrush">테두리 브러시</param>
        /// <param name="borderThickness">테두리 두께</param>
        /// <param name="backgroundBrush">배경 브러시</param>
        /// <param name="foregroundBrush">전경 브러시</param>
        /// <returns>타원</returns>
        public static Ellipse DrawDot
        (
            this Canvas      canvas,
            out Label        label,
            double           centerX,
            double           centerY,
            double           radius,
            Brush            fillBrush,
            Brush            strokeBrush,
            double           strokeThickness,
            DoubleCollection strokeDashCollection,
            string           text,
            string           fontFamily,
            double           fontSize,
            FontWeight       fontWeight,
            FontStyle        fontStyle,
            Brush            borderBrush,
            Thickness        borderThickness,
            Brush            backgroundBrush,
            Brush            foregroundBrush
        )
        {
            Ellipse ellipse = canvas.DrawDot(centerX, centerY, radius, fillBrush, strokeBrush, strokeThickness, strokeDashCollection);

            label = canvas.DrawLabel
            (
                centerX - radius,
                centerY - radius,
                2.0 * radius,
                2.0 * radius,
                text,
                fontFamily,
                fontSize,
                fontWeight,
                fontStyle,
                borderBrush,
                borderThickness,
                backgroundBrush,
                foregroundBrush,
                HorizontalAlignment.Center,
                VerticalAlignment.Center
            );

            return ellipse;
        }

        #endregion
        #region 점 그리기 - DrawDot(canvas, label, centerX, centerY, radius, fillBrush, strokeBrush, strokeThickness, strokeDashCollection,
            text, fontFamily, fontSize, fontWeight, fontStyle, backgroundBrush, foregroundBrush)

        /// <summary>
        /// 점 그리기
        /// </summary>
        /// <param name="canvas">캔버스</param>
        /// <param name="label">레이블</param>
        /// <param name="centerX">중심 X</param>
        /// <param name="centerY">중심 Y</param>
        /// <param name="radius">반지름</param>
        /// <param name="fillBrush">채우기 브러시</param>
        /// <param name="strokeBrush">스트로크 브러시</param>
        /// <param name="strokeThickness">스트로크 두께</param>
        /// <param name="strokeDashCollection">스트로크 대시 컬렉션</param>
        /// <param name="text">텍스트</param>
        /// <param name="fontFamily">폰트 패밀리</param>
        /// <param name="fontSize">폰트 크기</param>
        /// <param name="fontWeight">폰트 가중치</param>
        /// <param name="fontStyle">폰트 스타일</param>
        /// <param name="backgroundBrush">배경 브러시</param>
        /// <param name="foregroundBrush">전경 브러시</param>
        /// <returns>타원</returns>
        public static Ellipse DrawDot
        (
            this Canvas      canvas,
            out Label        label,
            double           centerX,
            double           centerY,
            double           radius,
            Brush            fillBrush,
            Brush            strokeBrush,
            double           strokeThickness,
            DoubleCollection strokeDashCollection,
            string           text,
            string           fontFamily,
            double           fontSize,
            FontWeight       fontWeight,
            FontStyle        fontStyle,
            Brush            backgroundBrush,
            Brush            foregroundBrush
        )
        {
            return canvas.DrawDot
            (
                out label,
                centerX,
                centerY,
                radius,
                fillBrush,
                strokeBrush,
                strokeThickness,
                strokeDashCollection,
                text,
                fontFamily,
                fontSize,
                fontWeight,
                fontStyle,
                null,
                new Thickness(),
                backgroundBrush,
                foregroundBrush
            );
        }

        #endregion

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

        #region 테두리 설정하기 - SetBounds(shape, left, top, width, height)

        /// <summary>
        /// 테두리 설정하기
        /// </summary>
        /// <param name="shape">도형</param>
        /// <param name="left">왼쪽 좌표</param>
        /// <param name="top">위쪽 좌표</param>
        /// <param name="width">너비</param>
        /// <param name="height">높이</param>
        private static void SetBounds(this Shape shape, double left, double top, double width, double height)
        {
            Canvas.SetLeft(shape, left);
            Canvas.SetTop (shape, top );

            shape.Width  = width;
            shape.Height = height;
        }

        #endregion
        #region 테두리 설정하기 - SetBounds(shape, boundRectangle)

        /// <summary>
        /// 테두리 설정하기
        /// </summary>
        /// <param name="shape">도형</param>
        /// <param name="boundRectangle">테두리 사각형</param>
        private static void SetBounds(this Shape shape, Rect boundRectangle)
        {
            shape.SetBounds(boundRectangle.Left, boundRectangle.Top, boundRectangle.Width, boundRectangle.Height);
        }

        #endregion
        #region 채우기/스트로크 설정하기 - SetFillAndStroke(shape, fillBrush, strokeBrush, strokeThickness, strokeDashCollection)

        /// <summary>
        /// 채우기/스트로크 설정하기
        /// </summary>
        /// <param name="shape">도형</param>
        /// <param name="fillBrush">채우기 브러시</param>
        /// <param name="strokeBrush">스트로크 브러시</param>
        /// <param name="strokeThickness">스트로크 두께</param>
        /// <param name="strokeDashCollection">스트로크 대시 컬렉션</param>
        private static void SetFillAndStroke
        (
            this Shape       shape,
            Brush            fillBrush,
            Brush            strokeBrush,
            double           strokeThickness,
            DoubleCollection strokeDashCollection
        )
        {
            shape.Fill            = fillBrush;
            shape.Stroke          = strokeBrush;
            shape.StrokeThickness = strokeThickness;

            if(strokeDashCollection != null)
            {
                shape.StrokeDashArray = strokeDashCollection;
            }
        }

        #endregion
        #region 값 설정하기 - SetValue(shape, left, top, width, height, fillBrush, strokeBrush, strokeThickness, strokeDashCollection)

        /// <summary>
        /// 값 설정하기
        /// </summary>
        /// <param name="shape">도형</param>
        /// <param name="left">왼쪽 좌표</param>
        /// <param name="top">위쪽 좌표</param>
        /// <param name="width">너비</param>
        /// <param name="height">높이</param>
        /// <param name="fillBrush">채우기 브러시</param>
        /// <param name="strokeBrush">스트로크 브러시</param>
        /// <param name="strokeThickness">스트로크 두께</param>
        /// <param name="strokeDashCollection">스트로크 대시 컬렉션</param>
        private static void SetValue
        (
            this Shape       shape,
            double           left,
            double           top,
            double           width,
            double           height,
            Brush            fillBrush,
            Brush            strokeBrush,
            double           strokeThickness,
            DoubleCollection strokeDashCollection
        )
        {
            shape.SetBounds(left, top, width, height);

            shape.SetFillAndStroke(fillBrush, strokeBrush, strokeThickness, strokeDashCollection);
        }

        #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"
    Width="800"
    Height="600"
    Title="TestProject"
    FontFamily="나눔고딕코딩"
    FontSize="16">
    <Canvas Name="canvas" />
</Window>

 

728x90

 

▶ MainWindow.xaml.cs

using System.Windows;
using System.Windows.Controls;
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

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

        /// <summary>
        /// 윈도우 로드시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.canvas.DrawEllipse
            (
                100,
                50,
                100,
                100,
                Brushes.LightGreen,
                Brushes.Green,
                3,
                new DoubleCollection() { 5, 5 }
            );

            this.canvas.DrawRectangle
            (
                300,
                50,
                100,
                100,
                Brushes.LightBlue,
                Brushes.Blue
            );

            this.canvas.DrawLine
            (
                500,
                50,
                600,
                150,
                Brushes.Red,
                4
            );

            PointCollection pointCollection1 = new PointCollection()
            {
                new Point( 50, 250),
                new Point(260, 200),
                new Point(180, 310),
                new Point(130, 250),
                new Point( 70, 360)
            };

            this.canvas.DrawPolygon
            (
                pointCollection1,
                Brushes.Pink,
                Brushes.Red,
                2,
                new DoubleCollection() { 2, 2 }
            );

            PointCollection pointCollection2 = new PointCollection()
            {
                new Point(330, 200),
                new Point(380, 240),
                new Point(370, 290),
                new Point(300, 270),
                new Point(310, 220),
                new Point(340, 260)
            };

            this.canvas.DrawPolyline
            (
                pointCollection2,
                Brushes.Transparent,
                Brushes.Green,
                3
            );

            this.canvas.DrawLabel
            (
                500,
                200,
                100,
                100,
                "Centered",
                "나눔고딕코딩",
                16,
                FontWeights.DemiBold,
                FontStyles.Italic,
                Brushes.Black,
                1,
                Brushes.Yellow,
                Brushes.Blue
            );

            this.canvas.DrawDot
            (
                100,
                400,
                10,
                Brushes.LightCyan,
                Brushes.Blue,
                1,
                null
            );

            this.canvas.DrawDot
            (
                150,
                400,
                10,
                Brushes.LightCyan,
                Brushes.Blue,
                1,
                null
            );

            this.canvas.DrawDot
            (
                200,
                400,
                5,
                Brushes.Blue,
                null,
                1,
                null
            );

            this.canvas.DrawDot
            (
                250,
                400,
                5,
                Brushes.Blue,
                null,
                1,
                null
            );

            Label dotLabel;

            this.canvas.DrawDot
            (
                out dotLabel,
                300,
                400,
                20,
                Brushes.LightYellow,
                Brushes.Black,
                2,
                null,
                "1",
                "나눔고딕코딩",
                16,
                FontWeights.Normal,
                FontStyles.Normal,
                null,
                new Thickness(2),
                null,
                Brushes.Red
            );

            this.canvas.DrawDot
            (
                out dotLabel,
                350,
                400,
                20,
                Brushes.Orange,
                Brushes.Blue,
                1,
                null,
                "2",
                "나눔고딕코딩",
                16,
                FontWeights.Normal,
                FontStyles.Normal,
                null,
                Brushes.Black
            );
        }

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

댓글을 달아 주세요