最近需要做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;
}

VC RichEdit中英文关键字标红的更多相关文章

  1. laravel7 搜索关键字标红及手机号,身份证号隐藏

    控制器代码 public function index(Request $request) { //接受搜索关键字 $word = $request->get('name'); $start = ...

  2. think php 路由增删改查(搜索+关键字标红+缩略图)

    路由 use think\Route; //展示添加表单 Route::get('create','user/user/create'); //表单提交数据 Route::post('save','u ...

  3. python 安装 0x000007b错误解决及VC++ 安装第三方库报红

    dll 版本不对 dll 可能是 32 位和 64 位的 ,安装的可能不对 下载 DirectX_DLL修复工具v3.5增强版 进行修复 VC++ 安装第三方库报红问题 使用 VS 2017 或者 V ...

  4. 响应VC++ 标题栏右边的关闭按钮“红叉”

    击标题栏右边的关闭按钮“红叉”时,程序会向窗口发送WM_CLOSE消息,因此可以截取此消息在窗口关系前做一些提示或者是不允许点击时关闭程序 case WM_CLOSE: if (...) { Post ...

  5. excel怎么把单元格内某个字标红,其他字不变

    alt+F11,打开宏编辑器运行如下代码: Sub AAA() Dim R As Range, L As Long, S As String Application.ScreenUpdating = ...

  6. iOS UILabel 使用姿势大全(标红关键字)

    一.初始化 ? 1 2 3 UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, 40, 120, 44)];       ...

  7. JS正则对象 RegExp(有变量的时候使用),用来匹配搜索关键字(标红)

    1,平常我们写js正则规则的时候,一般是这样写: var reg = /abc/; 然而,这样写的话,如果abc是一个变量这样就不行,我们需要下面这种写法: var abc = "汉字&qu ...

  8. Java调用solrj5.5.3接口,查询数据

    前期准备 搭建solr服务 参考上一篇,搭建solr搜索服务. 添加依赖 maven工程的话,添加如下依赖, <!-- https://mvnrepository.com/artifact/or ...

  9. Atitit. C# java 的api 目录封装结构映射总结

    Atitit. C#  java 的api 目录封装结构映射总结 C# java ref System.Reflection System.Type, java.lang.ref concurrent ...

随机推荐

  1. request之setAtrribute

    当在servlet中有request.SetAtrribute("AtriruteName",AtrributeValue)语句时,在jsp页面获取AtrributeValue有两 ...

  2. 怎样搭建一个自有域名的 WORDPRESS 博客?

    博客搭建并不复杂,只是过程有点繁琐,适合喜欢折腾的人,主要有下面几个步骤: 新建一个博客文件 购买域名(Domain Name) 注册一个主机空间(Web Host) 域名解析(DNSPod) 安装W ...

  3. mysql安装使用详细教程

    1.数据库存储数据的方式与Excel类似. 一.数据库介绍 1.什么是数据库? 数据库(Database)是按照数据结构来组织.存储和管理数据的仓库, 每个数据库都有一个或多个不同的API用于创建,访 ...

  4. Django模板语言循环字典

    1. 对于字典,可以有下列用法: {% for row in user_dict.keys %} {% for row in user_dict.values %} {% for row in use ...

  5. 【刷题】BZOJ 4573 [Zjoi2016]大森林

    Description 小Y家里有一个大森林,里面有n棵树,编号从1到n.一开始这些树都只是树苗,只有一个节点,标号为1.这些树都有一个特殊的节点,我们称之为生长节点,这些节点有生长出子节点的能力.小 ...

  6. 不使用java内置函数,将String字符串转换为int类型

    package com.test; public class AtoiTest { public static void main(String[] args) throws Exception { ...

  7. 【BZOJ2329】括号修复(Splay)

    [BZOJ2329]括号修复(Splay) 题面 BZOJ 洛谷 题解 本来想着用线段树来写 但是有一个区间翻转 所以不能用线段树了,就只能用平衡树 然后直接\(Splay\)就好了 注意一下几个标记 ...

  8. Hive(三)hive的高级操作

    一.hive的各种join操作 语法结构:join_table:table_reference JOIN table_factor [join_condition]| table_reference ...

  9. bzoj4144【AMPPZ2014】Petrol

    题解:  首先注意到起点和终点都是加油站;          假设中途经过某个非加油站的点u,u连到v,离u最近的加油站是x,那么从u到x加油后回到u,再到v一定不比直接从u到v差:        因 ...

  10. codeforces 55D 数位dp

    D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...