^_^

2010年3月15日星期一

如何向 CRichEditCtrl 添加自定义字体的字符



多么简单的问题啊,折腾我够呛
strName += TEXT("\r\n");

CHARFORMAT cf;
cf.cbSize = sizeof (CHARFORMAT);  
cf.dwMask = CFM_COLOR | CFM_UNDERLINE | CFM_BOLD;
cf.dwEffects = (unsigned long)~(CFE_AUTOCOLOR | CFE_UNDERLINE | CFE_BOLD);
if(is_self)
    cf.crTextColor = RGB(0x91, 0xe0, 0x2a);
else
    cf.crTextColor = RGB(0x59, 0xb5, 0xff);

long start = GetTextLength();
SetSel(start, start);
ReplaceSel((LPCTSTR)strName);

long end = GetTextLength();
int line = GetLineCount();
SetSel(start - (line-2)*1, end - (line-2)*1);
SetSelectionCharFormat(cf); 

SetSel(-1, -1);
HideSelection(TRUE, FALSE);

PostMessage(WM_VSCROLL, SB_BOTTOM, 0);
update @ 2010.03.25 精益求精,上面的代码有隐患,应该用下面更安全、简洁的处理方法:
CHARFORMAT cf;
cf.cbSize = sizeof (CHARFORMAT);  
cf.dwMask = CFM_COLOR | CFM_UNDERLINE | CFM_BOLD;
cf.dwEffects = (unsigned long)~(CFE_AUTOCOLOR | CFE_UNDERLINE | CFE_BOLD);
cf.crTextColor = is_me?RGB(0x91, 0xe0, 0x2a):RGB(0x59, 0xb5, 0xff);

SetSel(-1, -1);
SetSelectionCharFormat(cf);
ReplaceSel((LPCTSTR)strName);
SetSel(-1, -1);
ReplaceSel((LPCTSTR)"\n");
PostMessage(WM_VSCROLL, SB_BOTTOM, 0);
本站文章除注明外,均为本站原创
转载请注明文章转载自: 大笨熊乐园 [ https://blog.foolbear.com/?m=0 ]
文章标题: 如何向 CRichEditCtrl 添加自定义字体的字符
文章地址: https://blog.foolbear.com/2010/03/cricheditctrl.html?m=0

没有评论 :

发表评论

Related Posts with Thumbnails