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. this.AutoSize = false;
  155. float x = 0.0F;
  156. float y = 0.0F;
  157. StringFormat drawFormat = new StringFormat();
  158. int step = 1;
  159. bool isFirst = true;
  160. SizeF sf1, sf2;
  161. string subN, subN1;
  162. lineCount = drawString.Length;//行数不超过总字符数目
  163. int i, idx, first;
  164. string subStr, tmpStr = "", midStr = "";
  165. string[] idxs;
  166. for (i = 0; i < section; i++)
  167. {
  168. first = 0;
  169. subStr = nrLine[i];
  170. if (nrLinePos[i] != null) tmpStr = nrLinePos[i].TrimStart(',');
  171. midStr = subStr.Substring(first);
  172. if (tmpStr != "")
  173. {
  174. idxs = tmpStr.Split(',');
  175. for (int j = 0; j < idxs.Length; j++)
  176. {
  177. idx = int.Parse(idxs[j]);
  178. midStr = subStr.Substring(first, idx - first);
  179. e.Graphics.DrawString(midStr, drawFont, drawBrush, x, Convert.ToInt16(htHeight), drawFormat);
  180. htHeight += (fHeight + lineDistance);
  181. first = idx;
  182. }
  183. //midStr = subStr.Substring(first);
  184. }
  185. //e.Graphics.DrawString(midStr, drawFont, drawBrush, x, Convert.ToInt16(htHeight), drawFormat);
  186. //htHeight += ( lineDistance);//fHeight +
  187. }
  188. }
  189. }
  190. }

C# winform自定义Label控件使其能设置行距的更多相关文章

  1. winform 可拖动的自定义Label控件

    效果预览: 实现步骤如下: (1)首先在项目上右击选择:添加->新建项,添加自定义控件 (2)自定义的一个Label让它继承LabelControl控件,LabelControl控件是DevEx ...

  2. 自定义Label控件

    最近开发过程中有一个需求就是修改label控件的模板,使其能够在鼠标移近的时候变成TextBox,从而方便输入,然后进行相应的修改,最终达到动态修改Label的目的,这里贴出相应的代码,并做简要的分析 ...

  3. Winform自定义键盘控件开发及使用

    最近有学员提出项目中要使用键盘控件,系统自带的osk.exe不好用,于是就有了下面的内容: 首先是进行自定义键盘控件的开发,其实核心大家都知道,就是利用SendKeys.Send发送相应 的字符,但是 ...

  4. WinForm使用Label控件模拟分割线(竖向)

    用Label控件进行模拟 宽度设为1:this.lblPagerSpliter1.Size = new System.Drawing.Size(1, 21); 去掉边框:this.lblPagerSp ...

  5. WinForm自定义验证控件

    本文转载:http://blog.csdn.net/ziyouli/article/details/7583824 此篇博文不错:http://blog.csdn.net/sony0732/artic ...

  6. winform 自定义分页控件 及DataGridview数据绑定

    分页效果如上图所示,用到的控件均为基本控件 ,其方法如下 右击项目-添加-新建项 选择用户控件 然后在用户控件中拖入所需要的Label,Button,Text 用户控件全部代码: using Syst ...

  7. Winform自定义分页控件的实现

    实现效果 有点丑陋 但是功能是没问题的 测试过 实现思路 先创建一个用户控件 代码实现 public partial class PagerControl : UserControl { ; /// ...

  8. winform自定义分页控件

    1.控件代码: public partial class PagerControl : UserControl { #region 构造函数 public PagerControl() { Initi ...

  9. winform自定义日期控件,要求可以手动输入日期DatePicker

    要求:文本框中能手动输入数字,向上箭头根据鼠标位置给年月日递增,向下箭头递减 一:页面加载时: private void FlatDatePicker_Load(object sender, Even ...

随机推荐

  1. mysql时间与日期函数

    返回日期相关的 Now() || CURRENT_TIMESTAMP();返回当前时间 to_days(date) 返回日期date是西元0年至今多少天(不计算1582年以前) 转换为天数 date是 ...

  2. 4.MySQL连接并选择数据库(SQL & C)

    在连接了MySQL数据库之后,可以通过SQL命令或者C.PHP.JAVA等程序来指定需要操作的数据库.这里主要介绍SQL命令和相应的C程序. 首先创建用户rick(赋予所有权限) mysql> ...

  3. MySQL二进制文件规范安装

    演示环境介绍 操作系统:CentOS 6.7  (64位) 二进制包:mysql-5.6.30-linux-glibc2.5-x86_64.tar.gz MySQL 下载地址:http://dev.m ...

  4. Apache虚拟主机设置

    Apache支持两种虚拟主机,一种是基于IP地址的,一种是基于域名的. 基于IP地址的虚拟机现在使用的很少,它需要一台服务器需要拥有多个IP地址.基于域名的虚拟主机要求服务器有一个IP地址就可以了,只 ...

  5. 计算连续的IP地址

    题目:要求计算连续的IP地址. 举例:起始IP为192.168.2.2,IP总个数为3,那么要求得的所有IP的为192.168.2.2,192.168.2.3,192.168.2.4.再举个例子,起始 ...

  6. MYSQL Error 2006HY000:MySQL server has gone away的解决方案

    MySQL server has gone away有几种情况. 1.应用程序(比如PHP)长时间的执行批量的MYSQL语句. 最常见的就是采集或者新旧数据转化. 解决方案: 在my.cnf文件中添加 ...

  7. Oracle 分析函数之聚集函数(MAX、MIN、AVG和SUM)

    MAX 查找组中表达式的最大值 MAX(COL ) OVER ( [ <partition_by_clause> ] < order_by_clause > )MIN 查找组中 ...

  8. 浏览我的php网页时,出现的都是网页的代码

    添加php模块 ,在apache/conf/httpd.conf,如果是windows下的话,添加如下代码,具体路径你根据具体情况设置#BEGIN PHP INSTALLER EDITS - REMO ...

  9. UPUPW PHP环境集成包

    UPUPW PHP环境集成包 http://www.upupw.net/

  10. HDU 1025 Constructing Roads In JGShining's Kingdom(DP+二分)

    点我看题目 题意 :两条平行线上分别有两种城市的生存,一条线上是贫穷城市,他们每一座城市都刚好只缺乏一种物资,而另一条线上是富有城市,他们每一座城市刚好只富有一种物资,所以要从富有城市出口到贫穷城市, ...