void CMFCApplication3View::OnFindNext(LPCTSTR lpszFind, BOOL bNext, BOOL bCase)
{
CEdit& editCtrl = GetEditCtrl();
CString searchText = lpszFind;
CString content;
// 获取文本内容
editCtrl.GetWindowText(content);
// 获取当前选中范围
int nStartChar, nEndChar;
editCtrl.GetSel(nStartChar, nEndChar);
int startPos = bNext ? nEndChar : 0; // 从当前位置开始查找
int foundPos = -1;
// 查找文本
if (bCase)
foundPos = content.Find(searchText, startPos);
else
foundPos = content.MakeLower().Find(searchText.MakeLower(), startPos);
if (foundPos != -1)
{
// 设置选中的文本
editCtrl.SetSel(foundPos, foundPos + searchText.GetLength());
editCtrl.SetFocus();
}
else
{
AfxMessageBox(_T("Text not found. hh"));
}
}
void CMFCApplication3View::OnReplaceSel(LPCTSTR lpszFind, BOOL bNext, BOOL bCase, LPCTSTR lpszReplace)
{
CEdit& editCtrl = GetEditCtrl();
CString findText = lpszFind;
CString replaceText = lpszReplace;
// 获取当前选中范围
int nStartChar, nEndChar;
editCtrl.GetSel(nStartChar, nEndChar);
// 如果没有选中任何文本,则查找下一个匹配的文本
if (nStartChar == nEndChar)
{
OnFindNext(lpszFind, bNext, bCase);
editCtrl.GetSel(nStartChar, nEndChar);
}
// 获取选中的文本
CString selectedText;
editCtrl.GetWindowText(selectedText);
// 确保选中的文本在当前选择范围内
CString selectedTextInRange = selectedText.Mid(nStartChar, nEndChar - nStartChar);
// 替换选中的文本
if (selectedTextInRange.Compare(findText) == 0)
{
editCtrl.ReplaceSel(replaceText);
}
else
{
AfxMessageBox(_T("The selected text does not match the find text."));
}
}
For further actions, you may consider blocking this person and/or reporting abuse
Top comments (0)