RicheditCtrl 控件在不同语言环境的操作系统上可能出现的乱码问题

由于工作上需要,把软件移植到英文版的os上(软件已是unicode版本的),发现RicheditCtrl
控件上显示的字体(本来内容应该显示为中文)乱码了。

发生乱码的具体代码:

mRichEditCtrl.SetSel(-1, -1);
mRichEditCtrl.ReplaceSel( (LPCTSTR)Message.GetBuffer());
Message.ReleaseBuffer();

查找原因:可能是控件不了解字符串已经是unicode代码,所以控件根据locale 的code page来硬性将代码转换为unicode显示,所以出现乱码。

解决方法:

mRichEditCtrl.SetSel(-1, -1);
#if UNICODE
	SETTEXTEX SetTxtEx = {0,};
	SetTxtEx.flags = ST_SELECTION;
	SetTxtEx.codepage = 1200;
	BOOL bRet = ::SendMessage(mRichEditCtrl.GetSafeHwnd(), EM_SETTEXTEX, (WPARAM)&SetTxtEx, (LPARAM)Message.GetBuffer());
	ASSERT(bRet);
#else
	mRichEditCtrl.ReplaceSel( (LPCTSTR)Message.GetBuffer());
#endif
Message.ReleaseBuffer();
The code page used to translate the text to Unicode. If codepage is 1200 (Unicode code page), no translation is done. If codepage is CP_ACP, the system code page is used.
参考 http://www.cppblog.com/jeffrey78/articles/2214.html

发布者