■ 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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
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
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 141 142 143 144 145 146 147 148 149 150 151 152 153 |
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 } } |