■ ProgressBar 클래스에서 상태 진행바를 사용하는 방법을 보여준다.
▶ StateProgressBar.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
using System.Drawing; using System.Windows.Forms; using System.Windows.Forms.VisualStyles; namespace TestProject { /// <summary> /// 상태 진행바 /// </summary> public class StateProgressBar : ProgressBar { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 클래스명 /// </summary> private const string CLASS_NAME = "PROGRESS"; /// <summary> /// 렌더러 /// </summary> private VisualStyleRenderer renderer = null; /// <summary> /// 바 상태 /// </summary> private ProgressBarState barState = ProgressBarState.NORMAL; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 바 상태 - BarState /// <summary> /// 바 상태 /// </summary> public ProgressBarState BarState { get { return this.barState; } set { this.barState = value; } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - StateProgressBar() /// <summary> /// 생성자 /// </summary> public StateProgressBar() { base.SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true); if(Application.RenderWithVisualStyles && VisualStyleRenderer.IsElementDefined(VisualStyleElement.ProgressBar.Bar.Normal)) { this.renderer = new VisualStyleRenderer(VisualStyleElement.ProgressBar.Bar.Normal); } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 페인트시 처리하기 - OnPaint(e) /// <summary> /// 페인트시 처리하기 /// </summary> /// <param name="e">이벤트 인자</param> protected override void OnPaint(PaintEventArgs e) { if(this.renderer == null) { return; } int backgroundPart = (int)ProgressBarPart.TRANSPARENT_BAR; int backgroundState = (int)TransparentBarState.NORMAL; int foregroundPart = (int)ProgressBarPart.FILL; int foregroundState = (int)FillState.NORMAL; switch(this.barState) { case ProgressBarState.NORMAL : break; case ProgressBarState.ERROR : foregroundState = (int)FillState.ERROR; break; case ProgressBarState.PAUSED : foregroundState = (int)FillState.PAUSED; break; case ProgressBarState.PARTIAL : backgroundState = (int)TransparentBarState.PARTIAL; foregroundState = (int)FillState.PARTIAL; break; } this.renderer.SetParameters(CLASS_NAME, backgroundPart, backgroundState); this.renderer.DrawBackground(e.Graphics, this.ClientRectangle); int chunkWidth = (int)((double)ClientSize.Width * (double)(Value - Minimum) / (double)(Maximum - Minimum)); Rectangle chunkRectangle = new Rectangle(0, 0, chunkWidth, ClientSize.Height); this.renderer.SetParameters(CLASS_NAME, foregroundPart, foregroundState); this.renderer.DrawBackground(e.Graphics, chunkRectangle); } #endregion } } |