呵呵,这是我自己的总结啊,不看是你的损失
我用字符串的形式对CRichEditCtrl进行查找:具体代码如下:
UpdateData(TRUE);
CString str;
m_RichTex.GetWindowText(str);
int len=strEdit.GetLength();
long index=0,end_index=0;
repText.Format("%d",len);
strEdit.MakeLower();
str.MakeLower();
if(m_down==IDC_DOWN)
{
m_RichTex.GetSel(index,end_index);//向下查找
index=str.Find(strEdit,end_index);
if (index!=-1)
{
m_RichTex.SetSel(index,index+len);
m_RichTex.SetFocus();
}
else
{
index=str.Find(strEdit,0);
if (index==-1)
{
MessageBox("搜索的字符串未找到", " 提示", MB_OK);
}
m_RichTex.SetSel(index,index+len);
m_RichTex.SetFocus();
}
}
else
{
m_RichTex.GetSel(index,end_index);//向上查找
CString strReverse=str.MakeReverse();
CString strEditReverse=strEdit.MakeReverse();
index=strReverse.Find(strEditReverse,str.GetLength()-index);
if (index!=-1)
{
end_index=str.GetLength()-index-1;
}
else
{
index=strReverse.Find(strEditReverse,0);
if (index==-1)
{
MessageBox("搜索的字符串未找到", " 提示", MB_OK);
}
end_index=str.GetLength()-index-1;
}
m_RichTex.SetSel(end_index+1-len,end_index+1);
m_RichTex.SetFocus();
}
}
结果运行时查找结果和字符对应不上。经网上查资料得知将空间中的内容转化成字符串存在这编码的差异,还有可能和CRichEdit版本有关。综合各种原因我的解决办法就是回头去用Findtext.
相信会有很多和我一样的菜鸟会遇到相同问题,希望能给你带去光明。