如何保存TRichEdit文本RTF(c ++代码集)的特定部分

时间:2014-11-03 21:20:19

标签: c++ c++builder trichedit

我在现有的c ++ CodeGear项目中创建搜索功能。

当你双击一个单词时,所有出现的单词的背景都是绿色的,就像在记事本++中一样。

在应用颜色之前,我将原始TRichEDit文本保存在TMemoryStream中,以便能够获取原始文本。我在TRichEdit的点击事件中将颜色重置为正常。

我想知道是否有办法在TMemoryStream中保存搜索字的每一次出现,或者使用EM_STREAMOUT之类的消息?

现在一切正常,但是当TRichEdit文本太大时,重新加载所有文本的大备忘录需要花费很长时间。我认为最好只记住改变的单词的颜色,然后重新加载所有文本。

我真的是编程的初学者,任何帮助都是值得欣赏的。告诉我它是否不够清楚。

这是我的代码正在运行并将背景颜色添加到单词的出现位置: ` void 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);
}

} `

2 个答案:

答案 0 :(得分:1)

尝试这样的事情:

#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);
}

答案 1 :(得分:0)

好的,所以我终于明白了! 我在.h中添加了一个结构 在里面,我存储:
- 找到的单词TRichEdit中的位置(nStart
- 单词的长度(nLength
- 实际文本和他的RTF

struct sSelectedWord : public TObject
{
    public:
        __fastcall  ~sSelectedWord(); //destructor
        int nStart;
        int nLength;
        TMemoryStream* memoRTF;
};

这是在我刚刚在.h中创建的结构中保存TRichEdit的RTF的代码。

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中,您可以看到您在TRichEdit中选择的单词 他的RTF!
希望它能帮助其他人解决同样的问题!感谢那些建议代码=)

的人