孤独的猫

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

带下划线的LABEL控件

继承自Label,代码如下:

  1 using System.Drawing.Drawing2D;
  2 using System.Windows.Forms;
  3 using System.Collections.Generic;
  4 using System.ComponentModel;
  5 using System.Linq;
  6 using System.Text;
  7 
  8 namespace CustomControlSample
  9 {
 10     public class LineLabel:System.Windows.Forms.Label
 11     {
 12         private Color _lineColor;
 13         private float _lineThick;
 14         private float[] _DashPattern;
 15         private DashStyle _DashStyle;
 16 
 17         public LineLabel()
 18         {
 19             _lineColor = base.ForeColor;
 20             _lineThick = 1f;
 21             _DashStyle = DashStyle.Solid;
 22         }
 23 
 24         [Browsable(true)]
 25         public Color LineColor
 26         {
 27             get
 28             {
 29                 return _lineColor;
 30             }
 31             set
 32             {
 33                 _lineColor = value;
 34                 Invalidate();
 35             }
 36         }
 37 
 38         [Browsable(true)]
 39         public float LineThick
 40         {
 41             get
 42             {
 43                 return _lineThick;
 44             }
 45             set
 46             {
 47                 _lineThick = value;
 48                 Invalidate();
 49             }
 50         }
 51 
 52         [Browsable(true)]
 53         public float LinePenMode
 54         {
 55             get
 56             {
 57                 return _lineThick;
 58             }
 59             set
 60             {
 61                 _lineThick = value;
 62                 Invalidate();
 63             }
 64         }
 65 
 66         [Browsable(true)]
 67         public float[] LineDashPattern
 68         {
 69             get
 70             {
 71                 return _DashPattern;
 72             }
 73             set
 74             {
 75                 _DashPattern = value;
 76                 Invalidate();
 77             }
 78         }
 79 
 80         [Browsable(true)]
 81         public DashStyle LineDashStyle
 82         {
 83             get
 84             {
 85                 return _DashStyle;
 86             }
 87             set
 88             {
 89                 _DashStyle = value;
 90                 Invalidate();
 91             }
 92         }
 93 
 94         protected override void OnPaint(PaintEventArgs e)
 95         {
 96             base.OnPaint(e);
 97             //StringFormat style = new StringFormat();
 98             //e.Graphics.DrawString(
 99             //    base.Text,
100             //    Font,
101             //    new SolidBrush(base.ForeColor),
102             //    ClientRectangle, style);
103 
104             Pen p = new Pen(LineColor, LineThick);
105             p.DashStyle = LineDashStyle;
106             if ((p.DashStyle == DashStyle.DashDot) || (p.DashStyle == DashStyle.DashDotDot))
107                 p.DashPattern = LineDashPattern;
108             e.Graphics.DrawLine(p,2,this.Height-1, this.Width,this.Height-1);
109         }

 

posted on 2012-12-30 12:09  孤独的猫  阅读(2335)  评论(0编辑  收藏  举报