该控件是继承于 Control 基类开发的。这个控件没有什么技术性的东西。因为功能比较简单。主要重写了 OnPaint 方法如下
1 protected override void OnPaint(PaintEventArgs e) 2 { 3 base.OnPaint(e); 4 5 Graphics g = e.Graphics; 6 7 int text_padding = 2; 8 SizeF text_size = SizeF.Empty; 9 RectangleF text_rect = RectangleF.Empty; 10 11 Pen line_pen = new Pen(this.LineColor, this.LineThickness); 12 int circular = this.lineCircular ? this.LineThickness : 0; 13 PointF line_left_s = PointF.Empty; 14 PointF line_left_e = PointF.Empty; 15 PointF line_right_s = PointF.Empty; 16 PointF line_right_e = PointF.Empty; 17 18 #region 文字 19 if (!String.IsNullOrEmpty(this.Text)) 20 { 21 SolidBrush text_sb = new SolidBrush(this.ForeColor); 22 StringFormat text_sf = null; 23 24 #region 25 if (this.LineOrientation == LineOrientations.Horizontal) 26 { 27 text_size = g.MeasureString(this.Text, this.Font, 1000, text_sf); 28 #region 29 float y = 0; 30 if (this.textAlign == TextAligns.Top) 31 { 32 y = (this.ClientRectangle.Height - (this.lineThickness + text_size.Height)) / 2f; 33 } 34 else if (this.textAlign == TextAligns.Center) 35 { 36 y = (this.ClientRectangle.Height - text_size.Height) / 2f; 37 } 38 else if (this.textAlign == TextAligns.Bottom) 39 { 40 y = (this.ClientRectangle.Height - (this.lineThickness + text_padding + text_size.Height)) / 2f + this.lineThickness+ text_padding; 41 } 42 #endregion 43 if (this.TextOrientation == TextOrientations.Left) 44 { 45 text_rect = new RectangleF(this.ClientRectangle.X + this.textDistance, y, text_size.Width, text_size.Height); 46 } 47 else 48 { 49 text_rect = new RectangleF(this.ClientRectangle.Right - this.textDistance - text_size.Width, y, text_size.Width, text_size.Height); 50 } 51 } 52 #endregion 53 #region 54 else 55 { 56 text_sf = new StringFormat() { FormatFlags = StringFormatFlags.DirectionVertical }; 57 text_size = g.MeasureString(this.Text, this.Font, 1000, text_sf); 58 #region 59 float x = 0; 60 if (this.textAlign == TextAligns.Top) 61 { 62 x = (this.ClientRectangle.Width - (this.lineThickness + text_size.Width)) / 2f; 63 } 64 else if (this.textAlign == TextAligns.Center) 65 { 66 x = (this.ClientRectangle.Width - text_size.Width) / 2f; 67 } 68 else if (this.textAlign == TextAligns.Bottom) 69 { 70 x = (this.ClientRectangle.Width - (this.lineThickness + text_padding + text_size.Width)) / 2f + this.lineThickness + text_padding; 71 } 72 #endregion 73 if (this.TextOrientation == TextOrientations.Left) 74 { 75 text_rect = new RectangleF(x, this.ClientRectangle.Y + this.textDistance, text_size.Width, text_size.Height); 76 } 77 else 78 { 79 text_rect = new RectangleF(x, this.ClientRectangle.Bottom - this.textDistance - text_size.Height, text_size.Width, text_size.Height); 80 } 81 } 82 #endregion 83 84 g.DrawString(this.Text, this.Font, text_sb, text_rect, text_sf); 85 text_sb.Dispose(); 86 if (text_sf != null) 87 { 88 text_sf.Dispose(); 89 } 90 } 91 #endregion 92 93 #region 线条 94 #region 两条线 95 if (!String.IsNullOrEmpty(this.Text) && this.textAlign == TextAligns.Center) 96 { 97 if (this.LineOrientation == LineOrientations.Horizontal) 98 { 99 #region 100 float y = 0; 101 if (this.textAlign == TextAligns.Top) 102 { 103 y = text_rect.Bottom + this.LineThickness / 2f; 104 } 105 else if (this.textAlign == TextAligns.Center) 106 { 107 y = this.ClientRectangle.Height / 2f; 108 } 109 else if (this.textAlign == TextAligns.Bottom) 110 { 111 y = text_rect.Y - this.LineThickness / 2f; 112 } 113 #endregion 114 line_left_s = new PointF(this.ClientRectangle.X + circular, y); 115 line_left_e = new PointF(text_rect.X - text_padding, y); 116 line_right_s = new PointF(text_rect.Right + text_padding, y); 117 line_right_e = new PointF(this.ClientRectangle.Right - circular, y); 118 } 119 else 120 { 121 #region 122 float x = 0; 123 if (this.textAlign == TextAligns.Top) 124 { 125 x = text_rect.Right + this.LineThickness / 2f; 126 } 127 else if (this.textAlign == TextAligns.Center) 128 { 129 x = this.ClientRectangle.Width / 2f; 130 } 131 else if (this.textAlign == TextAligns.Bottom) 132 { 133 x = text_rect.X - this.LineThickness / 2f; 134 } 135 #endregion 136 line_left_s = new PointF(x, this.ClientRectangle.Y + circular); 137 line_left_e = new PointF(x, text_rect.Y - text_padding); 138 line_right_s = new PointF(x, text_rect.Bottom + text_padding); 139 line_right_e = new PointF(x, this.ClientRectangle.Bottom - circular); 140 } 141 if (this.lineCircular) 142 { 143 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; 144 line_pen.StartCap = LineCap.Round; 145 g.DrawLine(line_pen, line_left_s, line_left_e); 146 line_pen.StartCap = LineCap.Flat; 147 line_pen.EndCap = LineCap.Round; 148 g.DrawLine(line_pen, line_right_s, line_right_e); 149 } 150 else 151 { 152 g.DrawLine(line_pen, line_left_s, line_left_e); 153 g.DrawLine(line_pen, line_right_s, line_right_e); 154 } 155 } 156 #endregion 157 #region 一条线 158 else 159 { 160 if (this.LineOrientation == LineOrientations.Horizontal) 161 { 162 #region 163 float y = 0; 164 if (this.textAlign == TextAligns.Top) 165 { 166 y = text_rect.Bottom + this.LineThickness / 2f; 167 } 168 else if (this.textAlign == TextAligns.Center) 169 { 170 y = this.ClientRectangle.Height / 2f; 171 } 172 else if (this.textAlign == TextAligns.Bottom) 173 { 174 y = text_rect.Y- text_padding - this.LineThickness / 2f; 175 } 176 #endregion 177 line_left_s = new PointF(this.ClientRectangle.X + circular, y); 178 line_left_e = new PointF(this.ClientRectangle.Right - circular, y); 179 } 180 else 181 { 182 #region 183 float x = 0; 184 if (this.textAlign == TextAligns.Top) 185 { 186 x = text_rect.Right + this.LineThickness / 2f; 187 } 188 else if (this.textAlign == TextAligns.Center) 189 { 190 x = this.ClientRectangle.Width / 2f; 191 } 192 else if (this.textAlign == TextAligns.Bottom) 193 { 194 x = text_rect.X - text_padding - this.LineThickness / 2f; 195 } 196 #endregion 197 line_left_s = new PointF(x, this.ClientRectangle.Y + circular); 198 line_left_e = new PointF(x, this.ClientRectangle.Bottom - circular); 199 } 200 if (this.lineCircular) 201 { 202 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; 203 line_pen.StartCap = LineCap.Round; 204 line_pen.EndCap = LineCap.Round; 205 } 206 g.DrawLine(line_pen, line_left_s, line_left_e); 207 } 208 #endregion 209 210 line_pen.Dispose(); 211 #endregion 212 213 }
新增属性如下