C#自定义可以调行距的Lable

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace jmyd.extend
{
    public partial class myLabel : System.Windows.Forms.Label
    {
        int lineDistance = 1;//行间距   

        public int LineDistance
        {
            get { return lineDistance; }
            set { lineDistance = value; }
        }
        public myLabel()
        {
            //InitializeComponent();
        }

        public myLabel(IContainer container)
        {
            container.Add(this);
            //InitializeComponent();
        }
        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            try
            {
                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;//计算行数   

                this.Height = Convert.ToInt16((textSize.Height + lineDistance) * lineCount);//计算调整后的高度   
                this.AutoSize = false;
                float x = 0.0F;
                StringFormat drawFormat = new StringFormat();
                int step = 1;
                lineCount = drawString.Length;//行数不超过总字符数目   
                for (int i = 0; i < lineCount; i++)
                {
                    //计算每行容纳的字符数目   
                    int charCount;
                    for (charCount = 0; charCount < drawString.Length; charCount++)
                    {
                        string subN = drawString.Substring(0, charCount);
                        string subN1 = drawString.Substring(0, charCount + 1);
                        if (g.MeasureString(subN, this.Font).Width <= this.Width
                                && g.MeasureString(subN1, this.Font).Width > this.Width)
                        {
                            step = charCount;
                            break;
                        }
                    }
                    string subStr;
                    if (charCount == drawString.Length)//最后一行文本   
                    {
                        subStr = drawString;
                        e.Graphics.DrawString(subStr, drawFont, drawBrush, x, Convert.ToInt16(textSize.Height * i) + i * LineDistance, drawFormat);
                        break;
                    }
                    else
                    {
                        subStr = drawString.Substring(0, step);//当前行文本   
                        drawString = drawString.Substring(step);//剩余文本   
                        e.Graphics.DrawString(subStr, drawFont, drawBrush, x, Convert.ToInt16(textSize.Height * i) + i * LineDistance, drawFormat);
                    }
                }
            }
            catch
            { }
        }
    }
}

 

缺点:由于使用了OnPaint事件,会不断的调用,性能减低了。
转:http://gyy627.blog.163.com/blog/static/4293327520103140139684/ 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值