C#カスタム行送り可能なケーブル


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/