zoukankan      html  css  js  c++  java
  • C# winform自定义Label控件使其能设置行距

    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Text;  
    5. using System.Drawing;  
    6. using System.ComponentModel;  
    7. namespace WindowsFormsApplication10  
    8. {  
    9.     public partial class LabelTx : System.Windows.Forms.Label  
    10.     {  
    11.         int lineDistance = 5;//行间距      
    12.         Graphics gcs;  
    13.         int iHeight = 0, height = 200;  
    14.         string[] nrLine;  
    15.         string[] nrLinePos;  
    16.         int searchPos = 0;  
    17.         int section = 1;  
    18.         public int LineDistance  
    19.         {  
    20.             get { return lineDistance; }  
    21.             set  
    22.             {  
    23.                 lineDistance = value;  
    24.                 Changed(this.Font, this.Width, this.Text);  
    25.             }  
    26.         }  
    27.         public LabelTx()  
    28.             : base()  
    29.         {  
    30.             //this.TextChanged += new EventHandler(LabelTx_TextChanged);     
    31.             this.SizeChanged += new EventHandler(LabelTx_SizeChanged);  
    32.             this.FontChanged += new EventHandler(LabelTx_FontChanged);  
    33.             //this.Font = new Font(this.Font.FontFamily, this.Font.Size, GraphicsUnit.Pixel);    
    34.         }  
    35.         void LabelTx_FontChanged(object sender, EventArgs e)  
    36.         {  
    37.             Changed(this.Font, this.Width, this.Text);  
    38.         }  
    39.         void LabelTx_SizeChanged(object sender, EventArgs e)  
    40.         {  
    41.             Changed(this.Font, this.Width, this.Text);  
    42.         }  
    43.         public LabelTx(IContainer container)  
    44.         {  
    45.             container.Add(this);  
    46.             //base.Height     
    47.             //InitializeComponent();     
    48.         }  
    49.         public int FHeight  
    50.         {  
    51.             get { return this.Font.Height; }  
    52.         }  
    53.         protected int Height  
    54.         {  
    55.             get { return height; }  
    56.             set  
    57.             {  
    58.                 height = value;  
    59.                 base.Height = value;  
    60.             }  
    61.         }  
    62.         public override string Text  
    63.         {  
    64.             get  
    65.             {  
    66.                 return base.Text;  
    67.             }  
    68.             set  
    69.             {  
    70.                 //is.Font.Size.                         
    71.                 base.Text = value;  
    72.                 Changed(this.Font, this.Width, value);  
    73.             }  
    74.         }  
    75.         protected void Changed(Font ft, int iWidth, string value)  
    76.         {  
    77.             iHeight = 0;  
    78.             if (value != "")  
    79.             {  
    80.                 if (gcs == null)  
    81.                 {  
    82.                     gcs = this.CreateGraphics();  
    83.                     SizeF sf0 = gcs.MeasureString(new string('测', 20), ft);  
    84.                     searchPos = (int)(iWidth * 20 / sf0.Width);  
    85.                 }  
    86.                 nrLine = value.Split(new string[1] { "/r/n" }, StringSplitOptions.RemoveEmptyEntries);  
    87.                 section = nrLine.Length;  
    88.                 nrLinePos = new string[section];  
    89.                 SizeF sf1, sf2;  
    90.                 string temps, tempt;  
    91.                 string drawstring;  
    92.                 int temPos, ipos;  
    93.                 for (int i = 0; i < section; i++)  
    94.                 {  
    95.                     ipos = 0;  
    96.                     temPos = searchPos;  
    97.                     if (searchPos >= nrLine[i].Length)  
    98.                     {  
    99.                         ipos += nrLine[i].Length;  
    100.                         nrLinePos[i] += "," + ipos.ToString();  
    101.                         iHeight++;  
    102.                         continue;  
    103.                     }  
    104.                     drawstring = nrLine[i];  
    105.                     nrLinePos[i] = "";  
    106.                     while (drawstring.Length > searchPos)  
    107.                     {  
    108.                         bool isfind = false;  
    109.                         for (int j = searchPos; j < drawstring.Length; j++)  
    110.                         {  
    111.                             temps = drawstring.Substring(0, j);  
    112.                             tempt = drawstring.Substring(0, j + 1);  
    113.                             sf1 = gcs.MeasureString(temps, ft);  
    114.                             sf2 = gcs.MeasureString(tempt, ft);  
    115.                             if (sf1.Width < iWidth && sf2.Width > iWidth)  
    116.                             {  
    117.                                 iHeight++;  
    118.                                 ipos += j;  
    119.                                 nrLinePos[i] += "," + ipos.ToString();  
    120.                                 isfind = true;  
    121.                                 drawstring = drawstring.Substring(j);  
    122.                                 break;  
    123.                             }  
    124.                         }  
    125.                         if (!isfind)  
    126.                         {  
    127.                             //drawstring = drawstring.Substring(searchPos);     
    128.                             //iHeight++;     
    129.                             break;  
    130.                         }  
    131.                     }  
    132.                     ipos += drawstring.Length;  
    133.                     nrLinePos[i] += "," + ipos.ToString();  
    134.                     iHeight++;  
    135.                     //tempLine = (int)(sf1.Width - 1) / this.Width + 1;                            
    136.                     //iHeight += tempLine;     
    137.                 }  
    138.             }  
    139.             this.Height = iHeight * (ft.Height + lineDistance);  
    140.         }  
    141.         protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)  
    142.         {  
    143.             //base.OnPaint(e);                
    144.             //if (isPaint) return;     
    145.             //isPaint = true;     
    146.             Graphics g = e.Graphics;  
    147.             String drawString = this.Text;  
    148.             Font drawFont = this.Font;  
    149.             SolidBrush drawBrush = new SolidBrush(this.ForeColor);  
    150.             SizeF textSize = g.MeasureString(this.Text, this.Font);//文本的矩形区域大小       
    151.             int lineCount = Convert.ToInt16(textSize.Width / this.Width) + 1;//计算行数       
    152.             int fHeight = this.Font.Height;  
    153.             int htHeight = 0;  
    154.   
    155.             this.AutoSize = false;  
    156.             float x = 0.0F;  
    157.             float y = 0.0F;  
    158.             StringFormat drawFormat = new StringFormat();  
    159.             int step = 1;  
    160.             bool isFirst = true;  
    161.             SizeF sf1, sf2;  
    162.             string subN, subN1;  
    163.             lineCount = drawString.Length;//行数不超过总字符数目        
    164.             int i, idx, first;  
    165.             string subStr, tmpStr = "", midStr = "";  
    166.             string[] idxs;  
    167.             for (i = 0; i < section; i++)  
    168.             {  
    169.                 first = 0;  
    170.                 subStr = nrLine[i];  
    171.                 if (nrLinePos[i] != null) tmpStr = nrLinePos[i].TrimStart(',');  
    172.                 midStr = subStr.Substring(first);  
    173.                 if (tmpStr != "")  
    174.                 {  
    175.                     idxs = tmpStr.Split(',');  
    176.                     for (int j = 0; j < idxs.Length; j++)  
    177.                     {  
    178.                         idx = int.Parse(idxs[j]);  
    179.                         midStr = subStr.Substring(first, idx - first);  
    180.                         e.Graphics.DrawString(midStr, drawFont, drawBrush, x, Convert.ToInt16(htHeight), drawFormat);  
    181.                         htHeight += (fHeight + lineDistance);  
    182.                         first = idx;  
    183.                     }  
    184.                     //midStr = subStr.Substring(first);     
    185.                 }  
    186.                 //e.Graphics.DrawString(midStr, drawFont, drawBrush, x, Convert.ToInt16(htHeight), drawFormat);    
    187.                 //htHeight += ( lineDistance);//fHeight +     
    188.             }  
    189.         }  
    190.     }  
    191. }   
  • 相关阅读:
    Swift
    UIWindow 详解及使用场景
    点击状态栏回到顶部的功能失效的解决办法
    iOS
    从经典问题来看 Copy 方法
    从汇编层面深度剖析C++虚函数
    数值的整数次方
    求整数二进制中1的个数
    C++中的位运算总结
    嵌入在C++程序中的extern "C"
  • 原文地址:https://www.cnblogs.com/Alex80/p/4499021.html
Copyright © 2011-2022 走看看