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

■ PathGradientBrush 클래스 : 브러시 회전시키기

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


TestProject.zip


MainForm.cs

 

 

using System;

using System.Collections.Generic;

using System.Drawing;

using System.Drawing.Drawing2D;

using System.Windows.Forms;

 

namespace TestProject

{

    /// <summary>

    /// 메인 폼

    /// </summary>

    public partial class MainForm : Form

    {

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

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

 

        #region Field

 

        /// <summary>

        /// 다각형 포인트 배열

        /// </summary>

        private PointF[] polygonPointArray;

 

        /// <summary>

        /// 그래픽스 경로

        /// </summary>

        private GraphicsPath graphicsPath;

 

        /// <summary>

        /// 그리기 영역

        /// </summary>

        private Rectangle drawingArea;

 

        /// <summary>

        /// 색상 오프셋

        /// </summary>

        private int colorOffset = 0;

 

        #endregion

 

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

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

 

        #region 생성자 - MainForm()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainForm()

        {

            InitializeComponent();

 

            #region 이벤트를 설정한다.

 

            Load            += Form_Load;

            Paint           += Form_Paint;

            this.timer.Tick += timer_Tick;

 

            #endregion

        }

 

        #endregion

 

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

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

        //////////////////////////////////////////////////////////////////////////////// Event

 

        #region 폼 로드시 처리하기 - Form_Load(sender, e)

 

        /// <summary>

        /// 폼 로드시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void Form_Load(object sender, EventArgs e)

        {

            DoubleBuffered = true;

 

            const int MARGIN = 10;

 

            this.drawingArea = new Rectangle

            (

                MARGIN,

                MARGIN,

                ClientSize.Width  - 2 * MARGIN,

                ClientSize.Height - 2 * MARGIN

            );

 

            this.polygonPointArray = GetPolygonPointArray(22, this.drawingArea);

 

            this.graphicsPath = new GraphicsPath();

 

            this.graphicsPath.AddPolygon(InsertPoint(this.polygonPointArray));

        }

 

        #endregion

        #region 폼 페인트시 처리하기 - Form_Paint(sender, e)

 

        /// <summary>

        /// 폼 페인트시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void Form_Paint(object sender, PaintEventArgs e)

        {

            using(PathGradientBrush brush = new PathGradientBrush(this.graphicsPath))

            {

                Color[] edgeColorArray = new Color[this.polygonPointArray.Length * 2];

 

                Color[] seriesColorArray = new Color[]

                {

                    Color.Green,

                    Color.LightGreen,

                    Color.White,

                    Color.LightGreen,

                };

 

                for(int i = 0; i < edgeColorArray.Length; i++)

                {

                    edgeColorArray[i] = seriesColorArray[(i + this.colorOffset) % seriesColorArray.Length];

                }

 

                brush.SurroundColors = edgeColorArray;

                brush.CenterColor    = Color.White;

 

                this.colorOffset++;

 

                e.Graphics.FillRectangle(brush, this.drawingArea);

 

                using(Font font = new Font("Times New Roman", 50, FontStyle.Bold))

                {

                    using(StringFormat stringFormat = new StringFormat())

                    {

                        stringFormat.Alignment     = StringAlignment.Center;

                        stringFormat.LineAlignment = StringAlignment.Center;

 

                        e.Graphics.DrawString("테스트", font, Brushes.Blue, this.drawingArea, stringFormat);

                    }

                }

            }

        }

 

        #endregion

        #region 타이머 틱 처리하기 - timer_Tick(sender, e)

 

        /// <summary>

        /// 타이머 틱 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void timer_Tick(object sender, EventArgs e)

        {

            Refresh();

        }

 

        #endregion

 

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

 

        #region 다각형 포인트 배열 구하기 - GetPolygonPointArray(pointCount, boundRectangle)

 

        /// <summary>

        /// 다각형 포인트 배열 구하기

        /// </summary>

        /// <param name="pointCount">포인트 카운트</param>

        /// <param name="boundRectangle">테두리 사각형</param>

        /// <returns>다각형 포인트 배열</returns>

        private PointF[] GetPolygonPointArray(int pointCount, Rectangle boundRectangle)

        {

            PointF[] pointArray = new PointF[pointCount];

 

            float sqrt2 = (float)Math.Sqrt(2.0);

            float rx    = boundRectangle.Width  / 2f * sqrt2;

            float ry    = boundRectangle.Height / 2f * sqrt2;

            float cx    = boundRectangle.X + boundRectangle.Width  / 2f;

            float cy    = boundRectangle.Y + boundRectangle.Height / 2f;

 

            float theta      = (float)(-Math.PI / 2.0);

            float deltaTheta = (float)(2.0 * Math.PI / pointCount);

 

            for(int i = 0; i < pointCount; i++)

            {

                pointArray[i] = new PointF

                (

                    (float)(cx + rx * Math.Cos(theta)),

                    (float)(cy + ry * Math.Sin(theta))

                );

 

                theta += deltaTheta;

            }

 

            return pointArray;

        }

 

        #endregion

        #region 포인트 구하기 - GetPoint(point1, point2)

 

        /// <summary>

        /// 포인트 구하기

        /// </summary>

        /// <param name="point1">포인트 1</param>

        /// <param name="point2">포인트 2</param>

        /// <returns>포인트</returns>

        /// <remarks>두 포인트 사이의 포인트를 구한다.</remarks>

        private PointF GetPoint(PointF point1, PointF point2)

        {

            return new PointF

            (

                (point1.X + point2.X) / 2,

                (point1.Y + point2.Y) / 2

            );

        }

 

        #endregion

        #region 포인트 삽입하기 - InsertPoint(sourcePointArray)

 

        /// <summary>

        /// 포인트 삽입하기

        /// </summary>

        /// <param name="sourcePointArray">소스 포인트 배열</param>

        /// <returns>포인트 배열</returns>

        /// <remarks>각 다각형 포인트 사이에 포인트를 추가한다.</remarks>

        private PointF[] InsertPoint(PointF[] sourcePointArray)

        {

            List<PointF> targetPointList = new List<PointF>();

 

            for(int i = 0; i < sourcePointArray.Length - 1; i++)

            {

                targetPointList.Add(sourcePointArray[i]);

 

                targetPointList.Add(GetPoint(sourcePointArray[i], sourcePointArray[i + 1]));

            }

 

            targetPointList.Add(sourcePointArray[sourcePointArray.Length - 1]);

 

            targetPointList.Add(GetPoint(sourcePointArray[0], sourcePointArray[sourcePointArray.Length - 1]));

 

            return targetPointList.ToArray();

        }

 

        #endregion

    }

}

 

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

Posted by 사용자 icodebroker

댓글을 달아 주세요