前段時間在優化項目時,突然想到在Panel上顯示文字並換行的問題。當有一段文字需要顯示並且最好是顯示在Panel。於是我就動手寫了下,做了一個Demo程序,現在將主要的代碼貼出來,如果存在不足還希望能夠幫忙提意見,以補充完善.
/// <summary>
/// 顯示文本時需要用到的方法
/// </summary>
int lineDistance = 5;//行間距
Graphics gcs;
int iHeight = 0;
string[] nrLine;
string[] nrLinePos;
int searchPos = 0;
int section = 1;
int sectionHeight = 10;
DispMode dm = DispMode.None;
int iPanelNotPagerHeight=0;
/// <summary>
/// 分析要顯示文本的內容,將文本進行分段,分行,並測算好行距,段距等
/// </summary>
/// <param name="pl"></param>
/// <param name="ft"></param>
/// <param name="iWidth"></param>
/// <param name="value"></param>
private void GetTextInfo(Panel pl,Font ft, int iWidth, 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;
string temps, tempt;
string drawstring;
int temPos;//臨時Index
int ipos;//文字Index
//將每一段文字的分成句子,並記下每句的起始Idex
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)
{
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>
/// <param name="pl"></param>
/// <param name="text"></param>
/// <param name="font"></param>
/// <param name="solidbrushColor"></param>
/// <param name="iWidth"></param>
private void PaintTextOnPanel(Panel pl,string text,Font font,Color solidbrushColor,int iWidth)
{
Graphics g = pl.CreateGraphics();
String drawString = text;
Font drawFont = font;
SolidBrush drawBrush = new SolidBrush(solidbrushColor);
SizeF textSize = g.MeasureString(text, font);//文本的矩形區域大小
int lineCount = Convert.ToInt16(textSize.Width / iWidth) + 1;//計算行數
int fHeight = font.Height;
int htHeight = 0;
bool isPageStart = false;
float x = 0.0F;
StringFormat drawFormat = new StringFormat();
lineCount = drawString.Length;//行數不超過總字符數目
int i, idx, first;
string subStr, tmpStr = "", midStr = "";
string[] idxs;
int tmpPage = 1;
string preLineStr = "";
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 (int j = 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 enum DispMode
{
None = 0,
Page = 1
}
調用顯示Panel上文本的方法如下
private showTextOnPanel()
{
string txt="客服本來就是一個充滿壓力的工作,也許你以前沒怎麼挨過罵,起碼挨罵還可以頂嘴,但是作為一個專業的客服專員,你就不允許在電話裡表現出情緒波動,無論客戶如何無理,如果語氣不好,甚至爆粗,你的導師都會教你如何應付,但是前提是你一定不可以表現的不客氣,否則全都是你錯。這個就是客服,專業的客服!你會面對客戶的質問(為什麼這個你不懂?我要知道准確的解決時間),面對客戶的質疑(你是不是新手啊?你確定這樣做一定有效?),面對客戶的威脅(再弄不好我要投訴你!你經理是誰?我要你經理接電話)。";
//指定字體
Font drawFont = new Font("Arial", 14);
//調用方法顯示
GetTextInfo(panel1,drawFont,400,txt);
PaintTextOnPanel(panel1,txt,drawFont,Color.Red,400);
}