该控件是继承于 Control 基类开发的。
1 /// <summary> 2 /// 分割线控件 3 /// </summary> 4 [ToolboxItem(true)] 5 [Description("分割线控件")] 6 [DefaultProperty("Text")] 7 [DefaultEvent("TextChanged")] 8 public partial class HalvingLineExt : Control 9 { 10 #region 停用事件 11 12 [Browsable(false)] 13 [EditorBrowsable(EditorBrowsableState.Never)] 14 public new event EventHandler BackgroundImageChanged; 15 16 [Browsable(false)] 17 [EditorBrowsable(EditorBrowsableState.Never)] 18 public new event EventHandler BackgroundImageLayoutChanged; 19 20 [Browsable(false)] 21 [EditorBrowsable(EditorBrowsableState.Never)] 22 public new event EventHandler Click; 23 24 [Browsable(false)] 25 [EditorBrowsable(EditorBrowsableState.Never)] 26 public new event EventHandler DoubleClick; 27 28 [Browsable(false)] 29 [EditorBrowsable(EditorBrowsableState.Never)] 30 public new event MouseEventHandler MouseClick; 31 32 [Browsable(false)] 33 [EditorBrowsable(EditorBrowsableState.Never)] 34 public new event MouseEventHandler MouseDoubleClick; 35 36 [Browsable(false)] 37 [EditorBrowsable(EditorBrowsableState.Never)] 38 public new event UICuesEventHandler ChangeUICues; 39 40 [Browsable(false)] 41 [EditorBrowsable(EditorBrowsableState.Never)] 42 public new event MouseEventHandler ImeModeChanged; 43 44 [Browsable(false)] 45 [EditorBrowsable(EditorBrowsableState.Never)] 46 public new event EventHandler Validated; 47 48 [Browsable(false)] 49 [EditorBrowsable(EditorBrowsableState.Never)] 50 public new event CancelEventHandler Validating; 51 52 [Browsable(false)] 53 [EditorBrowsable(EditorBrowsableState.Never)] 54 public new event EventHandler Enter; 55 56 [Browsable(false)] 57 [EditorBrowsable(EditorBrowsableState.Never)] 58 public new event EventHandler Leave; 59 60 [Browsable(false)] 61 [EditorBrowsable(EditorBrowsableState.Never)] 62 public new event KeyEventHandler KeyDown; 63 64 [Browsable(false)] 65 [EditorBrowsable(EditorBrowsableState.Never)] 66 public new event KeyPressEventHandler KeyPress; 67 68 [Browsable(false)] 69 [EditorBrowsable(EditorBrowsableState.Never)] 70 public new event KeyEventHandler KeyUp; 71 72 [Browsable(false)] 73 [EditorBrowsable(EditorBrowsableState.Never)] 74 public new event PreviewKeyDownEventHandler PreviewKeyDown; 75 76 [Browsable(false)] 77 [EditorBrowsable(EditorBrowsableState.Never)] 78 public new event EventHandler TabIndexChanged; 79 80 [Browsable(false)] 81 [EditorBrowsable(EditorBrowsableState.Never)] 82 public new event EventHandler TabStopChanged; 83 84 [Browsable(false)] 85 [EditorBrowsable(EditorBrowsableState.Never)] 86 public new event DragEventHandler DragDrop; 87 88 [Browsable(false)] 89 [EditorBrowsable(EditorBrowsableState.Never)] 90 public new event DragEventHandler DragEnter; 91 92 [Browsable(false)] 93 [EditorBrowsable(EditorBrowsableState.Never)] 94 public new event EventHandler DragLeave; 95 96 [Browsable(false)] 97 [EditorBrowsable(EditorBrowsableState.Never)] 98 public new event DragEventHandler DragOver; 99 100 [Browsable(false)]101 [EditorBrowsable(EditorBrowsableState.Never)]102 public new event GiveFeedbackEventHandler GiveFeedback;103 104 [Browsable(false)]105 [EditorBrowsable(EditorBrowsableState.Never)]106 public new event QueryContinueDragEventHandler QueryContinueDrag;107 108 [Browsable(false)]109 [EditorBrowsable(EditorBrowsableState.Never)]110 public new event EventHandler RightToLeftChanged;111 112 [Browsable(false)]113 [EditorBrowsable(EditorBrowsableState.Never)]114 public new event EventHandler ContextMenuStripChanged;115 116 #endregion117 118 #region 新增属性119 120 #region 线条121 122 private LineOrientations lineOrientation = LineOrientations.Horizontal;123 /// <summary>124 ///线条方向125 /// </summary>126 [DefaultValue(LineOrientations.Horizontal)]127 [Description("线条方向")]128 public LineOrientations LineOrientation129 {130 get { return this.lineOrientation; }131 set132 {133 if (this.lineOrientation == value)134 return;135 136 this.lineOrientation = value;137 if (this.DesignMode)138 {139 this.SetBounds(this.Location.X, this.Location.Y, this.Height, this.Width, BoundsSpecified.Size);140 }141 this.Invalidate();142 }143 }144 145 private int lineThickness = 2;146 /// <summary>147 ///线条厚度148 /// </summary>149 [DefaultValue(2)]150 [Description("线条厚度")]151 public int LineThickness152 {153 get { return this.lineThickness; }154 set155 {156 if (this.lineThickness == value || value < 1)157 return;158 159 this.lineThickness = value;160 this.Invalidate();161 }162 }163 164 private bool lineCircular = false;165 /// <summary>166 ///线条圆角167 /// </summary>168 [DefaultValue(false)]169 [Description("线条圆角")]170 public bool LineCircular171 {172 get { return this.lineCircular; }173 set174 {175 if (this.lineCircular == value)176 return;177 178 this.lineCircular = value;179 this.Invalidate();180 }181 }182 183 private Color lineColor = Color.YellowGreen;184 /// <summary>185 /// 线条颜色186 /// </summary>187 [DefaultValue(typeof(Color), "YellowGreen")]188 [Description("线条颜色")]189 [Editor(typeof(ColorEditorExt), typeof(System.Drawing.Design.UITypeEditor))]190 public Color LineColor191 {192 get { return this.lineColor; }193 set194 {195 if (this.lineColor == value)196 return;197 this.lineColor = value;198 this.Invalidate();199 }200 }201 202 #endregion203 204 #region 文本205 206 private TextOrientations textOrientation = TextOrientations.Left;207 /// <summary>208 ///文本方向209 /// </summary>210 [DefaultValue(TextOrientations.Left)]211 [Description("文本方向")]212 public TextOrientations TextOrientation213 {214 get { return this.textOrientation; }215 set216 {217 if (this.textOrientation == value)218 return;219 220 this.textOrientation = value;221 this.Invalidate();222 }223 }224 225 private TextAligns textAlign = TextAligns.Center;226 /// <summary>227 ///文本对齐方式228 /// </summary>229 [DefaultValue(TextAligns.Center)]230 [Description("文本对齐方式")]231 public TextAligns TextAlign232 {233 get { return this.textAlign; }234 set235 {236 if (this.textAlign == value)237 return;238 239 this.textAlign = value;240 this.Invalidate();241 }242 }243 244 private int textDistance = 40;245 /// <summary>246 ///文本间距247 /// </summary>248 [DefaultValue(40)]249 [Description("文本间距")]250 public int TextDistance251 {252 get { return this.textDistance; }253 set254 {255 if (this.textDistance == value || value < 0)256 return;257 this.textDistance = value;258 this.Invalidate();259 }260 }261 262 #endregion263 264 #endregion265 266 #region 停用属性267 268 [Browsable(false)]269 [EditorBrowsable(EditorBrowsableState.Never)]270 public new Image BackgroundImage { get; set; }271 272 [Browsable(false)]273 [EditorBrowsable(EditorBrowsableState.Never)]274 public new ImageLayout BackgroundImageLayout { get; set; }275 276 [Browsable(false)]277 [EditorBrowsable(EditorBrowsableState.Never)]278 public new int TabIndex279 {280 get { return 0; }281 set { base.TabIndex = 0; }282 }283 284 [Browsable(false)]285 [EditorBrowsable(EditorBrowsableState.Never)]286 public new bool TabStop287 {288 get { return false; }289 set { base.TabStop = false; }290 }291 292 [Browsable(false)]293 [EditorBrowsable(EditorBrowsableState.Never)]294 public new bool CausesValidation { get; set; }295 296 [Browsable(false)]297 [EditorBrowsable(EditorBrowsableState.Never)]298 public override bool AllowDrop299 {300 get301 {302 return base.AllowDrop;303 }304 set305 {306 base.AllowDrop = value;307 }308 }309 310 [Browsable(false)]311 [EditorBrowsable(EditorBrowsableState.Never)]312 public override RightToLeft RightToLeft313 {314 get315 {316 return base.RightToLeft;317 }318 set319 {320 base.RightToLeft = value;321 }322 }323 324 [Browsable(false)]325 [EditorBrowsable(EditorBrowsableState.Never)]326 public override ContextMenuStrip ContextMenuStrip327 {328 get329 {330 return base.ContextMenuStrip;331 }332 set333 {334 base.ContextMenuStrip = value;335 }336 }337 338 [Browsable(false)]339 [EditorBrowsable(EditorBrowsableState.Never)]340 protected override ImeMode DefaultImeMode341 {342 get343 {344 return System.Windows.Forms.ImeMode.Disable;345 }346 }347 348 [Browsable(false)]349 [EditorBrowsable(EditorBrowsableState.Never)]350 public new ImeMode ImeMode { get; set; }351 #endregion352 353 #region 重写属性354 355 protected override Size DefaultSize356 {357 get358 {359 return new Size(200, 23); ;360 }361 }362 363 public override string Text364 {365 get { return base.Text; }366 set367 {368 369 if (base.Text == value)370 return;371 372 base.Text = value;373 this.Invalidate();374 }375 }376 377 [DefaultValue(typeof(Color), "Transparent")]378 [Editor(typeof(ColorEditorExt), typeof(System.Drawing.Design.UITypeEditor))]379 public override Color BackColor380 {381 get { return base.BackColor; }382 set { base.BackColor = value; }383 }384 385 [DefaultValue(typeof(Color), "109, 109, 109")]386 [Editor(typeof(ColorEditorExt), typeof(System.Drawing.Design.UITypeEditor))]387 public override Color ForeColor388 {389 get { return base.ForeColor; }390 set { base.ForeColor = value; }391 }392 393 #endregion394 395 public HalvingLineExt()396 {397 SetStyle(ControlStyles.UserPaint, true);398 SetStyle(ControlStyles.AllPaintingInWmPaint, true);399 SetStyle(ControlStyles.OptimizedDoubleBuffer, true);400 SetStyle(ControlStyles.ResizeRedraw, true);401 SetStyle(ControlStyles.SupportsTransparentBackColor, true);402 403 this.BackColor = Color.Transparent;404 this.ForeColor = Color.FromArgb(109, 109, 109);405 }406 407 #region 重写408 409 protected override void OnPaint(PaintEventArgs e)410 {411 base.OnPaint(e);412 413 Graphics g = e.Graphics;414 415 int text_padding = 2;416 SizeF text_size = SizeF.Empty;417 RectangleF text_rect = RectangleF.Empty;418 419 Pen line_pen = new Pen(this.LineColor, this.LineThickness);420 int circular = this.lineCircular ? this.LineThickness : 0;421 PointF line_left_s = PointF.Empty;422 PointF line_left_e = PointF.Empty;423 PointF line_right_s = PointF.Empty;424 PointF line_right_e = PointF.Empty;425 426 #region 文字427 if (!String.IsNullOrEmpty(this.Text))428 {429 SolidBrush text_sb = new SolidBrush(this.ForeColor);430 StringFormat text_sf = null;431 432 #region433 if (this.LineOrientation == LineOrientations.Horizontal)434 {435 text_size = g.MeasureString(this.Text, this.Font, 1000, text_sf);436 #region437 float y = 0;438 if (this.textAlign == TextAligns.Top)439 {440 y = (this.ClientRectangle.Height - (this.lineThickness + text_size.Height)) / 2f;441 }442 else if (this.textAlign == TextAligns.Center)443 {444 y = (this.ClientRectangle.Height - text_size.Height) / 2f;445 }446 else if (this.textAlign == TextAligns.Bottom)447 {448 y = (this.ClientRectangle.Height - (this.lineThickness + text_padding + text_size.Height)) / 2f + this.lineThickness+ text_padding;449 }450 #endregion451 if (this.TextOrientation == TextOrientations.Left)452 {453 text_rect = new RectangleF(this.ClientRectangle.X + this.textDistance, y, text_size.Width, text_size.Height);454 }455 else456 {457 text_rect = new RectangleF(this.ClientRectangle.Right - this.textDistance - text_size.Width, y, text_size.Width, text_size.Height);458 }459 }460 #endregion461 #region462 else463 {464 text_sf = new StringFormat() { FormatFlags = StringFormatFlags.DirectionVertical };465 text_size = g.MeasureString(this.Text, this.Font, 1000, text_sf);466 #region467 float x = 0;468 if (this.textAlign == TextAligns.Top)469 {470 x = (this.ClientRectangle.Width - (this.lineThickness + text_size.Width)) / 2f;471 }472 else if (this.textAlign == TextAligns.Center)473 {474 x = (this.ClientRectangle.Width - text_size.Width) / 2f;475 }476 else if (this.textAlign == TextAligns.Bottom)477 {478 x = (this.ClientRectangle.Width - (this.lineThickness + text_padding + text_size.Width)) / 2f + this.lineThickness + text_padding;479 }480 #endregion481 if (this.TextOrientation == TextOrientations.Left)482 {483 text_rect = new RectangleF(x, this.ClientRectangle.Y + this.textDistance, text_size.Width, text_size.Height);484 }485 else486 {487 text_rect = new RectangleF(x, this.ClientRectangle.Bottom - this.textDistance - text_size.Height, text_size.Width, text_size.Height);488 }489 }490 #endregion491 492 g.DrawString(this.Text, this.Font, text_sb, text_rect, text_sf);493 text_sb.Dispose();494 if (text_sf != null)495 {496 text_sf.Dispose();497 }498 }499 #endregion500 501 #region 线条502 #region 两条线503 if (!String.IsNullOrEmpty(this.Text) && this.textAlign == TextAligns.Center)504 {505 if (this.LineOrientation == LineOrientations.Horizontal)506 {507 #region508 float y = 0;509 if (this.textAlign == TextAligns.Top)510 {511 y = text_rect.Bottom + this.LineThickness / 2f;512 }513 else if (this.textAlign == TextAligns.Center)514 {515 y = this.ClientRectangle.Height / 2f;516 }517 else if (this.textAlign == TextAligns.Bottom)518 {519 y = text_rect.Y - this.LineThickness / 2f;520 }521 #endregion522 line_left_s = new PointF(this.ClientRectangle.X + circular, y);523 line_left_e = new PointF(text_rect.X - text_padding, y);524 line_right_s = new PointF(text_rect.Right + text_padding, y);525 line_right_e = new PointF(this.ClientRectangle.Right - circular, y);526 }527 else528 {529 #region530 float x = 0;531 if (this.textAlign == TextAligns.Top)532 {533 x = text_rect.Right + this.LineThickness / 2f;534 }535 else if (this.textAlign == TextAligns.Center)536 {537 x = this.ClientRectangle.Width / 2f;538 }539 else if (this.textAlign == TextAligns.Bottom)540 {541 x = text_rect.X - this.LineThickness / 2f;542 }543 #endregion544 line_left_s = new PointF(x, this.ClientRectangle.Y + circular);545 line_left_e = new PointF(x, text_rect.Y - text_padding);546 line_right_s = new PointF(x, text_rect.Bottom + text_padding);547 line_right_e = new PointF(x, this.ClientRectangle.Bottom - circular);548 }549 if (this.lineCircular)550 {551 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;552 line_pen.StartCap = LineCap.Round;553 g.DrawLine(line_pen, line_left_s, line_left_e);554 line_pen.StartCap = LineCap.Flat;555 line_pen.EndCap = LineCap.Round;556 g.DrawLine(line_pen, line_right_s, line_right_e);557 }558 else559 {560 g.DrawLine(line_pen, line_left_s, line_left_e);561 g.DrawLine(line_pen, line_right_s, line_right_e);562 }563 }564 #endregion565 #region 一条线566 else567 {568 if (this.LineOrientation == LineOrientations.Horizontal)569 {570 #region571 float y = 0;572 if (this.textAlign == TextAligns.Top)573 {574 y = text_rect.Bottom + this.LineThickness / 2f;575 }576 else if (this.textAlign == TextAligns.Center)577 {578 y = this.ClientRectangle.Height / 2f;579 }580 else if (this.textAlign == TextAligns.Bottom)581 {582 y = text_rect.Y- text_padding - this.LineThickness / 2f;583 }584 #endregion585 line_left_s = new PointF(this.ClientRectangle.X + circular, y);586 line_left_e = new PointF(this.ClientRectangle.Right - circular, y);587 }588 else589 {590 #region591 float x = 0;592 if (this.textAlign == TextAligns.Top)593 {594 x = text_rect.Right + this.LineThickness / 2f;595 }596 else if (this.textAlign == TextAligns.Center)597 {598 x = this.ClientRectangle.Width / 2f;599 }600 else if (this.textAlign == TextAligns.Bottom)601 {602 x = text_rect.X - text_padding - this.LineThickness / 2f;603 }604 #endregion605 line_left_s = new PointF(x, this.ClientRectangle.Y + circular);606 line_left_e = new PointF(x, this.ClientRectangle.Bottom - circular);607 }608 if (this.lineCircular)609 {610 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;611 line_pen.StartCap = LineCap.Round;612 line_pen.EndCap = LineCap.Round;613 }614 g.DrawLine(line_pen, line_left_s, line_left_e);615 }616 #endregion617 618 line_pen.Dispose();619 #endregion620 621 }622 623 /// <summary> 624 /// 清理所有正在使用的资源。625 /// </summary>626 /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>627 protected override void Dispose(bool disposing)628 {629 if (disposing)630 {631 632 }633 base.Dispose(disposing);634 }635 636 #endregion637 638 #region 枚举639 640 /// <summary>641 /// 线条方向642 /// </summary>643 [Description("线条方向")]644 public enum LineOrientations645 {646 /// <summary>647 /// 水平648 /// </summary>649 Horizontal,650 /// <summary>651 /// 竖直652 /// </summary>653 Vertical654 }655 656 /// <summary>657 /// 文本方向658 /// </summary>659 [Description("文本方向")]660 public enum TextOrientations661 {662 /// <summary>663 /// 左边664 /// </summary>665 Left,666 /// <summary>667 /// 右边668 /// </summary>669 Right670 }671 672 /// <summary>673 /// 文本对齐方式674 /// </summary>675 [Description("文本对齐方式")]676 public enum TextAligns677 {678 /// <summary>679 /// 顶部680 /// </summary>681 Top,682 /// <summary>683 /// 居中684 /// </summary>685 Center,686 /// <summary>687 /// 底部688 /// </summary>689 Bottom690 }691 692 #endregion693 }
新增属性如下
源码下载:分割线控件.zip