■ 시계 컨트롤을 만드는 방법을 보여준다.
▶ ClockControl.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 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms; namespace TestProject { /// <summary> /// 시계 컨트롤 /// </summary> public partial class ClockControl: UserControl { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 일시 /// </summary> private DateTime dateTime; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 일시 - DateTime /// <summary> /// 일시 /// </summary> public DateTime DateTime { get { return this.dateTime; } set { Graphics graphics = CreateGraphics(); InitializeCoordinate(graphics); Pen pen = new Pen(BackColor); if(this.dateTime.Hour != value.Hour) { DrawHourHand(graphics, pen); } if(this.dateTime.Minute != value.Minute) { DrawHourHand(graphics, pen); DrawMinuteHand(graphics, pen); } if(this.dateTime.Second != value.Second) { DrawMinuteHand(graphics, pen); DrawSecondHand(graphics, pen); } if(this.dateTime.Millisecond != value.Millisecond) { DrawSecondHand(graphics, pen); } this.dateTime = value; pen = new Pen(ForeColor); DrawHourHand(graphics, pen); DrawMinuteHand(graphics, pen); DrawSecondHand(graphics, pen); graphics.Dispose(); } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - ClockControl() /// <summary> /// 생성자 /// </summary> public ClockControl() { InitializeComponent(); ResizeRedraw = true; Enabled = false; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 페인트시 처리하기 - OnPaint(e) /// <summary> /// 페인트시 처리하기 /// </summary> /// <param name="e">이벤트 인자</param> protected override void OnPaint(PaintEventArgs e) { Graphics graphics = e.Graphics; Pen pen = new Pen(ForeColor); Brush brush = new SolidBrush(ForeColor); InitializeCoordinate(graphics); DrawDot(graphics, brush); DrawHourHand(graphics, pen); DrawMinuteHand(graphics, pen); DrawSecondHand(graphics, pen); } #endregion #region 시침 그리기 - DrawHourHand(graphics, pen) /// <summary> /// 시침 그리기 /// </summary> /// <param name="graphics">그래픽스</param> /// <param name="pen">펜</param> protected virtual void DrawHourHand(Graphics graphics, Pen pen) { GraphicsState graphicsState = graphics.Save(); graphics.RotateTransform(360f * DateTime.Hour / 12 + 30f * DateTime.Minute / 60); graphics.DrawPolygon ( pen, new Point[] { new Point( 0, 150), new Point( 100, 0), new Point( 0, -600), new Point(-100, 0) } ); graphics.Restore(graphicsState); } #endregion #region 분침 그리기 - DrawMinuteHand(graphics, pen) /// <summary> /// 분침 그리기 /// </summary> /// <param name="graphics">그래픽스</param> /// <param name="pen">펜</param> protected virtual void DrawMinuteHand(Graphics graphics, Pen pen) { GraphicsState graphicsState = graphics.Save(); graphics.RotateTransform(360f * DateTime.Minute / 60 + 6f * DateTime.Second / 60); graphics.DrawPolygon ( pen, new Point[] { new Point( 0, 200), new Point( 50, 0), new Point( 0, -800), new Point(-50, 0) } ); graphics.Restore(graphicsState); } #endregion #region 초침 그리기 - DrawSecondHand(graphics, pen) /// <summary> /// 초침 그리기 /// </summary> /// <param name="graphics">그래픽스</param> /// <param name="pen">펜</param> protected virtual void DrawSecondHand(Graphics graphics, Pen pen) { GraphicsState graphicsState = graphics.Save(); graphics.RotateTransform(360f * DateTime.Second / 60 + 6f * DateTime.Millisecond / 1000); graphics.DrawLine(pen, 0, 0, 0, -800); graphics.Restore(graphicsState); } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private #region 좌표계 초기화 하기 - InitializeCoordinate(graphics) /// <summary> /// 좌표계 초기화 하기 /// </summary> /// <param name="graphics">그래픽스</param> private void InitializeCoordinate(Graphics graphics) { if(Width == 0 || Height == 0) { return; } graphics.TranslateTransform(Width / 2, Height / 2); float inches = Math.Min(Width / graphics.DpiX, Height / graphics.DpiY); graphics.ScaleTransform ( inches * graphics.DpiX / 2000, inches * graphics.DpiY / 2000 ); } #endregion #region 도트 그리기 - DrawDot(graphics, brush) /// <summary> /// 도트 그리기 /// </summary> /// <param name="graphics">그래픽스</param> /// <param name="brush">브러시</param> private void DrawDot(Graphics graphics, Brush brush) { for(int i = 0; i < 60; i++) { int size = i % 5 == 0 ? 100 : 30; graphics.FillEllipse(brush, 0 - size / 2, -900 - size / 2, size, size); graphics.RotateTransform(6); } } #endregion } } |