【发布时间】:2014-11-03 21:20:18
【问题描述】:
我正在现有的 c++ CodeGear 项目中创建搜索功能。
当你双击一个单词时,所有出现的单词的背景都被涂成绿色,就像在 notepad++ 中一样。
在应用颜色之前,我将原始 TRichEDit 文本保存在 TMemoryStream 中,以便能够取回原始文本。我在TRichEdit 中的点击事件中将颜色重置为正常。
我想知道是否有办法将每次出现的搜索词保存在 TMemoryStream 中,或者使用像 EM_STREAMOUT 这样的消息?
现在一切正常,但是当TRichEdit 文本太大时,重新加载所有文本的大备忘录需要很长时间。我认为最好只记住改变的单词的颜色而不是重新加载所有文本。
我真的是编程初学者,任何帮助都非常感谢。如果还不够清楚,请告诉我。
这是我的代码,它正在工作并将背景颜色添加到单词的出现处: ` 无效 SearchInText::searchWordInText(TRichEdit* reTextToSearch, AnsiString strWordToFind) { lstOccurrences->清除(); //重置lst
strTextToParse = AnsiReplaceText(strTextToParse, "\r\n", "\n");
int nPrevTagPos = 0;
int nTagPos = strTextToParse.AnsiPos(strWordToFind);
while (nTagPos != 0)
{
int nPosMin = nPrevTagPos + nTagPos - 1;
//List of all the occurrence in the TRichEdit with their position in the text
//It's not a list of int, but it POINT to adresses of INT so it's the same result =)
lstOccurrences->Add((TObject*) nPosMin);
//Change color of background when there is an occurrence
changeBgColor(reTextToSearch, strWordToFind, nPosMin +1, 155, 255, 155); //lime
bColorWasApplied = true;
nPrevTagPos = nPosMin + strWordToFind.Length();
strTextToParse = strTextToParse.SubString(nTagPos + strWordToFind.Length(), strTextToParse.Length());
nTagPos = strTextToParse.AnsiPos(strWordToFind);
}
} `
【问题讨论】:
-
您可以使用
Start和Length成员创建自己的结构,并将它们存储在列表或数组中。为您突出显示的每个单词保存RichEdit->SelStart和->SelLength。当您需要恢复它们时,遍历列表,将 RichEdit->SelStart 和 ->SelLength 更改为存储的值,并将该选择的颜色重置为原始颜色。 (如果需要,您甚至可以将原始颜色保存为该结构的一部分。) -
@KenWhite:应该作为答案而不是评论发布。
-
@Remy:我这里没有安装 Builder 的机器,它离我的专业领域还很远。 :-)
-
@KenWhite:SelStart 的问题在于它不会在我的
TRichEdit中保留文本的 RTF。我的 .h 中已经有一个“列表”,即TObjectList* lstOccurrences;。我用它来跟踪我们在哪一次出现,以便我们可以转到文本中的下一个。它指向单词在文本中开始的位置的 int。也是用EM_EXSETSEL设置了背景色。
标签: c++ c++builder trichedit