■ Label 클래스에서 외곽선 텍스트를 사용하는 방법을 보여준다.
▶ OutlineTextLabel.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 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
using System; using System.ComponentModel; using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms; namespace TestProject { /// <summary> /// 외곽선 레이블 /// </summary> public partial class OutlineTextLabel : Label { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 테두리 크기 /// </summary> private float borderSize; /// <summary> /// 테두리 색상 /// </summary> private Color borderColor; /// <summary> /// 전경색 브러시 /// </summary> private SolidBrush foregroundColorBrush; /// <summary> /// 그리기 크기 /// </summary> private SizeF drawSize; /// <summary> /// 그리기 포인트 /// </summary> private PointF drawPoint; /// <summary> /// 그리기 펜 /// </summary> private Pen drawPen; /// <summary> /// 그리기 경로 /// </summary> private GraphicsPath drawPath; /// <summary> /// 빈 포인트 /// </summary> private PointF emptyPoint = PointF.Empty; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 테두리 크기 - BorderSize /// <summary> /// 테두리 크기 /// </summary> [Category("OutlineTextLabel")] [Browsable(true)] [DefaultValue(1f)] [Description("테두리 두께")] public float BorderSize { get { return this.borderSize; } set { this.borderSize = value; if(value == 0) { this.drawPen.Color = Color.Transparent; } else { this.drawPen.Color = this.BorderColor; this.drawPen.Width = value; } OnTextChanged(EventArgs.Empty); } } #endregion #region 테두리 색상 - BorderColor /// <summary> /// 테두리 색상 /// </summary> [Category("OutlineTextLabel")] [Browsable(true)] [DefaultValue(typeof(Color), "White")] [Description("테두리 색상")] public Color BorderColor { get { return this.borderColor; } set { this.borderColor = value; if(this.BorderSize != 0) { this.drawPen.Color = value; } Invalidate(); } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - OutlineTextLabel() /// <summary> /// 생성자 /// </summary> public OutlineTextLabel() { this.borderSize = 1f; this.borderColor = Color.White; this.drawPen = new Pen(new SolidBrush(this.borderColor), this.borderSize); this.drawPath = new GraphicsPath(); this.foregroundColorBrush = new SolidBrush(this.ForeColor); Invalidate(); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 전경색 변경시 처리하기 - OnForeColorChanged(e) /// <summary> /// 전경색 변경시 처리하기 /// </summary> /// <param name="e">이벤트 발생자</param> protected override void OnForeColorChanged(EventArgs e) { this.foregroundColorBrush.Color = base.ForeColor; base.OnForeColorChanged(e); } #endregion #region 페인트시 처리하기 - OnPaint(e) /// <summary> /// 페인트시 처리하기 /// </summary> /// <param name="e">이벤트 발생자</param> protected override void OnPaint(PaintEventArgs e) { if(Text.Length == 0) { return; } e.Graphics.SmoothingMode = SmoothingMode.AntiAlias; e.Graphics.CompositingQuality = CompositingQuality.HighQuality; this.drawSize = e.Graphics.MeasureString ( Text, Font, this.emptyPoint, StringFormat.GenericTypographic ); if(AutoSize) { this.drawPoint.X = this.Padding.Left; this.drawPoint.Y = this.Padding.Top; } else { if ( TextAlign == ContentAlignment.TopLeft || TextAlign == ContentAlignment.MiddleLeft || TextAlign == ContentAlignment.BottomLeft ) { this.drawPoint.X = this.Padding.Left; } else if ( TextAlign == ContentAlignment.TopCenter || TextAlign == ContentAlignment.MiddleCenter || TextAlign == ContentAlignment.BottomCenter ) { this.drawPoint.X = (Width - this.drawSize.Width) / 2; } else { this.drawPoint.X = Width - (Padding.Right + this.drawSize.Width); } if ( TextAlign == ContentAlignment.TopLeft || TextAlign == ContentAlignment.TopCenter || TextAlign == ContentAlignment.TopRight ) { this.drawPoint.Y = Padding.Top; } else if ( TextAlign == ContentAlignment.MiddleLeft || TextAlign == ContentAlignment.MiddleCenter || TextAlign == ContentAlignment.MiddleRight ) { this.drawPoint.Y = (Height - this.drawSize.Height) / 2; } else { this.drawPoint.Y = Height - (Padding.Bottom + this.drawSize.Height); } } float fontSize = e.Graphics.DpiY * Font.SizeInPoints / 72; this.drawPath.Reset(); this.drawPath.AddString ( Text, Font.FontFamily, (int)Font.Style, fontSize, this.drawPoint, StringFormat.GenericTypographic ); e.Graphics.FillPath(this.foregroundColorBrush, this.drawPath); e.Graphics.DrawPath(this.drawPen, this.drawPath); } #endregion #region 리소스 해제하기 - Dispose(disposing) /// <summary> /// 리소스 해제하기 /// </summary> /// <param name="disposing">리소스 해제 여부</param> protected override void Dispose(bool disposing) { if(disposing) { if(this.foregroundColorBrush != null) { this.foregroundColorBrush.Dispose(); } if(this.drawPath != null) { this.drawPath.Dispose(); } if(this.drawPen != null) { this.drawPen.Dispose(); } } base.Dispose(disposing); } #endregion } } |