码迷,mamicode.com
首页 > Windows程序 > 详细

分割线控件----------WinForm控件开发系列

时间:2020-09-07 18:57:55      阅读:61      评论:0      收藏:0      [点我收藏+]

标签:tin   ide   sed   amp   property   hand   net   graphic   center   

技术图片

该控件是继承于 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         #endregion
117 
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 LineOrientation
129         {
130             get { return this.lineOrientation; }
131             set
132             {
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 LineThickness
152         {
153             get { return this.lineThickness; }
154             set
155             {
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 LineCircular
171         {
172             get { return this.lineCircular; }
173             set
174             {
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 LineColor
191         {
192             get { return this.lineColor; }
193             set
194             {
195                 if (this.lineColor == value)
196                     return;
197                 this.lineColor = value;
198                 this.Invalidate();
199             }
200         }
201 
202         #endregion
203 
204         #region 文本
205 
206         private TextOrientations textOrientation = TextOrientations.Left;
207         /// <summary>
208         ///文本方向
209         /// </summary>
210         [DefaultValue(TextOrientations.Left)]
211         [Description("文本方向")]
212         public TextOrientations TextOrientation
213         {
214             get { return this.textOrientation; }
215             set
216             {
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 TextAlign
232         {
233             get { return this.textAlign; }
234             set
235             {
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 TextDistance
251         {
252             get { return this.textDistance; }
253             set
254             {
255                 if (this.textDistance == value || value < 0)
256                     return;
257                 this.textDistance = value;
258                 this.Invalidate();
259             }
260         }
261 
262         #endregion
263 
264         #endregion
265 
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 TabIndex
279         {
280             get { return 0; }
281             set { base.TabIndex = 0; }
282         }
283 
284         [Browsable(false)]
285         [EditorBrowsable(EditorBrowsableState.Never)]
286         public new bool TabStop
287         {
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 AllowDrop
299         {
300             get
301             {
302                 return base.AllowDrop;
303             }
304             set
305             {
306                 base.AllowDrop = value;
307             }
308         }
309 
310         [Browsable(false)]
311         [EditorBrowsable(EditorBrowsableState.Never)]
312         public override RightToLeft RightToLeft
313         {
314             get
315             {
316                 return base.RightToLeft;
317             }
318             set
319             {
320                 base.RightToLeft = value;
321             }
322         }
323 
324         [Browsable(false)]
325         [EditorBrowsable(EditorBrowsableState.Never)]
326         public override ContextMenuStrip ContextMenuStrip
327         {
328             get
329             {
330                 return base.ContextMenuStrip;
331             }
332             set
333             {
334                 base.ContextMenuStrip = value;
335             }
336         }
337 
338         [Browsable(false)]
339         [EditorBrowsable(EditorBrowsableState.Never)]
340         protected override ImeMode DefaultImeMode
341         {
342             get
343             {
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         #endregion
352 
353         #region 重写属性
354 
355         protected override Size DefaultSize
356         {
357             get
358             {
359                 return new Size(200, 23); ;
360             }
361         }
362 
363         public override string Text
364         {
365             get { return base.Text; }
366             set
367             {
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 BackColor
380         {
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 ForeColor
388         {
389             get { return base.ForeColor; }
390             set { base.ForeColor = value; }
391         }
392 
393         #endregion
394 
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                 #region
433                 if (this.LineOrientation == LineOrientations.Horizontal)
434                 {
435                     text_size = g.MeasureString(this.Text, this.Font, 1000, text_sf);
436                     #region
437                     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                     #endregion
451                     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                     else
456                     {
457                         text_rect = new RectangleF(this.ClientRectangle.Right - this.textDistance - text_size.Width, y, text_size.Width, text_size.Height);
458                     }
459                 }
460                 #endregion
461                 #region
462                 else
463                 {
464                     text_sf = new StringFormat() { FormatFlags = StringFormatFlags.DirectionVertical };
465                     text_size = g.MeasureString(this.Text, this.Font, 1000, text_sf);
466                     #region
467                     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                     #endregion
481                     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                     else
486                     {
487                         text_rect = new RectangleF(x, this.ClientRectangle.Bottom - this.textDistance - text_size.Height, text_size.Width, text_size.Height);
488                     }
489                 }
490                 #endregion
491 
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             #endregion
500 
501             #region 线条
502             #region 两条线
503             if (!String.IsNullOrEmpty(this.Text) && this.textAlign == TextAligns.Center)
504             {
505                 if (this.LineOrientation == LineOrientations.Horizontal)
506                 {
507                     #region
508                     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                     #endregion
522                     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                 else
528                 {
529                     #region
530                     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                     #endregion
544                     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                 else
559                 {
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             #endregion
565             #region 一条线
566             else
567             {
568                 if (this.LineOrientation == LineOrientations.Horizontal)
569                 {
570                     #region
571                     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                     #endregion
585                     line_left_s = new PointF(this.ClientRectangle.X + circular, y);
586                     line_left_e = new PointF(this.ClientRectangle.Right - circular, y);
587                 }
588                 else
589                 {
590                     #region
591                     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                     #endregion
605                     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             #endregion
617 
618             line_pen.Dispose();
619             #endregion
620 
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         #endregion
637 
638         #region 枚举
639 
640         /// <summary>
641         /// 线条方向
642         /// </summary>
643         [Description("线条方向")]
644         public enum LineOrientations
645         {
646             /// <summary>
647             /// 水平
648             /// </summary>
649             Horizontal,
650             /// <summary>
651             /// 竖直
652             /// </summary>
653             Vertical
654         }
655 
656         /// <summary>
657         /// 文本方向
658         /// </summary>
659         [Description("文本方向")]
660         public enum TextOrientations
661         {
662             /// <summary>
663             /// 左边
664             /// </summary>
665             Left,
666             /// <summary>
667             /// 右边
668             /// </summary>
669             Right
670         }
671 
672         /// <summary>
673         /// 文本对齐方式
674         /// </summary>
675         [Description("文本对齐方式")]
676         public enum TextAligns
677         {
678             /// <summary>
679             /// 顶部
680             /// </summary>
681             Top,
682             /// <summary>
683             /// 居中
684             /// </summary>
685             Center,
686             /// <summary>
687             /// 底部
688             /// </summary>
689             Bottom
690         }
691 
692         #endregion
693     }

 

 新增属性如下

技术图片

源码下载:分割线控件.zip

分割线控件----------WinForm控件开发系列

标签:tin   ide   sed   amp   property   hand   net   graphic   center   

原文地址:https://www.cnblogs.com/tlmbem/p/13573872.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!