266 lines
8.7 KiB
C#
266 lines
8.7 KiB
C#
|
|
using System;
|
|
using System.ComponentModel;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.Text;
|
|
using System.Drawing;
|
|
|
|
namespace Tom.Component
|
|
{
|
|
/// <summary>
|
|
/// 颜色标签
|
|
/// </summary>
|
|
public partial class ColorLabel : System.Windows.Forms.Label
|
|
{
|
|
int lineDistance = 5;//行间距
|
|
Graphics gcs;
|
|
int iHeight = 0, height = 200;
|
|
string[] nrLine;
|
|
string[] nrLinePos;
|
|
int searchPos = 0;
|
|
int section = 1;
|
|
/// <summary>
|
|
/// 行间距
|
|
/// </summary>
|
|
[Description("行间距")]
|
|
public int LineDistance
|
|
{
|
|
get { return lineDistance; }
|
|
set
|
|
{
|
|
lineDistance = value;
|
|
Changed(this.Font, this.Width, this.Text);
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 颜色标签
|
|
/// </summary>
|
|
public ColorLabel()
|
|
: 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);
|
|
|
|
}
|
|
/// <summary>
|
|
/// 更新
|
|
/// </summary>
|
|
public void UpdateInfo()
|
|
{
|
|
Changed(this.Font, this.Width, this.Text);
|
|
}
|
|
/// <summary>
|
|
/// 字体高度
|
|
/// </summary>
|
|
public int FHeight
|
|
{
|
|
get { return this.Font.Height; }
|
|
}
|
|
/// <summary>
|
|
/// 控件高
|
|
/// </summary>
|
|
protected new int Height
|
|
{
|
|
get { return height; }
|
|
set
|
|
{
|
|
height = value;
|
|
base.Height = value;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 控件文本
|
|
/// </summary>
|
|
public override string Text
|
|
{
|
|
get
|
|
{
|
|
return base.Text;
|
|
}
|
|
set
|
|
{
|
|
//is.Font.Size.
|
|
base.Text = value;
|
|
Changed(this.Font, this.Width, value);
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 获取过滤掉颜色的字
|
|
/// </summary>
|
|
/// <param name="value"></param>
|
|
/// <returns></returns>
|
|
private string GetNoColorText(string value)
|
|
{
|
|
string t_str = "";
|
|
string[] text = value.Split('|');
|
|
for (int i = 0; i < text.Length; i++)
|
|
{
|
|
int iPos = text[i].IndexOf(":");
|
|
string mtext = text[i];
|
|
Color color = ForeColor;
|
|
if (iPos > 0)
|
|
{
|
|
string color_str = text[i].Substring(0, iPos);
|
|
try
|
|
{
|
|
color = ColorTranslator.FromHtml(color_str);
|
|
t_str += text[i].Substring(iPos + 1);
|
|
}
|
|
catch { }
|
|
}
|
|
}
|
|
return t_str;
|
|
}
|
|
private string[] nrColorLine;
|
|
/// <summary>
|
|
/// 改变字体
|
|
/// </summary>
|
|
/// <param name="ft"></param>
|
|
/// <param name="iWidth"></param>
|
|
/// <param name="value"></param>
|
|
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 = GetNoColorText(value).Replace("\r", "").Split("\n".ToCharArray());
|
|
nrColorLine=value.Replace("\r", "").Split("\n".ToCharArray());
|
|
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);
|
|
}
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="e"></param>
|
|
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.ToInt32(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;
|
|
Color lastColor = ForeColor;
|
|
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.ToInt32(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 +
|
|
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|