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();
}
...全文
301 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
xwsn007 2009-07-23
  • 打赏
  • 举报
回复
继续关注,帮顶
古月无华 2009-07-23
  • 打赏
  • 举报
回复
问题解决了,
void AddText(szTextMsg) //内容不变
void AddTitle(LPCTSTR szText)//变化如下
{
int len;
CTime ctTime = CTime::GetCurrentTime();
//显示名子和时间
CString strTimeName;
strTimeName.Format(_T("%s %02d:%02d:%02d\n"),szText,ctTime.GetHour(),ctTime.GetMinute(),ctTime.GetSecond());

COLORREF crNewColor;
if(szText == _T("Server"))
crNewColor = RGB(0, 000, 255);
else
crNewColor = RGB(0, 128, 0);
ASSERT(szText);
ASSERT(AfxIsValidString(szText));


int iTotalTextLength = GetTextLengthEx(GTL_NUMCHARS,1200);
SetSel(iTotalTextLength, iTotalTextLength);
ReplaceSel(strTimeName);
int iStartPos = iTotalTextLength;

CHARFORMAT cf;
cf.cbSize = sizeof(CHARFORMAT);
cf.dwMask = CFM_COLOR | CFM_UNDERLINE | CFM_BOLD|CFM_FACE|CFM_SIZE;
cf.dwEffects =(unsigned long) ~( CFE_AUTOCOLOR | CFE_UNDERLINE | CFE_BOLD);
cf.crTextColor = crNewColor;//RGB(0, 0, 0);
cf.yHeight = 200;
_tcscpy(cf.szFaceName, _T("宋体")); //设置字体
int iEndPos = GetTextLengthEx(GTL_NUMCHARS,1200);
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);
}
SetSel(iStartPos, iEndPos);
SetSelectionCharFormat(cf);

//恢愎缩进为0
PARAFORMAT2 pf2;
ZeroMemory(&pf2, sizeof(PARAFORMAT2));
pf2.cbSize = sizeof(PARAFORMAT2);
pf2.dwMask = PFM_STARTINDENT;
pf2.dxStartIndent = 50;
SetParaFormat(pf2);

}
guyuewuhua 2009-07-23
  • 打赏
  • 举报
回复
顶一下,也遇到这个问题,有解决方法吗?
古月无华 2009-07-20
  • 打赏
  • 举报
回复
晕,还没有,按上面的方法,如果不输入汉字还好,当输入汉字时,问题照样
古月无华 2009-07-20
  • 打赏
  • 举报
回复
OK,问题解决了
CHARFORMAT cf;
......
cf.dwMask|= CFM_CHARSET;
cf.bCharSet = DEFAULT_CHARSET;
加上上面的,就没问题了,多谢楼上的各位,结贴了
古月无华 2009-07-20
  • 打赏
  • 举报
回复
现在做了测试了,只有这种字符有问题'の',其它系统字符都没问题的
  • 打赏
  • 举报
回复
古月无华 2009-07-19
  • 打赏
  • 举报
回复
晕,难到没有人遇到这种问题???
mynamelj 2009-07-18
  • 打赏
  • 举报
回复
关注,我也遇到这样的问题了
古月无华 2009-07-18
  • 打赏
  • 举报
回复
还有就是其它格式,像颜色和字号等都正常的,其它字体还好,就是这种'@'字体有问题
古月无华 2009-07-18
  • 打赏
  • 举报
回复
系统中设定的是UNICODE,1200也用过,还是老样子
jyh_baoding 2009-07-18
  • 打赏
  • 举报
回复
估计和字符设置有关系
skyxie 2009-07-18
  • 打赏
  • 举报
回复
GetTextLengthEx(GTL_NUMCHARS, 1200); //1200 for unicode
纠结的程序猿 2009-07-17
  • 打赏
  • 举报
回复
VC工程有没有指定 UNICODE?
zqlong_sunday 2009-07-17
  • 打赏
  • 举报
回复
换种字体试试

15,979

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧