查找功能的实现

haha0909 2015-08-26 03:10:45
又是Unicode的问题,GelSel和SetSel位置总是不对,这里怎么处理?


LRESULT CFMDIDEView::OnFindReplace(WPARAM wParam,LPARAM lParam) //wParam = 0替换 ,wParam = 1查找
{
CString strText,repText;
strText=m_DlgFind->m_strFindWord; // 要查找的字符串
CString str;
CRichEditCtrl & theEdit=GetRichEditCtrl(); // RICHEDIT控件中的总的字符串
theEdit.GetWindowText(str);
int len=strText.GetLength(); // 要查找的字符串长度
long index=0,end_index=0; // 索引的起始位置 及 结束位置, 从0开始
if (wParam) // 查找对话框
{
if (m_DlgFind == NULL) // 销毁对话框 首先检查是否撤销对话框,否则可能会先执行其他操作,然后再撤销
{
m_DlgFind->DestroyWindow();
m_DlgFind=NULL;
return 0;
}
if (m_DlgFind->m_bDirection) // 向下搜索
{
theEdit.GetSel(index,end_index); //得到查询的起始与结束位置 为半闭半开区域 [index,end_index)
index=str.Find(strText,end_index); // 从上次查询的结束位置处继续往下查询
if (index!=-1) //往下搜索时,找到所要查询的子字符串
{
theEdit.SetSel(index,index+len); // 为找到的子字符串 设置选择标记
theEdit.SetFocus(); // 在找到的子字符串上,设置焦点
}
else // 往下搜索时,未找到所要查找的子字符串
{
index=str.Find(strText,0); //从文件开始处,重新查找, 可以实现当搜索到文件末尾时,点查找下一个按钮时,可以从头重新查,从而实现循环功能
if (index==-1)
{
MessageBox(L"以查找完毕,无所查内容"); //若从开始处查找,仍然找不到,则说明无此查找内容
return 0;
}
theEdit.SetSel(index,index+len); // 若从开始处查找,查询到所查内容,则为其做标记
theEdit.SetFocus();
}
}
else // 向上搜索
{
theEdit.GetSel(index,end_index); // 得到当前选择文本的始末位置
CString strReverse=str.MakeReverse(); // 对控件中的文字进行反转
CString strTextReverse=strText.MakeReverse(); // 对查找串的文字进行反转
index=strReverse.Find(strTextReverse,str.GetLength()-index); ////从反串中往下查找 查找的起点位置索引为 ID=STR.Length-index-1的前一位,即从源串开始位置的前一位搜索 ,不能从源串开始位置处搜索,因为若仅单个字符时,总是查询到这个位置,不会再往前查询了
if (index!=-1)
{
end_index=str.GetLength()-index-1; // 源串与反串index 的关系 反串的起始位置即为源串的末尾
}
else
{
index=strReverse.Find(strTextReverse,0);
if (index==-1)
{
MessageBox(L"以查找完毕,无所查内容"); //若从开始处查找,仍然找不到,则说明无此查找内容
return 0;
}
end_index=str.GetLength()-index-1; //转换成源串的末尾位置索引 反串的起始位置即为源串的末尾
}
theEdit.SetSel(end_index+1-len,end_index+1); // 由结尾位置 倒推源串,其中要包含结尾位置。 SETSEL 为【)区间,所以要使位置end_index 包含之中,范围必须是setsel(,end_index+1)
theEdit.SetFocus();
}
}
else // 替换对话框
{
}
}



...全文
208 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
haha0909 2015-08-28
  • 打赏
  • 举报
回复
至于GetSel和SetSel为啥不对还是没搞清楚,先结贴
haha0909 2015-08-28
  • 打赏
  • 举报
回复
搞定


void CFMDIDEView::FindText( LPCTSTR pszText,BOOL bWholeWord,BOOL bCaseSen,BOOL bUp )
{
 	CRichEditCtrl & theEdit=GetRichEditCtrl();   // RICHEDIT控件中的总的字符串   
	 DWORD dwOptions = 0;
	 if (bWholeWord)
		 dwOptions |= FR_WHOLEWORD;
	 if (bCaseSen)
		 dwOptions |= FR_MATCHCASE;
	 if (!bUp)
		 dwOptions |= FR_DOWN;
 
	 CHARRANGE crCurSel;
	 theEdit.GetSel(crCurSel);
	 FINDTEXTEXW  ftw;
	 ZeroMemory(&ftw,sizeof(FINDTEXTEXW));
	 if (bUp) //注意,如果是向上查找的话,FINDTEXTEXW 中的chrg 应该是倒序
	 {
		 ftw.chrg.cpMin = crCurSel.cpMin ;
		 ftw.chrg.cpMax = 0;
	 }
	 else
	 {
		 ftw.chrg.cpMin = crCurSel.cpMax;
		 ftw.chrg.cpMax = -1;
	 }
#ifdef _UNICODE
 ftw.lpstrText = pszText;
#else
 USES_CONVERSION;
 ftw.lpstrText = A2W(pszText);
#endif
 
	if( -1 == theEdit.SendMessage(EM_FINDTEXTEXW,dwOptions,(LPARAM)&ftw) )
	{
		CString strText;
		strText.Format(_T("没有找到 \" %s \" "),pszText);
		MessageBox(strText);
	}
	else
	{
		theEdit.SetSel(ftw.chrgText);
		theEdit.SendMessage(EM_HIDESELECTION,0,0);
	}

}
赵4老师 2015-08-28
  • 打赏
  • 举报
回复
C:\Program Files\Tencent\QQ\Bin\Riched20.dll
haha0909 2015-08-28
  • 打赏
  • 举报
回复
EM_FINDTEXTEXW message 找到一个这个,研究一下
haha0909 2015-08-28
  • 打赏
  • 举报
回复
引用 4 楼 zhao4zhong1 的回复:
据说CRichEdit有很多版本,其中有不够新的,也有不是Unicode版的。

//装入rich edit version 2.0
	if (LoadLibrary(_T("RICHED20.DLL")) == NULL)
	{
		AfxMessageBox(_T("Fail to load \"riched20.dll\"."),
			MB_OK | MB_ICONERROR);
		PostMessage(WM_QUIT,0,0);
		return FALSE;
	}
	m_strClass = RICHEDIT_CLASSA;	//for 2.0 class 
	cs.style = cs.style & (~WS_VSCROLL);
	
	AfxInitRichEdit2();
我用的是Unicode版本,2.0,那个版本才是最新最好用的呢
赵4老师 2015-08-27
  • 打赏
  • 举报
回复
Strings in MFC Home | Overview | How Do I This article describes the general-purpose services that the class library provides related to string manipulation. Topics covered in this article include: Unicode and MBCS provide portability CStrings and const char pointers CString reference counting The CString class provides support for manipulating strings. It is intended to replace and extend the functionality normally provided by the C run-time library string package. The CString class supplies member functions and operators for simplified string handling, similar to those found in Basic. The class also provides constructors and operators for constructing, assigning, and comparing CStrings and standard C++ string data types. Because CString is not derived from CObject, you can use CString objects independently of most of the Microsoft Foundation Class Library (MFC). CString objects follow “value semantics.” A CString object represents a unique value. Think of a CString as an actual string, not as a pointer to a string. A CString object represents a sequence of a variable number of characters. CString objects can be thought of as arrays of characters. Unicode and MBCS Provide Portability With MFC version 3.0 and later, MFC, including CString, is enabled for both Unicode and Multibyte Character Sets (MBCS). This support makes it easier for you to write portable applications that you can build for either Unicode or ANSI characters. To enable this portability, each character in a CString object is of type TCHAR, which is defined as wchar_t if you define the symbol _UNICODE when you build your application, or as char if not. A wchar_t character is 16 bits wide. (Unicode is available only under Windows NT.) MBCS is enabled if you build with the symbol _MBCS defined. MFC itself is built with either the _MBCS symbol (for the NAFX libraries) or the _UNICODE symbol (for the UAFX libraries) defined. Note The CString examples in this and the accompanying articles on strings show literal strings properly formatted for Unicode portability, using the _T macro, which translates the literal string to the form L"literal string" which the compiler treats as a Unicode string. For example, the following code: CString strName = _T("Name"); is translated as a Unicode string if _UNICODE is defined or as an ANSI string if not. For more information, see the article Strings: Unicode and Multibyte Character Set (MBCS) Support. A CString object can store up to INT_MAX (2,147,483,647) characters. The TCHAR data type is used to get or set individual characters inside a CString object. Unlike character arrays, the CString class has a built-in memory allocation capability. This allows CString objects to automatically grow as needed (that is, you don’t have to worry about growing a CString object to fit longer strings). CStrings and const char Pointers A CString object also can act like a literal C-style string (an LPCTSTR, which is the same as const char* if not under Unicode). The LPCTSTR conversion operator allows CString objects to be freely substituted for character pointers in function calls. The CString( LPCTSTR lpsz ) constructor allows character pointers to be substituted for CString objects. No attempt is made to fold CString objects. If you make two CString objects containing Chicago, for example, the characters in Chicago are stored in two places. (This may not be true of future versions of MFC, so you should not depend on it.) Tips Use the GetBuffer and ReleaseBuffer member functions when you need to directly access a CString as a nonconstant pointer to a character (LPTSTR instead of a const character pointer, LPCTSTR). Use the AllocSysString and SetSysString member functions to allocate and set BSTR objects used in Automation (formerly known as OLE Automation). Where possible, allocate CString objects on the frame rather than on the heap. This saves memory and simplifies parameter passing. The CString class is not implemented as a Microsoft Foundation Class Library collection class, though CString objects can certainly be stored as elements in collections. CString Reference Counting As of MFC version 4.0, when CString objects are copied, MFC increments a reference count rather than copying the data. This makes passing parameters by value and returning CString objects by value more efficient. These operations cause the copy constructor to be called, sometimes more than once. Incrementing a reference count reduces that overhead for these common operations and makes using CString a more attractive option. As each copy is destroyed, the reference count in the original object is decremented. The original CString object is not destroyed until its reference count is reduced to zero. You can use the CString member functions LockBuffer and UnlockBuffer to disable or enable reference counting. Further Reading About Strings The following articles provide more information about CString: Strings: Basic CString Operations Strings: CString Semantics Strings: CString Operations Relating to C-Style Strings Strings: CString Exception Cleanup Strings: CString Argument Passing Strings: Unicode and Multibyte Character Set (MBCS) Support
haha0909 2015-08-27
  • 打赏
  • 举报
回复
没有人用UNICODE吗,为啥总是出现许多莫名其妙的问题
赵4老师 2015-08-27
  • 打赏
  • 举报
回复
据说CRichEdit有很多版本,其中有不够新的,也有不是Unicode版的。
haha0909 2015-08-27
  • 打赏
  • 举报
回复
赵4老师,这一大段英文我费力啃完,但是并没有发现有什么*用 我做的是一个编辑器,这个编辑器可能打开的是外部编辑器编辑的文字,有可能是Unicode,有可能是非Unicode,但是我的程序是Unicode的。编辑器视图是基于CRichEditView的,当我GetSelText的时候也是得到的乱码,然后我这样处理了一下 :

	int nTemp = theEdit.LineIndex(nLine);
	nTemp = theEdit.LineLength(nTemp)*16+32; //缓冲区必须足够大,否则读取中文时可能会有乱码

	char *achLine = new char[nTemp];
	//findString = theEdit.GetSelText(); //乱码
	theEdit.GetSelText(achLine);//将上一句换成这样才能得正确的字符,否则就是乱码
	CString findString;
	CodeSwitchForMTW(achLine,findString);//多字节转宽字符
	delete achLine;
这样处理得到了正确的字符,然而这个是转为Unicode后的字符,而在查找时位置就不对了
一、项目简介本课程演示的是一套基于SSM实现的宠物销售网站,主要针对计算机相关专业的正在做毕设的学生与需要项目实战练习的Java学习者。课程包含:1. 项目源码、项目文档、数据库脚本、软件工具等所有资料2. 带你从零开始部署运行本套系统3. 该项目附带的源码资料可作为毕设使用4. 提供技术答疑二、技术实现后台框架:Spring、SpringMVC、MyBatisUI界面:JSP、jQuery 数据库:MySQL 三、系统功能该系统主要分为前台和后台两大功能模块,共包含两个角色:用户、管理员。具体的系统功能如下:1.前台功能前台首页、宠物商品展示、宠物搜索、宠物分类查找、宠物详情、网站公告、特价商品、推荐商品、全部商品、查看更多商品、加入购物车、确认订购信息、修改购物车、支付结算、清空购物车、提交订单、用户注册、用户登陆、用户中心、修改个人信息、修改密码、我的订单、订单详情、用户留言、我的留言等功能。2.后台功能 后台系统登陆、用户信息管理、管理员管理、宠物分类管理、宠物信息管理、订单信息管理、订单明细管理、用户留言管理、新闻公告管理等功能。该系统功能完善、界面美观、操作简单、功能齐全、管理便捷,具有很高的应用价值。 四、项目截图1)前台首页2)宠物详情页面3)购物车4)我的订单5)宠物信息管理6)订单信息管理  更多Java毕设项目请关注【毕设系列课程】https://edu.csdn.net/lecturer/2104   
项目介绍:[含论文+答辩PPT+源码等]SSM个人博客系统的设计与实现[包运行成功]系统说明:《SSM个人博客系统的设计与实现》该项目采用技术jsp、SpringMVC、Spring、Mybatis、tomcat服务器、mysql数据库 开发工具eclipse,项目含有源码、论文、配套开发软件、软件安装教程、项目发布教程个人博客系统主要用于发表个人博客,记录个人生活日常,学习心得,技术分享等,供他人浏览,查阅,评论等。本系统结构如下:(1)博主端:登录模块:登入后台管理系统:首先进入登录页面,需要输入账号和密码。它会使用Shiro进行安全管理,对前台输入的密 码进行加密运算,然后与数据库中的进行比较。成功后才能登入后台系统。博客管理模块: 博客管理功能分为写博客和博客信息管理。写博客是博主用来发表编写博客的,需要博客标题,然后选择博 客类型,最后将博客内容填入百度的富文本编辑器中,点击发布博客按钮即可发布博客。博客类别管理模块:博主类别管理系统可以添加,修改和删除博客类型名称和排序序号。将会显示到首页的按日志类别区域。 游客可以从这里查找相关的感兴趣的博客内容评论信息管理模块:评论管理功能分为评论审核和评论信息管理两部分。评论审核是当有游客或自己发表了评论之后,博主需 要在后台管理系统中审核评论。若想将此评论显示在页面上则点击审核通过,否则点击审核不通过。个人信息管理模块:修改博主的个人信息,可以修改昵称,个性签名,可以添加个人头像,修改个人简介;系统管理功能模块:友情链接管理,修改密码,刷新系统缓存和安全退出,友情链接管理可以添加,修改,删除友情链接网址(2)游客端:查询博客: 查询具体哪一篇博客查看博客内容: 查看博客内容查看博主个人信息:查看博主个人简介发表评论: 可以评论具体某篇博客友情链接: 查看友情链接适用场景:毕业论文、课程设计、公司项目参考运行截图:

15,979

社区成员

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

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