首页
Web开发
Windows程序
编程语言
数据库
移动开发
系统相关
微信
其他好文
会员
首页
>
Windows程序
> 详细
C# winform自定义Label控件使其能设置行距
时间:
2015-05-13 00:47:56
阅读:
432
评论:
0
收藏:
0
[点我收藏+]
标签:
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控件使其能设置行距
标签:
原文地址:http://www.cnblogs.com/Alex80/p/4499021.html
踩
(
0
)
赞
(
0
)
举报
评论
一句话评论(
0
)
登录后才能评论!
分享档案
更多>
2021年07月29日 (22)
2021年07月28日 (40)
2021年07月27日 (32)
2021年07月26日 (79)
2021年07月23日 (29)
2021年07月22日 (30)
2021年07月21日 (42)
2021年07月20日 (16)
2021年07月19日 (90)
2021年07月16日 (35)
周排行
更多
动态 WebApi 引擎使用教程(3行代码完成动态 WebApi 构建)
2021-07-28
windows 查看文件的md5/sha1/sha256
2021-07-28
git windows下换行符问题 LF与CRLF转换
2021-07-27
FileExistsError: [WinError 183] 当文件已存在时,无法创建该文件。
2021-07-26
K8S--可视化界面Kubernetes Dashboard(API Server方式)
2021-07-26
Redis安装成windows服务
2021-07-26
c#32位支持大内存(>2gb)
2021-07-23
【c#】Dev BarStaticItem问题汇总
2021-07-23
Exception: URL fetch failure on https://s3.amazonaws.com/text-datasets/nietzsche.txt: None -- [WinError 10054] 远程主机强迫关闭了一个现有的连接。
2021-07-22
WinForm使用DataGridView实现类似Excel表格的查找替换
2021-07-22
友情链接
兰亭集智
国之画
百度统计
站长统计
阿里云
chrome插件
新版天听网
关于我们
-
联系我们
-
留言反馈
© 2014
mamicode.com
版权所有 联系我们:gaon5@hotmail.com
迷上了代码!