C#中在panel上写文字并换行的方法

前段时间在优化项目时,突然想到在Panel上显示文字并换行的问题。当有一段文字需要显示并且最好是显示在Panel。于是我就动手写了下,做了一个Demo程序,现在将主要的代码贴出来,如果存在不足还希望能够帮忙提意见,以补充完善.

/// <summary>
/// 显示文本时需要用到的方法
/// </summary>
intlineDistance = 5; //行间距
Graphics gcs;
intiHeight = 0;
string[]nrLine;
string[]nrLinePos;
intsearchPos = 0;
intsection = 1;
intsectionHeight = 10;
DispMode dm = DispMode.None;
intiPanelNotPagerHeight= 0;


/// <summary>
/// 分析要显示文本的内容,将文本进行分段,分行,并测算好行距,段距等
/// </summary>
/// <paramname="pl"></param>
/// <paramname="ft"></param>
/// <paramname="iWidth"></param>
/// <paramname="value"></param>
private void GetTextInfo(Panel pl,Font ft, intiWidth, string value)
{
try
{
iHeight = 0;
if( value!= "")
{
if(gcs == null)
{
gcs = pl. CreateGraphics();
SizeF sf0 = gcs. MeasureString( new string( '测', 20),ft);
searchPos =( int)(iWidth* 20/sf0.Width);
if(searchPos> 2)searchPos-= 2;
}

nrLine = value. Split( new string[ 1]{ "\r\n"},StringSplitOptions.RemoveEmptyEntries); //记下每一段文本的信息
section = nrLine.Length;
nrLinePos = new string[section]; //存放每行分割的Index数字
SizeF sf1,sf2;
stringtemps,tempt;
stringdrawstring;
inttemPos; //临时Index
intipos; //文字Index
//将每一段文字的分成句子,并记下每句的起始Idex
for( inti = 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)
{
boolisfind = false;
for( intj = 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)
{
break;
}
}
ipos+= drawstring.Length;
nrLinePos[i]+= ","+ipos. ToString();
iHeight++;

}
}
if(dm == DispMode.None)
{
if( value== "")
{
iPanelNotPagerHeight = 0;
return;
}
else
{
iPanelNotPagerHeight = iHeight*(ft.Height+lineDistance)+(section- 1)*(sectionHeight-lineDistance);
}
}
}
catch(Exception e)
{
label1.Text=e.Message;
return;
}
}
/// <summary>
/// 根据GetTextInfo方法中测算好的信息来绘制文本,将文本显示到Panel上
/// </summary>
/// <paramname="pl"></param>
/// <paramname="text"></param>
/// <paramname="font"></param>
/// <paramname="solidbrushColor"></param>
/// <paramname="iWidth"></param>
private void PaintTextOnPanel(Panel pl, stringtext,Font font,Color solidbrushColor, intiWidth)
{
Graphics g = pl. CreateGraphics();
String drawString = text;
Font drawFont = font;
SolidBrush drawBrush = new SolidBrush(solidbrushColor);
SizeF textSize = g. MeasureString(text,font); //文本的矩形区域大小
intlineCount = Convert. ToInt16(textSize.Width/iWidth)+ 1; //计算行数
intfHeight = font.Height;
inthtHeight = 0;
boolisPageStart = false;
floatx = 0.0F;
StringFormat drawFormat = new StringFormat();
lineCount = drawString.Length; //行数不超过总字符数目
inti,idx,first;
stringsubStr,tmpStr = "",midStr = "";
string[]idxs;
inttmpPage = 1;
stringpreLineStr = "";
for(i = 0;i<section;i++)
{
if(i == 10)
{
first = 0;
}
first = 0;
subStr = nrLine[i];
if(nrLinePos[i]!= null)tmpStr = nrLinePos[i]. TrimStart( ',');
midStr = subStr. Substring(first);
if(tmpStr!= "")
{
idxs = tmpStr. Split( ',');
for( intj = 0;j<idxs.Length;j++)
{
idx = int. Parse(idxs[j]); //每句的结束Index
midStr = subStr. Substring(first,idx-first); //通过上句的结束Index和本句的结束Index计算本句的内容
if(dm == DispMode.None)
{

g. DrawString(midStr,drawFont,drawBrush,x,Convert. ToInt16(htHeight),drawFormat);
}

if(j == idxs.Length- 1)
{
htHeight+=(fHeight+sectionHeight);
}
else
{
htHeight+=(fHeight+lineDistance);
}
first = idx; //记下本句结束的Index
}
}
}
}





/// <summary>
/// 显示模式,分为全显示和分页显示两种
/// </summary>
public enumDispMode
{
None = 0,
Page = 1

}


调用显示Panel上文本的方法如下


private showTextOnPanel()

{

string txt="客服本来就是一个充满压力的工作,也许你以前没怎么挨过骂,起码挨骂还可以顶嘴,但是作为一个专业的客服专员,你就不允许在电话里表现出情绪波动,无论客户如何无理,如果语气不好,甚至爆粗,你的导师都会教你如何应付,但是前提是你一定不可以表现的不客气,否则全都是你错。这个就是客服,专业的客服!你会面对客户的质问(为什么这个你不懂?我要知道准确的解决时间),面对客户的质疑(你是不是新手啊?你确定这样做一定有效?),面对客户的威胁(再弄不好我要投诉你!你经理是谁?我要你经理接电话)。";

//指定字体

Font drawFont =newFont("Arial",14);

//调用方法显示
GetTextInfo(panel1,drawFont,400,txt);
PaintTextOnPanel(panel1,txt,drawFont,Color.Red,400);


}







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值