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

How to save specific part of a TRichEdit text RTF (c++ codegear)

本文关键字:定部 代码 RTF 何保存 保存 TRichEdit 文本 c++      更新时间:2023-10-16

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

双击一个单词时,该单词出现的所有背景都会像记事本++中一样涂成绿色。

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

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

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

我真的是一个编程初学者,任何帮助都很感激。如果不够清楚,请告诉我。

以下是我的代码,它正在工作并为单词的出现添加背景色:`void SearchInText::searchWordInText(TRichEdit*reTextToSearch,AnsiString strWordToFind){lstCoccurrences->Clear()//重置第一个

strTextToParse = AnsiReplaceText(strTextToParse, "rn", "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);
}

好吧,我终于拿到了!我在.h

中添加了一个结构在里面,我存储:
-找到的单词在TRichEdit中的位置(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, "rn", "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!
希望它能帮助其他人解决同样的问题!感谢那些建议代码=)

的人
相关文章: