如何保存TRichEdit文本RTF(c+;+;codegear)的特定部分 我在一个现有的C++代码档项目中创建了一个搜索特性。
双击一个单词时,该单词的所有出现的背景都是绿色的,就像在记事本++中一样 在应用颜色之前,我将原始的如何保存TRichEdit文本RTF(c+;+;codegear)的特定部分 我在一个现有的C++代码档项目中创建了一个搜索特性。,c++,c++builder,trichedit,C++,C++builder,Trichedit,双击一个单词时,该单词的所有出现的背景都是绿色的,就像在记事本++中一样 在应用颜色之前,我将原始的TRichEDit文本保存在一个TMemoryStream中,以便能够取回原始文本。我在TRichEdit中将颜色重置为正常点击事件 我想知道是否有办法将每次出现的搜索词保存在TMemoryStream中,或者使用类似EM\u STREAMOUT的消息 现在一切正常,但是当TRichEdit文本太大时,需要永远重新加载所有文本的大备忘录。我认为最好只记住改变过的单词的颜色,而不是重新加载所有文本
TRichEDit
文本保存在一个TMemoryStream中,以便能够取回原始文本。我在TRichEdit
中将颜色重置为正常点击事件
我想知道是否有办法将每次出现的搜索词保存在TMemoryStream
中,或者使用类似EM\u STREAMOUT
的消息
现在一切正常,但是当TRichEdit
文本太大时,需要永远重新加载所有文本的大备忘录。我认为最好只记住改变过的单词的颜色,而不是重新加载所有文本
我真的是一个编程初学者,任何帮助都是感激的。如果不够清楚,请告诉我
下面是我的代码,它正在工作,并将背景色添加到word的出现处:
`
void SearchInText::searchWordInText(TRichEdit*reTextToSearch,AnsiString strWordToFind)
{
lst事件->清除();//重置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);
}
}
`试试这样的方法:
#include <vector>
struct WordOccurrence
{
CHARRANGE Range;
CHARFORMAT2 OriginalFormat;
};
std::vector<WordOccurrence> WordOccurrences;
void TMyForm::HighlightWords(const String &WordToFind)
{
// disable the RichEdit's notification messages
int OriginalEventMask = RichEdit1->Perform(EM_SETEVENTMASK, 0, 0);
// disable the RichEdit's painting
RichEdit1->Perform(WM_SETREDRAW, FALSE, 0);
// save the RichEdit's current selection
CHARRANGE OriginalSelection;
RichEdit1->Perform(EM_EXGETSEL, 0, (LPARAM)&OriginalSelection);
// assign values to use while searching
int WordLen = WordToFind.Length();
int TextLen = RichEdit1->GetTextLen();
TSearchTypes SearchTypes = TSearchTypes() << stWholeWord << stMatchCase;
// find the first occurrence of the word
int StartPos = RichEdit1->FindText(WordToFind, 0, TextLen, SearchTypes);
while (StartPos != -1)
{
WordOccurrence Occurrence;
Occurrence.Range.cpMin = StartPos;
Occurrence.Range.cpMax = StartPos + WordLen;
// select the word
RichEdit1->Perform(EM_EXSETSEL, 0, (LPARAM)&Occurrence.Range);
// get the word's current formatting
Occurrence.OriginalFormat.cbSize = sizeof(CHARFORMAT2);
RichEdit1->Perform(EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&Occurrence.OriginalFormat);
// save it for later
WordOccurrences.push_back(Occurrence);
// set the word's new formatting
CHARFORMAT2 NewFormat = Occurrence.OriginalFormat;
NewFormat.dwMask |= (CFM_COLOR | CFM_BACKCOLOR);
NewFormat.crTextColor = ...;
newFormat.crBackColor = ...;
RichEdit1->Perform(EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&NewFormat);
// find the next occurrence of the word
StartPos = RichEdit1->FindText(WordToFind, Occurrence.Range.cpMax, TextLen - Occurence.Range.cpMax, SearchTypes);
}
// restore the RichEdit's original selection
RichEdit1->Perform(EM_EXSETSEL, 0, (LPARAM)&OriginalSelection);
// re-enable the RichEdit's painting
RichEdit1->Perform(WM_SETREDRAW, TRUE, 0);
RichEdit1->Invalidate();
// re-enable the RichEdit's notification messages
RichEdit1->Perform(EM_SETEVENTMASK, 0, OriginalEventMask);
}
void TMyForm::RestoreHighlightedWords()
{
// are there any occurrences to restore?
if (WordOccurances.empty())
return;
// disable the RichEdit's notification messages
int OriginalEventMask = RichEdit1->Perform(EM_SETEVENTMASK, 0, 0);
// disable the RichEdit's painting
RichEdit1->Perform(WM_SETREDRAW, FALSE, 0);
// save the RichEdit's current selection
CHARRANGE OriginalSelection;
RichEdit1->Perform(EM_EXGETSEL, 0, (LPARAM)&OriginalSelection);
// restore the formatting of each occurrence
for (std::vector<WordOccurrence>::iterator iter = WordOccurrences.begin();
iter != WordOccurrences.end();
++iter)
{
WordOccurrence &occurrence = *iter;
// select the word
RichEdit1->Perform(EM_EXSETSEL, 0, (LPARAM)&occurrence.Range);
// restore the word's original formatting
RichEdit1->Perform(EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&occurrence.OriginalFormat);
}
// clear the list
WordOccurances.clear();
// restore the RichEdit's original selection
RichEdit1->Perform(EM_EXSETSEL, 0, (LPARAM)&OriginalSelection);
// re-enable the RichEdit's painting
RichEdit1->Perform(WM_SETREDRAW, TRUE, 0);
RichEdit1->Invalidate();
// re-enable the RichEdit's notification messages
RichEdit1->Perform(EM_SETEVENTMASK, 0, OriginalEventMask);
}
#包括
结构词发生
{
CHARRANGE范围;
字符格式2原始格式;
};
std::向量词;
void TMyForm::HighlightWords(常量字符串和WordToFind)
{
//禁用RichEdit的通知消息
int OriginalEventMask=RichEdit1->Perform(EM_SETEVENTMASK,0,0);
//禁用RichEdit的绘制
RichEdit1->Perform(WM_SETREDRAW,FALSE,0);
//保存RichEdit的当前选择
CHARRANGE原始选举;
RichEdit1->Perform(EM_EXGETSEL,0,(lpram)和OriginalSelection);
//指定搜索时要使用的值
int-WordLen=WordToFind.Length();
int TextLen=RichEdit1->GetTextLen();
TSearchTypes SearchTypes=TSearchTypes()执行(EM_EXSETSEL,0,(lpram)和Occurrence.Range);
//获取单词的当前格式
Occurrence.OriginalFormat.cbSize=sizeof(CHARFORMAT2);
RichEdit1->Perform(EM_GETCHARFORMAT,SCF_SELECTION,(LPARAM)和Occurrence.OriginalFormat);
//留着以后用
单词引用。推回(引用);
//设置单词的新格式
CHARFORMAT2 NewFormat=Occurrence.OriginalFormat;
NewFormat.dwMask |=(CFM|U颜色| CFM|U背景色);
NewFormat.crTextColor=。。。;
newFormat.crBackColor=。。。;
RichEdit1->Perform(EM_SETCHARFORMAT,SCF_SELECTION,(LPARAM)和NewFormat);
//查找该单词的下一个匹配项
StartPos=RichEdit1->FindText(WordToFind,Occurrence.Range.cpMax,TextLen-Occurrence.Range.cpMax,SearchTypes);
}
//恢复RichEdit的原始选择
RichEdit1->Perform(EM_EXSETSEL,0,(lpram)和OriginalSelection);
//重新启用RichEdit的绘制
RichEdit1->Perform(WM_SETREDRAW,TRUE,0);
RichEdit1->Invalidate();
//重新启用RichEdit的通知消息
RichEdit1->Perform(EM_SETEVENTMASK,0,OriginalEventMask);
}
void TMyForm::RestoreHighlightedWords()
{
//是否有要还原的事件?
if(WordOccurances.empty())
返回;
//禁用RichEdit的通知消息
int OriginalEventMask=RichEdit1->Perform(EM_SETEVENTMASK,0,0);
//禁用RichEdit的绘制
RichEdit1->Perform(WM_SETREDRAW,FALSE,0);
//保存RichEdit的当前选择
CHARRANGE原始选举;
RichEdit1->Perform(EM_EXGETSEL,0,(lpram)和OriginalSelection);
//恢复每个事件的格式
for(std::vector::iterator iter=wordexecutions.begin();
iter!=wordoccurrencess.end();
++国际热核聚变实验堆(iter)
{
单词发生率和发生率=*iter;
//选择单词
RichEdit1->Perform(EM_EXSETSEL,0,(lpram)和occurrence.Range);
//恢复单词的原始格式
RichEdit1->Perform(EM_SETCHARFORMAT,SCF_SELECTION,(lpram)和occurrence.OriginalFormat);
}
//清除列表
单词出现。清除();
//恢复RichEdit的原始选择
RichEdit1->Perform(EM_EXSETSEL,0,(lpram)和OriginalSelection);
//重新启用RichEdit的绘制
RichEdit1->Perform(WM_SETREDRAW,TRUE,0);
RichEdit1->Invalidate();
//重新启用RichEdit的通知消息
RichEdit1->Perform(EM_SETEVENTMASK,0,OriginalEventMask);
}
好的,我终于明白了!
我在my.h中添加了一个结构在它里面,我存储:
-找到的单词在拼图中的位置(
nStart
)-单词的长度(
nLength
)-实际文本和他的RTF
struct sSelectedWord : public TObject
{
public:
__fastcall ~sSelectedWord(); //destructor
int nStart;
int nLength;
TMemoryStream* memoRTF;
};
下面是将我的TRichEdit的RTF保存在我刚在.h中创建的结构中的代码
void SearchInText::searchWordInText(TRichEdit* reTextToSearch, AnsiString strWordToFind)
{
lstOccurrences->Clear(); //reset lst
lstOccWithRTFMemo->Clear();
nCountOccurrence = 0;
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);
nCountOccurrence++;
//selected the word in the TRichEdit to save it with is RTF
reTextToSearch->SelStart = nPosMin;
reTextToSearch->SelLength = strWordToFind.Length();
TMemoryStream* memo = new TMemoryStream;
//important part!
rtfSaveStream(reTextToSearch,memo);
sSelectedWord* currentWord = new sSelectedWord;
currentWord->nStart = nPosMin;
currentWord->nLength = strWordToFind.Length();
currentWord->memoRTF = memo;
//Here we go, we add our new object in a list to be able to loop through it when we will want to reset the color
lstOccWithRTFMemo->Add(currentWord);
lstOccWithRTFMemo->Count;
//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);
}
}
重要的部分是通过EM_STREAMOUT消息完成的
void SearchInText::rtfSaveStream(TRichEdit* re, TMemoryStream* memo)
{
// Create an instance of an EDITSTREAM that will contain:
// - The detail of our callback (StreamInCallback)
// - The TMemoryStream that contains the text to paste
EDITSTREAM es = {0};
ZeroMemory(&es, sizeof(es));
es.dwCookie = (DWORD_PTR) memo;
es.dwError = 0;
es.pfnCallback = &StreamSaveInCallback ; //pointer to function callBack
//To save the selected word of the TRichEdit, use STREAMOUT
re->Perform(EM_STREAMOUT, SF_RTF | SFF_SELECTION, (LPARAM)&es);
}
DWORD CALLBACK SearchInText::StreamSaveInCallback(DWORD_PTR dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb)
{
TMemoryStream *memo = (TMemoryStream*)dwCookie;
memo->Write(pbBuff, cb);
*pcb = cb;
return 0;
}
在pbuff
中,您可以看到您用他的RTF在TRichEdit中选择的单词!
希望它能帮助其他人解决同样的问题!感谢那些建议使用code=)的人,您可以使用
Start
和Length
成员创建自己的结构,并将它们存储在列表或数组中。为突出显示的每个单词保存RichEdit->SelStart
和->SelLength
。当您需要恢复它们时,在列表中循环,将RichEdit->SelStart和->SelLength更改为存储值,并将该选择的颜色重置为原始颜色。(如果需要,您甚至可以将原始颜色保存为该结构的一部分。)@KenWhite:这应该作为答案而不是评论发布。@Remy:我这里没有安装带Builder的机器,而且它离我的专业领域很远。:-)@肯怀特:SelStart的问题是它没有在myTRichEdit
中保留文本的RTF。我的.h中已经有一个“列表”,即TObje