■ UserControl 클래스 : 환형 진행바 사용하기

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


TestProject.zip


CircularProgressBar.cs

 

 

using System;

using System.ComponentModel;

using System.Drawing;

using System.Drawing.Drawing2D;

using System.Windows.Forms;

 

namespace TestProject

{

    /// <summary>

    /// 환형 진행바

    /// </summary>

    public partial class CircularProgressBar : UserControl

    {

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

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

 

        #region Field

 

        /// <summary>

        /// 펜 너비

        /// </summary>

        private int penWidth;

 

        /// <summary>

        /// 배경 펜 색상

        /// </summary>

        private Color backgroundPenColor;

 

        /// <summary>

        /// 전경 펜 색상

        /// </summary>

        private Color foregroundPenColor;

 

        /// <summary>

        /// 배경 펜

        /// </summary>

        private Pen backgroundPen;

 

        /// <summary>

        /// 전경 펜

        /// </summary>

        private Pen foregroundPen;

 

        /// <summary>

        /// 최대 값

        /// </summary>

        private int maximumValue;

 

        /// <summary>

        ///

        /// </summary>

        private int value;

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Property

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

 

        #region 펜 너비 - PenWidth

 

        /// <summary>

        /// 펜 너비

        /// </summary>

        [Category("CircularProgressBar")]

        [Browsable(true)]

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]

        [Description("펜 너비")]

        public int PenWidth

        {

            get

            {

                return this.penWidth;

            }

            set

            {

                this.penWidth = value;

                

                this.backgroundPen = new Pen(this.backgroundPenColor, this.penWidth);

                this.foregroundPen = new Pen(this.foregroundPenColor, this.penWidth);

 

                Invalidate();

            }

        }

 

        #endregion

        #region 배경 펜 색상 - BackgroundPenColor

 

        /// <summary>

        /// 배경 펜 색상

        /// </summary>

        [Category("CircularProgressBar")]

        [Browsable(true)]

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]

        [Description("펜 색상")]

        public Color BackgroundPenColor

        {

            get

            {

                return this.backgroundPenColor;

            }

            set

            {

                this.backgroundPenColor = value;

                

                this.backgroundPen = new Pen(this.backgroundPenColor, this.penWidth);

 

                Invalidate();

            }

        }

 

        #endregion

        #region 전경 펜 색상 - ForegroundPenColor

 

        /// <summary>

        /// 전경 펜 색상

        /// </summary>

        [Category("CircularProgressBar")]

        [Browsable(true)]

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]

        [Description("전경 펜 색상")]

        public Color ForegroundPenColor

        {

            get

            {

                return foregroundPenColor;

            }

            set

            {

                foregroundPenColor = value;

                

                this.foregroundPen = new Pen(this.foregroundPenColor, this.penWidth);

 

                Invalidate();

            }

        }

 

        #endregion

        #region 최대 값 - MaximumValue

 

        /// <summary>

        /// 최대 값

        /// </summary>

        [Category("CircularProgressBar")]

        [Browsable(true)]

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]

        [Description("최대 값")]

        public int MaximumValue

        {

            get

            {

                return this.maximumValue;

            }

            set

            {

                this.maximumValue = value;

                

                Invalidate();

            }

        }

 

        #endregion

        #region 값 - Value

 

        /// <summary>

        ///

        /// </summary>

        [Category("CircularProgressBar")]

        [Browsable(true)]

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]

        [Description("값")]

        public int Value

        {

            get

            {

                return this.value;

            }

            set

            {

                this.value = value;

                

                Invalidate();

            }

        }

 

        #endregion

 

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

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

 

        #region 생성자 - CircularProgressBar()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public CircularProgressBar()

        {

            InitializeComponent();

 

            this.penWidth           = 10;

            this.backgroundPenColor = Color.LightGray;

            this.backgroundPen      = new Pen(this.backgroundPenColor, this.penWidth);

            this.foregroundPenColor = Color.Orange;

            this.foregroundPen      = new Pen(this.foregroundPenColor, this.penWidth);

            this.maximumValue       = 100;

            this.value              = 0;

 

            Load  += UserControl_Load;

            Paint += UserControl_Paint;

        }

 

        #endregion

 

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

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

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

 

        #region 사용자 컨트롤 로드시 처리하기 - UserControl_Load(sender, e)

 

        /// <summary>

        /// 사용자 컨트롤 로드시 처리하기

        /// </summary>

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

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

        private void UserControl_Load(object sender, EventArgs e)

        {

            DoubleBuffered = true;

 

            Invalidate();

        }

 

        #endregion

        #region 사용자 컨트롤 페인트시 처리하기 - UserControl_Paint(sender, e)

 

        /// <summary>

        /// 사용자 컨트롤 페인트시 처리하기

        /// </summary>

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

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

        private void UserControl_Paint(object sender, PaintEventArgs e)

        {

            Graphics graphics = e.Graphics;

 

            graphics.SmoothingMode = SmoothingMode.AntiAlias;

 

            graphics.DrawEllipse(this.backgroundPen, 10, 10, Width - 20,Height - 20);

 

            int progressValue = GetProgresValue(this.value, this.maximumValue);

 

            graphics.DrawArc(this.foregroundPen, 10, 10, Width - 20, Height - 20 , -90, progressValue);

        }

 

        #endregion

 

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

 

        #region 진행 값 구하기 - GetProgresValue(value , maximumValue)

 

        /// <summary>

        /// 진행 값 구하기

        /// </summary>

        /// <param name="value"></param>

        /// <param name="maximumValue">최대 값</param>

        /// <returns>진행</returns>

        private int GetProgresValue(int value , int maximumValue)

        {

            return value * 360 / maximumValue;

        }

 

        #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();

 

            this.leftCircularProgressBar.ForegroundPenColor = Color.BlueViolet;

 

            this.timer.Tick += timer_Tick;

 

            this.timer.Start();

        }

 

        #endregion

 

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

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

 

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

 

        /// <summary>

        /// 타이머 틱 처리하기

        /// </summary>

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

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

        private void timer_Tick(object sender, EventArgs e)

        {

            if(this.leftCircularProgressBar.Value >= 100)

            {

                this.leftCircularProgressBar.Value = 0;

            }

 

            if(this.rightCircularProgressBar.Value >= 100)

            {

                this.rightCircularProgressBar.Value = 0;

            }

 

            this.leftCircularProgressBar.Value  += 1;

            this.rightCircularProgressBar.Value += 3;

        }

 

        #endregion

    }

}

 

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

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요