C# winform自定义Label控件使其能设置行距
- 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 +
- }
- }
- }
- }
C# winform自定义Label控件使其能设置行距的更多相关文章
- winform 可拖动的自定义Label控件
效果预览: 实现步骤如下: (1)首先在项目上右击选择:添加->新建项,添加自定义控件 (2)自定义的一个Label让它继承LabelControl控件,LabelControl控件是DevEx ...
- 自定义Label控件
最近开发过程中有一个需求就是修改label控件的模板,使其能够在鼠标移近的时候变成TextBox,从而方便输入,然后进行相应的修改,最终达到动态修改Label的目的,这里贴出相应的代码,并做简要的分析 ...
- Winform自定义键盘控件开发及使用
最近有学员提出项目中要使用键盘控件,系统自带的osk.exe不好用,于是就有了下面的内容: 首先是进行自定义键盘控件的开发,其实核心大家都知道,就是利用SendKeys.Send发送相应 的字符,但是 ...
- WinForm使用Label控件模拟分割线(竖向)
用Label控件进行模拟 宽度设为1:this.lblPagerSpliter1.Size = new System.Drawing.Size(1, 21); 去掉边框:this.lblPagerSp ...
- WinForm自定义验证控件
本文转载:http://blog.csdn.net/ziyouli/article/details/7583824 此篇博文不错:http://blog.csdn.net/sony0732/artic ...
- winform 自定义分页控件 及DataGridview数据绑定
分页效果如上图所示,用到的控件均为基本控件 ,其方法如下 右击项目-添加-新建项 选择用户控件 然后在用户控件中拖入所需要的Label,Button,Text 用户控件全部代码: using Syst ...
- Winform自定义分页控件的实现
实现效果 有点丑陋 但是功能是没问题的 测试过 实现思路 先创建一个用户控件 代码实现 public partial class PagerControl : UserControl { ; /// ...
- winform自定义分页控件
1.控件代码: public partial class PagerControl : UserControl { #region 构造函数 public PagerControl() { Initi ...
- winform自定义日期控件,要求可以手动输入日期DatePicker
要求:文本框中能手动输入数字,向上箭头根据鼠标位置给年月日递增,向下箭头递减 一:页面加载时: private void FlatDatePicker_Load(object sender, Even ...
随机推荐
- bzoj 1066: [SCOI2007] 蜥蜴
这道题还是挺好想的,但我一开始还是想错了…… 把每个石柱拆成两个点,一个入度,一个出度,两个点连一条容量为高度的边,这样就可以限制从此石柱上经过的蜥蜴的数量.关于蜥蜴是否单独成点,我是单独当成了一个点 ...
- Android_Json实例
概要: 最近由于自己的兴趣,想在Android开发一个自己的App,需要使用服务器,所以交换数据是逃不掉了的,但是学生党没有固定的服务器,因此使用的新浪的SAE,在学习的前期下可以尝试一下,挺不错的一 ...
- Linux与Windows的桥梁--共享目录
1.关闭防火墙和selinux # service iptables stop # chkconfig --level 2345 iptables off ...
- Linux DNS 设置失败
在执行 yum install gcc 时 发现下载失败 ping www.baidu.com ping 不通 ping 百度的IP:220.181.111.188却能ping 通 由此证明是DNS的 ...
- Sublime Text3注册码
这是一个注册码-– BEGIN LICENSE -– Michael Barnes Single User License EA7E-821385 8A353C41 872A0D5C DF9B2950 ...
- Python设计模式——单例模式
单例模式是日常应用中最广泛的模式了,其目的就是令到单个进程中只存在一个类的实例,从而可以实现数据的共享,节省系统开销,防止io阻塞等等 但是在多进程的应用中,单例模式就实现不了了,例如一些web应用, ...
- MapReduce多表连接
多表关联 多表关联和单表关联类似,它也是通过对原始数据进行一定的处理,从其中挖掘出关心的信息.下面进入这个实例. 1 实例描述 输入是两个文件,一个代表工厂表,包含工厂名列和地址编号列:另一个代表地址 ...
- CentOS PPTP配置LNMP+PPTP+FreeRADIUS+DaloRADIUS+流量控制
折腾了好几天,查阅了很多资料,终于搞定了,泪牛满面,下面记录详细操作过程!注:测试环境为CENTOS5.8 x86 安装PPTP 直接使用赵蓉的PPTP一键安装包即可 wget http://dl.z ...
- js高手
http://kb.cnblogs.com/page/173798/ http://kb.cnblogs.com/page/121539/ http://blog.jobbole.com/9648/ ...
- win7 64下暗黑世界V1.1 服务器端及客户端的安装及运行 成功
原地址:http://bbs.gameres.com/thread_223717.html 屌丝一枚,没有MAC 更没有XCODE 潜心整了星期六与星期天两天才安装运行成功...只能说安装太复杂了,, ...