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

■ ProgressBar 클래스 : 색상 변경하기

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


TestProject.zip


StateProgressBar.cs

 

 

using System;

using System.Drawing;

using System.Drawing.Drawing2D;

using System.Windows.Forms;

 

namespace TestProject

{

    /// <summary>

    /// 상태 진행바

    /// </summary>

    public class StateProgressBar : ProgressBar

    {

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

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

 

        #region 생성자 - StateProgressBar()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public StateProgressBar()

        {

            if(ProgressBarRenderer.IsSupported)

            {

                SetStyle(ControlStyles.UserPaint, true);

            }

        }

 

        #endregion

 

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

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

 

        #region 배경 페인트시 처리하기 - OnPaintBackground(e)

 

        /// <summary>

        /// 배경 페인트시 처리하기

        /// </summary>

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

        protected override void OnPaintBackground(PaintEventArgs e)

        {

        }

 

        #endregion

        #region 페인트시 처리하기 - OnPaint(e)

 

        /// <summary>

        /// 페인트시 처리하기

        /// </summary>

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

        protected override void OnPaint(PaintEventArgs e)

        {

            using(Image image = new Bitmap(Width, Height))

            {

                using(Graphics imageGraphics = Graphics.FromImage(image))

                {

                    #region 외곽선을 그린다.

 

                    Rectangle outlineRectangle = new Rectangle(Point.Empty, Size);

 

                    if(ProgressBarRenderer.IsSupported)

                    {

                        ProgressBarRenderer.DrawHorizontalBar(imageGraphics, outlineRectangle);

                    }

 

                    #endregion

                    #region 배경바를 칠한다.

 

                    outlineRectangle.Inflate(new Size(-2, -2));

 

                    SolidBrush backgroundBrush = new SolidBrush(Color.FromArgb(251, 251, 251));

 

                    imageGraphics.FillRectangle(backgroundBrush, outlineRectangle);

 

                    #endregion

                    #region 메인바(수직 그라데이션)를 칠한다.


                    Rectangle mainRectangle = new Rectangle(Point.Empty, Size);

 

                    mainRectangle.Inflate(new Size(-1, -1));

 

                    mainRectangle.Width = (int)Math.Truncate((double)mainRectangle.Width * Value / Maximum);

 

                    if(mainRectangle.Width == 0)

                    {

                        mainRectangle.Width = 1;

                    }

 

                    LinearGradientBrush verticalMainBrush = new LinearGradientBrush(mainRectangle, this.BackColor, this.ForeColor, 

                        LinearGradientMode.Vertical);

 

                    ColorBlend verticalMainColorBlend = new ColorBlend

                    {

                        Positions = new float[]

                        {

                            0.0f,

                            0.55f,

                            1.0f

                        },

                        Colors = new Color[]

                        {

                            this.BackColor,

                            this.ForeColor,

                            this.BackColor

                        }

                    };

 

                    verticalMainBrush.InterpolationColors = verticalMainColorBlend;

 

                    imageGraphics.FillRectangle(verticalMainBrush, mainRectangle);

 

                    #endregion

                    #region 메인바(수평 그라데이션)를 칠한다.

 

                    LinearGradientBrush horizontalMainBrush = new LinearGradientBrush(mainRectangle, this.ForeColor, this.BackColor, 

                        LinearGradientMode.Horizontal);

 

                    ColorBlend horizontalMainColorBlend = new ColorBlend

                    {

                        Positions = new float[]

                        {

                            0.0f,

                            0.35f,

                            0.65f,

                            1.0f

                        },

                        Colors = new Color[]

                        {

                            Color.FromArgb(200, this.ForeColor),

                            Color.FromArgb(100, this.BackColor),

                            Color.FromArgb(100, this.BackColor),

                            Color.FromArgb(200, this.ForeColor)

                        }

                    };

 

                    horizontalMainBrush.InterpolationColors = horizontalMainColorBlend;

 

                    horizontalMainBrush.GammaCorrection = true;

 

                    imageGraphics.FillRectangle(horizontalMainBrush, mainRectangle);

 

                    #endregion

                    #region 사이드 엣지(투명)를 그린다.


                    Pen sideEdgePen = new Pen(Color.FromArgb(80, Color.White));

 

                    imageGraphics.DrawLine(sideEdgePen, 1                  , 1, 1                  , mainRectangle.Height); // 왼쪽

                    imageGraphics.DrawLine(sideEdgePen, mainRectangle.Width, 1, mainRectangle.Width, mainRectangle.Height); // 오른쪽

 

                    #endregion

                    #region 하이라이트(투명)를 그린다.

 

                    Rectangle highlightRectangle = new Rectangle(1, 1, mainRectangle.Width,

                        (int)(Math.Truncate(mainRectangle.Height * 0.45)));

 

                    LinearGradientBrush highlightBrush = new LinearGradientBrush(highlightRectangle, Color.White, Color.White, 

                        LinearGradientMode.Vertical);

 

                    ColorBlend highlightColorBlend = new ColorBlend

                    {

                        Positions = new float[]

                        {

                            0.0f,

                            0.3f,

                            1.0f

                        },

                        Colors = new Color[]

                        {

                            Color.FromArgb(120, Color.White),

                            Color.FromArgb(110, Color.White),

                            Color.FromArgb(80, Color.White)

                        }

                    };

 

                    highlightBrush.InterpolationColors = highlightColorBlend;

 

                    highlightBrush.GammaCorrection = true;

 

                    imageGraphics.FillRectangle(highlightBrush, highlightRectangle);

 

                    #endregion

 

                    // 이미지를 그린다.

                    e.Graphics.DrawImage(image, Point.Empty);

                }

            }

        }

 

        #endregion

    }

}

 

 

MainForm.cs

 

 

using System;

using System.Drawing;

using System.Windows.Forms;

 

namespace TestProject

{

    /// <summary>

    /// 메인 폼

    /// </summary>

    public partial class MainForm : Form

    {

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

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

 

        #region 생성자 - MainForm()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainForm()

        {

            InitializeComponent();

 

            if(!ProgressBarRenderer.IsSupported)

            {

                this.progressBar.BackColor = SystemColors.Control;

            }

 

            #region 이벤트를 설정한다.

 

            this.turquoiseButton.Click += turquoiseButton_Click;

            this.blueButton.Click      += blueButton_Click;

            this.greenButton.Click     += greenButton_Click;

            this.yellowButton.Click    += yellowButton_Click;

            this.redButton.Click       += redButton_Click;

            this.trackBar.Scroll       += trackBar_Scroll;

 

            #endregion

        }

 

        #endregion

 

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

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

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

 

        #region 터키색 버튼 클릭시 처리하기 - turquoiseButton_Click(sender, e)

 

        /// <summary>

        /// 터키색 버튼 클릭시 처리하기

        /// </summary>

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

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

        private void turquoiseButton_Click(object sender, EventArgs e)

        {

            SetColor(Color.MediumTurquoise, Color.DarkCyan);

        }

 

        #endregion

        #region 파랑 버튼 클릭시 처리하기 - blueButton_Click(sender, e)

 

        /// <summary>

        /// 파랑 버튼 클릭시 처리하기

        /// </summary>

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

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

        private void blueButton_Click(object sender, EventArgs e)

        {

            SetColor(Color.LightSkyBlue, Color.DodgerBlue);

        }

 

        #endregion

        #region 녹색 버튼 클릭시 처리하기 - greenButton_Click(sender, e)

 

        /// <summary>

        /// 녹색 버튼 클릭시 처리하기

        /// </summary>

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

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

        private void greenButton_Click(object sender, EventArgs e)

        {

            SetColor(Color.LimeGreen, Color.ForestGreen);

        }

 

        #endregion

        #region 노랑 버튼 클릭시 처리하기 - yellowButton_Click(sender, e)

 

        /// <summary>

        /// 노랑 버튼 클릭시 처리하기

        /// </summary>

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

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

        private void yellowButton_Click(object sender, EventArgs e)

        {

            SetColor(Color.Khaki, Color.Gold);

        }

 

        #endregion

        #region 빨강 버튼 클릭시 처리하기 - redButton_Click(sender, e)

 

        /// <summary>

        /// 빨강 버튼 클릭시 처리하기

        /// </summary>

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

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

        private void redButton_Click(object sender, EventArgs e)

        {

            SetColor(Color.Red, Color.Firebrick);

        }

 

        #endregion

        #region 트랙바 스크롤시 처리하기 - trackBar_Scroll(sender, e)

 

        /// <summary>

        /// 트랙바 스크롤시 처리하기

        /// </summary>

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

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

        private void trackBar_Scroll(object sender, EventArgs e)

        {

            this.progressBar.Value = trackBar.Value;

        }

 

        #endregion

 

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

 

        #region 색상 설정하기 - SetColor(backgroundColor, foregroungColor)

 

        /// <summary>

        /// 색상 설정하기

        /// </summary>

        /// <param name="backgroundColor">배경색</param>

        /// <param name="foregroungColor">전경색</param>

        private void SetColor(Color backgroundColor, Color foregroungColor)

        {

            if (ProgressBarRenderer.IsSupported)

            {

                progressBar.BackColor = backgroundColor;

                progressBar.ForeColor = foregroungColor;

            }

            else

            {

                progressBar.BackColor = SystemColors.Control;

                progressBar.ForeColor = backgroundColor;

            }

        }

 

        #endregion

    }

}

 

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

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요