最近需要做vc的RichEdit控件里的内容关键字标红,由于RichEdit的内容可能是中英文混合的,所以需要先转成Unicode,再用wcsstr函数找到关键字出现的位置,再用SetSel、SelSelectionCharFormat方法进行标红。

//CString m_richText; RichEdit内容
//CString strKeyword; 关键字
CHARFORMAT cf;
ZeroMemory(&cf, sizeof(CHARFORMAT));
cf.dwMask = CFM_STRIKEOUT|CFM_BOLD | CFM_COLOR;
cf.dwEffects = CFE_BOLD;
cf.crTextColor = RGB(,,); richFileContext.GetWindowText(m_richText); if(m_richText.GetLength()<= || strKeyword.GetLength()<=)
{
return;
} char *pstrRichText = m_richText.GetBuffer();
char *pstrKeyword = strKeyword.GetBuffer();
wchar_t w_richtext[]={},w_keyword[]={};
MByteToWChar(pstrRichText,w_richtext,sizeof(w_richtext)/sizeof(w_richtext[]));
MByteToWChar(pstrKeyword,w_keyword,sizeof(w_keyword)/sizeof(w_keyword[]));
wchar_t *p = wcsstr(w_richtext,w_keyword);
while(p)
{
int nIndex = p-w_richtext;
int selIndex = nIndex; //每换行一次selIndex需要减1,不知道为啥,反复debug出的经验
for(int i=;i<richFileContext.GetLineCount();i++)
{
int lineIndex = richFileContext.LineIndex(i);
if(nIndex>lineIndex)
{
selIndex--;
}
else
{
break;
}
}
//////////////////////////////////////// richFileContext.SetSel(selIndex,selIndex+wcslen(w_keyword));
richFileContext.SetSelectionCharFormat(cf);
richFileContext.SetSel(-,-); p = wcsstr(w_richtext+nIndex+wcslen(w_keyword),w_keyword);
}

其中MByteToWChar如下:

//-------------------------------------------------------------------------------------
//Description:
// This function maps a character string to a wide-character (Unicode) string
//
//Parameters:
// lpcszStr: [in] Pointer to the character string to be converted
// lpwszStr: [out] Pointer to a buffer that receives the translated string.
// dwSize: [in] Size of the buffer
//
//Return Values:
// TRUE: Succeed
// FALSE: Failed
//
//Example:
// MByteToWChar(szA,szW,sizeof(szW)/sizeof(szW[0]));
//---------------------------------------------------------------------------------------
BOOL MByteToWChar(LPCSTR lpcszStr, LPWSTR lpwszStr, DWORD dwSize)
{
// Get the required size of the buffer that receives the Unicode
// string.
DWORD dwMinSize;
dwMinSize = MultiByteToWideChar (CP_ACP, , lpcszStr, -, NULL, ); if(dwSize < dwMinSize)
{
return FALSE;
} // Convert headers from ASCII to Unicode.
MultiByteToWideChar (CP_ACP, , lpcszStr, -, lpwszStr, dwMinSize);
return TRUE;
}

最新文章

  1. JavaFx导出文件
  2. log4net 运行时改变日志级别
  3. Atitit 面向对象 &#160;封装的实现原理
  4. arcglobe 图层三大类说明
  5. 使用dynatrace+showslow进行前端性能测试
  6. form 上传 html 代码
  7. Java Hour 53 HQL
  8. Linux/RedHat 编译安装GNU gcc 4.9.0 (g++)
  9. JavaScript执行上下文
  10. c++ 中关于int,unsigned int , short的关系与应用
  11. 绑定事件导致发送多个ajax请求的问题
  12. win8 VS2010 配制OpenGL
  13. HTML系列(五):超链接
  14. 安装mysql的遇到的问题
  15. SmtpDlg 调用SMTP
  16. python实现压缩当前文件夹下的所有文件
  17. PowerScript数据类型及变量
  18. Git储藏与恢复
  19. laravel上传文件FTP驱动配置
  20. Python 百度ai身份证接口案例

热门文章

  1. POJ 3276 Face The Right Way(前缀和优化)
  2. 转---秒杀多线程第十四篇 读者写者问题继 读写锁SRWLock
  3. 「美团 CodeM 资格赛」跳格子
  4. VSS2005设置不输入密码直接登录VSS
  5. 基于OpenResty和Node.js的微服务架构实践
  6. Linux常用网络工具:fping主机扫描
  7. Spring 源码学习(3) —— 增加属性注册编辑器
  8. 使用scikit-learn进行建模预测和评估操作_泰坦尼克号获救预测
  9. java绝对路径和相对路径的理解
  10. windows 安装elasticsearch-head插件