C# winform自定义Label控件使其能设置行距

 

1)在windows窗体应用程序中添加组件类代码如下

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.ComponentModel;
namespace WindowsFormsApplication10
{
    public partial class LabelTx : System.Windows.Forms.Label
    {
        int lineDistance = 5;//行间距    
        Graphics gcs;
        int iHeight = 0, height = 200;
        string[] nrLine;
        string[] nrLinePos;
        int searchPos = 0;
        int section = 1;
        public int LineDistance
        {
            get { return lineDistance; }
            set
            {
                lineDistance = value;
                Changed(this.Font, this.Width, this.Text);
            }
        }
        public LabelTx()
            : base()
        {
            //this.TextChanged += new EventHandler(LabelTx_TextChanged);   
            this.SizeChanged += new EventHandler(LabelTx_SizeChanged);
            this.FontChanged += new EventHandler(LabelTx_FontChanged);
            //this.Font = new Font(this.Font.FontFamily, this.Font.Size, GraphicsUnit.Pixel);  
        }
        void LabelTx_FontChanged(object sender, EventArgs e)
        {
            Changed(this.Font, this.Width, this.Text);
        }
        void LabelTx_SizeChanged(object sender, EventArgs e)
        {
            Changed(this.Font, this.Width, this.Text);
        }
        public LabelTx(IContainer container)
        {
            container.Add(this);
            //base.Height   
            //InitializeComponent();   
        }
        public int FHeight
        {
            get { return this.Font.Height; }
        }
        protected int Height
        {
            get { return height; }
            set
            {
                height = value;
                base.Height = value;
            }
        }
        public override string Text
        {
            get
            {
                return base.Text;
            }
            set
            {
                //is.Font.Size.                       
                base.Text = value;
                Changed(this.Font, this.Width, value);
            }
        }
        protected void Changed(Font ft, int iWidth, string value)
        {
            iHeight = 0;
            if (value != "")
            {
                if (gcs == null)
                {
                    gcs = this.CreateGraphics();
                    SizeF sf0 = gcs.MeasureString(new string('测', 20), ft);
                    searchPos = (int)(iWidth * 20 / sf0.Width);
                }
                nrLine = value.Split(new string[1] { "/r/n" }, StringSplitOptions.RemoveEmptyEntries);
                section = nrLine.Length;
                nrLinePos = new string[section];
                SizeF sf1, sf2;
                string temps, tempt;
                string drawstring;
                int temPos, ipos;
                for (int i = 0; i < section; i++)
                {
                    ipos = 0;
                    temPos = searchPos;
                    if (searchPos >= nrLine[i].Length)
                    {
                        ipos += nrLine[i].Length;
                        nrLinePos[i] += "," + ipos.ToString();
                        iHeight++;
                        continue;
                    }
                    drawstring = nrLine[i];
                    nrLinePos[i] = "";
                    while (drawstring.Length > searchPos)
                    {
                        bool isfind = false;
                        for (int j = searchPos; j < drawstring.Length; j++)
                        {
                            temps = drawstring.Substring(0, j);
                            tempt = drawstring.Substring(0, j + 1);
                            sf1 = gcs.MeasureString(temps, ft);
                            sf2 = gcs.MeasureString(tempt, ft);
                            if (sf1.Width < iWidth && sf2.Width > iWidth)
                            {
                                iHeight++;
                                ipos += j;
                                nrLinePos[i] += "," + ipos.ToString();
                                isfind = true;
                                drawstring = drawstring.Substring(j);
                                break;
                            }
                        }
                        if (!isfind)
                        {
                            //drawstring = drawstring.Substring(searchPos);   
                            //iHeight++;   
                            break;
                        }
                    }
                    ipos += drawstring.Length;
                    nrLinePos[i] += "," + ipos.ToString();
                    iHeight++;
                    //tempLine = (int)(sf1.Width - 1) / this.Width + 1;                          
                    //iHeight += tempLine;   
                }
            }
            this.Height = iHeight * (ft.Height + lineDistance);
        }
        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            //base.OnPaint(e);              
            //if (isPaint) return;   
            //isPaint = true;   
            Graphics g = e.Graphics;
            String drawString = this.Text;
            Font drawFont = this.Font;
            SolidBrush drawBrush = new SolidBrush(this.ForeColor);
            SizeF textSize = g.MeasureString(this.Text, this.Font);//文本的矩形区域大小     
            int lineCount = Convert.ToInt16(textSize.Width / this.Width) + 1;//计算行数     
            int fHeight = this.Font.Height;
            int htHeight = 0;

            this.AutoSize = false;
            float x = 0.0F;
            float y = 0.0F;
            StringFormat drawFormat = new StringFormat();
            int step = 1;
            bool isFirst = true;
            SizeF sf1, sf2;
            string subN, subN1;
            lineCount = drawString.Length;//行数不超过总字符数目      
            int i, idx, first;
            string subStr, tmpStr = "", midStr = "";
            string[] idxs;
            for (i = 0; i < section; i++)
            {
                first = 0;
                subStr = nrLine[i];
                if (nrLinePos[i] != null) tmpStr = nrLinePos[i].TrimStart(',');
                midStr = subStr.Substring(first);
                if (tmpStr != "")
                {
                    idxs = tmpStr.Split(',');
                    for (int j = 0; j < idxs.Length; j++)
                    {
                        idx = int.Parse(idxs[j]);
                        midStr = subStr.Substring(first, idx - first);
                        e.Graphics.DrawString(midStr, drawFont, drawBrush, x, Convert.ToInt16(htHeight), drawFormat);
                        htHeight += (fHeight + lineDistance);
                        first = idx;
                    }
                    //midStr = subStr.Substring(first);   
                }
                //e.Graphics.DrawString(midStr, drawFont, drawBrush, x, Convert.ToInt16(htHeight), drawFormat);  
                //htHeight += ( lineDistance);//fHeight +   
            }
        }
    }
} 


 

2)在工具栏中将自定义的LabelTx控件添加到winform窗体。

3)设置LabelTx控件的属性Size但是高度设置无效。

4)在LabelTx控件的属性Text中输入文字并且设置LineDistance属性就OK了。

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
可以通过继承 Label 控件并重写其 OnPaint 方法来实现自定义 Label 控件,并在该控件中添加设置行距的功能。 以下是一个实现设置行距自定义 Label 控件的示例代码: ```csharp using System.Drawing; using System.Windows.Forms; public class CustomLabel : Label { private int lineSpacing = 0; public int LineSpacing { get { return lineSpacing; } set { lineSpacing = value; } } protected override void OnPaint(PaintEventArgs e) { // 获取控件的文本内容 string text = this.Text; // 如果文本内容为空,则不需要绘制 if (string.IsNullOrEmpty(text)) { base.OnPaint(e); return; } // 获取控件的字体 Font font = this.Font; // 获取控件的前景色 Color foreColor = this.ForeColor; // 获取控件的绘图区域 RectangleF rect = new RectangleF(0, 0, this.Width, this.Height); // 创建绘图笔刷 Brush brush = new SolidBrush(foreColor); // 创建字符串格式化对象 StringFormat format = new StringFormat(); // 设置字符串格式化对象的行距属性 format.LineSpacing = lineSpacing; // 绘制文本内容 e.Graphics.DrawString(text, font, brush, rect, format); // 销毁绘图笔刷 brush.Dispose(); } } ``` 在这个示例中,我们重写了 Label 控件的 OnPaint 方法,首先获取了控件的文本内容、字体和前景色等属性,并创建了一个绘图笔刷和一个字符串格式化对象。 然后,我们设置了字符串格式化对象的 LineSpacing 属性,这个属性就是我们自定义行距属性,它可以通过 LineSpacing 属性来设置。 最后,我们使用 Graphics 类的 DrawString 方法来绘制文本内容,其中传入了字符串格式化对象来实现行距设置。 使用这个自定义 Label 控件的方法与使用普通的 Label 控件相同,只需要将控件的类型改为 CustomLabel 即可,然后就可以通过 LineSpacing 属性来设置行距了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值