VOID PlayerController::SplitDrawText(HDC hDC,WCHAR* szString,WCHAR* szOutString,LPRECT lpRect)
{
DWORD nLen = wcslen(szString);
SIZE mySize;
GetTextExtentPoint(hDC,szString,nLen,&mySize);
int everySize = mySize.cx / nLen;
int nMaxLineChar = abs(lpRect->right - lpRect->left) / everySize - 1;
if(nMaxLineChar < 2) //应该至少能显示一个汉字
{
wcscpy(szOutString,szString);
return;
}
//记录当前行的宽度
short theLineLength = 0;
//记录当前行中汉字字节数,以防止将一半汉字分为两行
unsigned short halfChinese = 0;
int lastPosition = 0;
for(int i=0; i<= nLen-1; i++)
{
if(((unsigned char)szString[i] == 0x0d) && ((unsigned char)szString[i+1] == 0x0a))
theLineLength = 0;
// 在此加入"||"字符为换行标志字符,输入时可根据此字符串来自动换行
if(((unsigned char)szString[i] == '|') && ((unsigned char)szString[i+1] == '|'))
{
//szString.SetAt(i,(unsigned char)0x0d);
szString[i] = (unsigned char)0x0d;
szString[i+1] = (unsigned char)0x0a;
}
//大于0xa1的字节为汉字字节
if((unsigned char)szString[i] >= 0xA1)
halfChinese++;
theLineLength++;
//如果行宽大于每行最大宽度,进行特殊处理
if(theLineLength > nMaxLineChar)
{
//防止将一个汉字分为两行,回溯
if(!(halfChinese%2) && (unsigned char)szString[i] >= 0xA1)
{
//szString.Insert(i-1,(unsigned char)0x0a);
//szString.Insert(i-1,(unsigned char)0x0d);
wcscat(szOutString,L"/r/n");
wcsncpy(szOutString + i + 2,szString + i,i-lastPosition);
lastPosition = i;
}
else
{
//szString.Insert(i,(unsigned char)0x0a);
//szString.Insert(i,(unsigned char)0x0d);
wcscat(szOutString,L"/r/n");
wcsncpy(szOutString + lastPosition + 2,szString + lastPosition,i-lastPosition);
lastPosition = i;
i++; //跳过新增的换行符,应跳到<i+2>处(循环中加一次,故这里只加一次)
}
theLineLength = 0;
halfChinese =0;
}
// 结束了,最后一行
if(i == nLen-1)
{
wcscat(szOutString,L"/r/n");
wcscpy(szOutString + lastPosition + 2,szString + lastPosition);
}
}
}
{
DWORD nLen = wcslen(szString);
SIZE mySize;
GetTextExtentPoint(hDC,szString,nLen,&mySize);
int everySize = mySize.cx / nLen;
int nMaxLineChar = abs(lpRect->right - lpRect->left) / everySize - 1;
if(nMaxLineChar < 2) //应该至少能显示一个汉字
{
wcscpy(szOutString,szString);
return;
}
//记录当前行的宽度
short theLineLength = 0;
//记录当前行中汉字字节数,以防止将一半汉字分为两行
unsigned short halfChinese = 0;
int lastPosition = 0;
for(int i=0; i<= nLen-1; i++)
{
if(((unsigned char)szString[i] == 0x0d) && ((unsigned char)szString[i+1] == 0x0a))
theLineLength = 0;
// 在此加入"||"字符为换行标志字符,输入时可根据此字符串来自动换行
if(((unsigned char)szString[i] == '|') && ((unsigned char)szString[i+1] == '|'))
{
//szString.SetAt(i,(unsigned char)0x0d);
szString[i] = (unsigned char)0x0d;
szString[i+1] = (unsigned char)0x0a;
}
//大于0xa1的字节为汉字字节
if((unsigned char)szString[i] >= 0xA1)
halfChinese++;
theLineLength++;
//如果行宽大于每行最大宽度,进行特殊处理
if(theLineLength > nMaxLineChar)
{
//防止将一个汉字分为两行,回溯
if(!(halfChinese%2) && (unsigned char)szString[i] >= 0xA1)
{
//szString.Insert(i-1,(unsigned char)0x0a);
//szString.Insert(i-1,(unsigned char)0x0d);
wcscat(szOutString,L"/r/n");
wcsncpy(szOutString + i + 2,szString + i,i-lastPosition);
lastPosition = i;
}
else
{
//szString.Insert(i,(unsigned char)0x0a);
//szString.Insert(i,(unsigned char)0x0d);
wcscat(szOutString,L"/r/n");
wcsncpy(szOutString + lastPosition + 2,szString + lastPosition,i-lastPosition);
lastPosition = i;
i++; //跳过新增的换行符,应跳到<i+2>处(循环中加一次,故这里只加一次)
}
theLineLength = 0;
halfChinese =0;
}
// 结束了,最后一行
if(i == nLen-1)
{
wcscat(szOutString,L"/r/n");
wcscpy(szOutString + lastPosition + 2,szString + lastPosition);
}
}
}