查找功能的实现

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 // 替换对话框
{
}
}



...全文
241 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用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后的字符,而在查找时位置就不对了

15,980

社区成员

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

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