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 ...
随机推荐
- socket通信_笔记
(socket通信) 客户端与服务器端通信问题: 我们首先要了解一个概念性的词汇:Socket socket的英文原义是“孔”或“插座”.作为进程通信机制,取后一种意思.通常也称作“套接字”,用于描述 ...
- 栈(顺序存储)C++模板实现
#include <iostream> using namespace std; template <typename T> class stack{ private: int ...
- 鸟哥笔记:linux系统日志文件介绍
简单的说日志文件就是记录系统活动信息的几个文件,例如:何时.何地(来源ip).何人(什么服务名称).做了什么操作.换句话说就是:记录系统在什么时候由哪个进程做了什么样的行为时,发生了什么事件等. 日志 ...
- web2py--------------用web2py写 django的例子 --------建立一个投票应用(1)
按照上一篇我们新建一个名为 polls 的app 然后文件结构如下 然后web2py 会自动向里边添加一些代码. 我们需要剔除一些,如这个 controllers ,defualt.py 的ind ...
- hdu5548
2015ACM/ICPC亚洲区上海站LCM WALK 题意:定义了一种走法,就是从当前的点为sx,sy,可以走到ex,ey;并且ex = sx + z,或者 ey = sy + z, 其中z为lcm( ...
- Java学习--final与static
final是java的关键字,它所表示的是“这部分是无法修改的”. 编译期常量,它在类加载的过程就已经完成了初始化,所以当类加载完成后是不可更改的,编译期可以将它代入到任何用到它的计算式中,也就是说可 ...
- DOS下文件操作命令
文件名是由文件路径和文件名称合起来的,如C:\DOS\COMMAND.COM. DIR 显示文件和文件夹(目录). 用法:DIR [文件名] [选项] 它有很多选项,如/A表示显示所有文件(即包括带隐 ...
- Elasticsearch强大的聚合功能Facet
在常规数据库中,我们都知道有一个sql就是group,分组.如果主表只有对应的一个列记录的分组的ID,那么还好统计,比如说每本书book表,有一个分类catId,记录是属于哪一类的书,那么直接按照ca ...
- 关于 Java 性能监控您不知道的 5 件事,第 1 部分
责怪糟糕的代码(或不良代码对象)并不能帮助您发现瓶颈,提高 Java? 应用程序速度,猜测也不能帮您解决.Ted Neward 引导您关注 Java 性能监控工具,从5 个技巧开始,使用Java 5 ...
- w3c subscribe
http://www.w3.org/html/ig/zh/ http://blog.csdn.net/spring21st/article/details/6126537 http://www.w3c ...