RichEdit 在vs 2005遇到特殊字符显示不正常
古月无华 2009-07-17 04:34:12 问题如下:
两个函数 AddTitle();//在控件上输出名子和时间 和QQ一样的
AddText();//在控件上输出聊天内容
由于输出名子和时间格式是固定的,不需要改变,而输出内容,是要根据用户自己设置的格式随时改变的,当用户设置字体时就像"@宋体"时,输出聊天内容是按照这个格式,但是名子就有问题,如果名子里有特殊字符像"古月の无华",'の'前面的"古月"显示正常,后面的"无华"则根据显示为"@宋体"格式.用尽各个方法,百思不得其解!
这两个方法如下:
void AddTitle(CString szText)
{
int len;
CTime ctTime = CTime::GetCurrentTime();
//显示名子和时间
CString strTimeName;
strTimeName.Format(L"%s %02d:%02d:%02d\n",szText,ctTime.GetHour(),ctTime.GetMinute(),ctTime.GetSecond());
COLORREF crNewColor;
if(szText == L"Server")
crNewColor = RGB(0, 000, 255);
else
crNewColor = RGB(0, 128, 0);
ASSERT(szText);
ASSERT(AfxIsValidString(szText));
int iTotalTextLength = GetTextLengthEx(GTL_NUMCHARS);
CWnd *focusWnd = GetFocus();
// Hide any selection and select the end of text marker.
HideSelection(TRUE, TRUE);
SetSel(iTotalTextLength, iTotalTextLength);
CHARFORMAT cf;
ZeroMemory(&cf, sizeof(CHARFORMAT));
cf.cbSize = sizeof(CHARFORMAT);
cf.crTextColor = crNewColor;
cf.dwMask = CFM_COLOR|CFM_SIZE|CFM_FACE;
cf.dwEffects = (unsigned long)~(CFE_AUTOCOLOR | CFE_UNDERLINE | CFE_BOLD);
_tcscpy(cf.szFaceName, _T("宋体")); //设置字体
cf.yHeight = 200;
// Now set the character format
SetSelectionCharFormat(cf);
//恢愎缩进为0
PARAFORMAT2 pf2;
ZeroMemory(&pf2, sizeof(PARAFORMAT2));
pf2.cbSize = sizeof(PARAFORMAT2);
pf2.dwMask = PFM_STARTINDENT;
pf2.dxStartIndent = 50;
SetParaFormat(pf2);
// And put the text into the selection
ReplaceSel(strTimeName);
len = GetTextLengthEx(GTL_NUMCHARS);
// Now select the end of text marker again
SetSel(len, len);
if (iTotalTextLength > 125000)
{
// The control's starting to get full so trim off the first
// 50,000 bytes....
SetSel(0, 50000);
ReplaceSel(_T(""));
SetSel(iTotalTextLength, iTotalTextLength);
}
HideSelection(FALSE, TRUE);
SendMessage(EM_SCROLLCARET, 0, 0);
if (focusWnd != (CWnd *) NULL)
focusWnd->SetFocus();
}
void AddText(szTextMsg)
{
int len;
ASSERT(szTextMsg);
ASSERT(AfxIsValidString(szTextMsg));
int iTotalTextLength = GetTextLengthEx(GTL_NUMCHARS);
CWnd *focusWnd = GetFocus();
// Hide any selection and select the end of text marker.
HideSelection(TRUE, TRUE);
SetSel(iTotalTextLength, iTotalTextLength);
szTextMsg += L"\n";
CHARFORMAT cf;
ZeroMemory(&cf, sizeof(CHARFORMAT));
cf.cbSize = sizeof (CHARFORMAT);
cf.dwMask = CFM_FACE | CFM_SIZE | CFM_BOLD |
CFM_ITALIC | CFM_UNDERLINE | CFM_STRIKEOUT | CFM_PROTECTED | CFM_COLOR ;
cf.dwEffects = 0;//(unsigned long)~(CFE_AUTOCOLOR | CFE_UNDERLINE | CFE_BOLD | CFE_ITALIC | CFE_STRIKEOUT);
cf.crTextColor = RGB(0,0,255);
cf.dwEffects = (cf.dwEffects | CFE_BOLD) ;
cf.dwEffects = (cf.dwEffects& ~CFE_ITALIC);
cf.dwEffects = (cf.dwEffects& ~CFE_UNDERLINE);
cf.dwEffects = (cf.dwEffects& ~CFE_STRIKEOUT);
cf.dwMask |= CFM_SIZE;
cf.yHeight = 12 * 20;
::lstrcpy(cf.szFaceName, L"宋体");
// Now set the character format
SetSelectionCharFormat(cf);
//恢愎缩进为0
PARAFORMAT2 pf2;
ZeroMemory(&pf2, sizeof(PARAFORMAT2));
pf2.cbSize = sizeof(PARAFORMAT2);
pf2.dwMask = PFM_STARTINDENT;
pf2.dxStartIndent = 200;
SetParaFormat(pf2);
// And put the text into the selection
ReplaceSel(szTextMsg);
len = GetTextLengthEx(GTL_NUMCHARS);
// Now select the end of text marker again
SetSel(len, len);
if (iTotalTextLength > 125000)
{
// The control's starting to get full so trim off the first
// 50,000 bytes....
SetSel(0, 50000);
ReplaceSel(_T(""));
SetSel(iTotalTextLength, iTotalTextLength);
}
HideSelection(FALSE, TRUE);
SendMessage(EM_SCROLLCARET, 0, 0);
if (focusWnd != (CWnd *) NULL)
focusWnd->SetFocus();
}