【问题标题】:How to save specific part of a TRichEdit text RTF (c++ codegear)如何保存 TRichEdit 文本 RTF 的特定部分(c++ codegear)
【发布时间】: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);
}

} `

【问题讨论】:

  • 您可以使用StartLength 成员创建自己的结构,并将它们存储在列表或数组中。为您突出显示的每个单词保存 RichEdit->SelStart->SelLength。当您需要恢复它们时,遍历列表,将 RichEdit->SelStart 和 ->SelLength 更改为存储的值,并将该选择的颜色重置为原始颜色。 (如果需要,您甚至可以将原始颜色保存为该结构的一部分。)
  • @KenWhite:应该作为答案而不是评论发布。
  • @Remy:我这里没有安装 Builder 的机器,它离我的专业领域还很远。 :-)
  • @KenWhite:SelStart 的问题在于它不会在我的TRichEdit 中保留文本的 RTF。我的 .h 中已经有一个“列表”,即 TObjectList* lstOccurrences;。我用它来跟踪我们在哪一次出现,以便我们可以转到文本中的下一个。它指向单词在文本中开始的位置的 int。也是用EM_EXSETSEL设置了背景色。

标签: c++ c++builder trichedit


【解决方案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);
}

【讨论】:

  • 感谢您的建议。在我的情况下,我不想通过将crTextColorcrBackColor 设置为特定的东西来手动恢复突出显示的单词。我想将原始格式恢复到搜索功能突出显示之前的状态。 (我说的是你的 RestoreHighlightedWords() void。)第一个 void HighlightWords 似乎很棒。有一个 TSearchTypes SearchTypes = TSearchTypes() &lt;&lt; stWholeWord &lt;&lt; stMatchCase 我不确定。
  • +1。我知道你可以比我做得更好。 :-)
  • 就像 Ken 在他之前的评论中所说,您可以保存原始格式,然后在需要时恢复它。突出显示单词时,使用EM_EXSETSEL 选择特定单词后,使用EM_GETCHARFORMAT 检索其当前格式,然后使用EM_SETCHARFORMAT 更改它。将格式存储在列表中,稍后您可以使用EM_SETCHARFORMAT 恢复它。
  • 对于SearchTypes 变量,它只是分配TRichEdit::FindText() 作为输入的标志来影响它如何找到匹配的文本。 stWholeWordstMatchCase 可以一起指定、单独指定或根本不指定。这取决于您的特定需求。为了突出显示匹配的文本,将它们一起指定是有意义的。
【解决方案2】:

好的,我终于明白了! 我在我的 .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, "\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!
希望它能帮助其他有同样问题的人!感谢那些建议代码的人=)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多