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

728x90
반응형

TestProject.zip
다운로드

▶ MainForm.cs

using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace TestProject
{
    /// <summary>
    /// 메인 폼
    /// </summary>
    public partial class MainForm : Form
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - MainForm()

        /// <summary>
        /// 생성자
        /// </summary>
        public MainForm()
        {
            InitializeComponent();

            #region 이벤트를 설정한다.

            Paint += Form_Paint;

            #endregion
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Private
        //////////////////////////////////////////////////////////////////////////////// Event

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

        /// <summary>
        /// 폼 페인트시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void Form_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

            int size = (ClientSize.Width > ClientSize.Height ? ClientSize.Height : ClientSize.Width) - 20;
            int left = (ClientSize.Width  - size) / 2;
            int top  = (ClientSize.Height - size) / 2;

            DrawColorWheel(e.Graphics, BackColor, left, top, size, size);
        }

        #endregion

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

        #region 서라운드 색상 배열 설정하기 - SetSurroundColorArray(surroundColorArray, index, stopPoint,
            fromAlpha, fromRed, fromGreen, fromBlue, toAlpha, toRed, toGreen, toBlue)

        /// <summary>
        /// 서라운드 색상 배열 설정하기
        /// </summary>
        /// <param name="surroundColorArray">서라운드 색상 배열</param>
        /// <param name="index">인덱스</param>
        /// <param name="stopPoint">중단 포인트</param>
        /// <param name="fromAlpha">FROM 투명도</param>
        /// <param name="fromRed">FROM 빨간색</param>
        /// <param name="fromGreen">FROM 녹색</param>
        /// <param name="fromBlue">FROM 파란색</param>
        /// <param name="toAlpha">TO 투명도</param>
        /// <param name="toRed">TO 빨간색</param>
        /// <param name="toGreen">TO 녹색</param>
        /// <param name="toBlue">TO 파란색</param>
        private void SetSurroundColorArray(Color[] surroundColorArray, ref int index, float stopPoint,
            int fromAlpha, int fromRed, int fromGreen, int fromBlue, int toAlpha, int toRed, int toGreen, int toBlue)
        {
            int pointCount = (int)stopPoint - index;

            float alpha = fromAlpha;
            float red   = fromRed;
            float green = fromGreen;
            float blue  = fromBlue;

            float deltaAlpha = (toAlpha - fromAlpha) / (pointCount - 1);
            float deltaRed   = (toRed   - fromRed  ) / (pointCount - 1);
            float deltaGreen = (toGreen - fromGreen) / (pointCount - 1);
            float deltaBlue  = (toBlue  - fromBlue ) / (pointCount - 1);

            for(int i = 0; i < pointCount; i++)
            {
                surroundColorArray[index++] = Color.FromArgb((int)alpha, (int)red, (int)green, (int)blue);

                alpha += deltaAlpha;
                red   += deltaRed;
                green += deltaGreen;
                blue  += deltaBlue;
            }
        }

        #endregion
        #region 색상 휠 그리기 - DrawColorWheel(graphics, outlineColor, minimumX, minimumY, width, height)

        /// <summary>
        /// 색상 휠 그리기
        /// </summary>
        /// <param name="graphics">그래픽스</param>
        /// <param name="outlineColor">외곽선 색상</param>
        /// <param name="minimumX">최소 X 좌표</param>
        /// <param name="minimumY">최소 Y 좌표</param>
        /// <param name="width">너비</param>
        /// <param name="height">높이</param>
        private void DrawColorWheel(Graphics graphics, Color outlineColor, int minimumX, int minimumY, int width, int height)
        {
            Rectangle rectangle = new Rectangle(minimumX, minimumY, width, height);

            GraphicsPath wheelGraphicsPath = new GraphicsPath();

            wheelGraphicsPath.AddEllipse(rectangle);

            wheelGraphicsPath.Flatten();

            float pointCount = (wheelGraphicsPath.PointCount - 1) / 6;

            Color[] surroundColorArray = new Color[wheelGraphicsPath.PointCount];

            int index = 0;

            SetSurroundColorArray(surroundColorArray, ref index, 1 * pointCount              , 255, 255, 0  , 0  , 255, 255, 0  , 255);
            SetSurroundColorArray(surroundColorArray, ref index, 2 * pointCount              , 255, 255, 0  , 255, 255, 0  , 0  , 255);
            SetSurroundColorArray(surroundColorArray, ref index, 3 * pointCount              , 255, 0  , 0  , 255, 255, 0  , 255, 255);
            SetSurroundColorArray(surroundColorArray, ref index, 4 * pointCount              , 255, 0  , 255, 255, 255, 0  , 255, 0  );
            SetSurroundColorArray(surroundColorArray, ref index, 5 * pointCount              , 255, 0  , 255, 0  , 255, 255, 255, 0  );
            SetSurroundColorArray(surroundColorArray, ref index, wheelGraphicsPath.PointCount, 255, 255, 255, 0  , 255, 255, 0  , 0  );

            using(PathGradientBrush pathBrush = new PathGradientBrush(wheelGraphicsPath))
            {
                pathBrush.CenterColor    = Color.White;
                pathBrush.SurroundColors = surroundColorArray;

                graphics.FillPath(pathBrush, wheelGraphicsPath);

                using(Pen thickPen = new Pen(outlineColor, 2))
                {
                    graphics.DrawPath(thickPen, wheelGraphicsPath);
                }
            }
        }

        #endregion
    }
}
728x90
반응형
Posted by 사용자 icodebroker

댓글을 달아 주세요