既不也不TMemo
支持TRichEdit
多项选择,因此您实际上无法突出显示已替换的单词。但是使用TRichEdit
,您可以做的是更改您替换的单词的前景色/背景色。
TRichEdit
有一个返回搜索字符串索引的FindText()
方法(包装EM_FINDTEXT
消息)。循环调用它,或者您可以选择每个找到的单词,设置它的颜色,然后用新文本替换它。重复直到FindText()
找不到更多匹配项。
尝试这样的事情:
uses
RichEdit, CommDlg;
procedure TForm1.TranslateExecute(Sender: TObject);
var
I, Pos: Integer;
EventMask: LRESULT;
OldPattern, NewPattern: string;
Find: RichEdit.FINDTEXT;
Rng: RichEdit.CHARRANGE;
Fmt: RichEdit.CHARFORMAT2;
begin
EventMask := SendMessage(RichEdit1.Handle, EM_SETEVENTMASK, 0, 0);
RichEdit1.Lines.BeginUpdate;
try
for I := 0 to StrListV.Count - 1 do
begin
OldPattern := StrListV.Strings[I];
NewPattern := StrListV1.Strings[I];
Pos := 0;
repeat
Find.chrg.cpMin := Pos;
Find.chrg.cpMax := -1;
Find.lpstrText := PChar(OldPattern);
Pos := SendMessage(RichEdit1.Handle, EM_FINDTEXT, FR_DOWN or FR_WHOLEWORD, LPARAM(@Find));
if Pos = -1 then Break;
Rng.cpMin := Pos;
Rng.cpMax := Pos + Length(OldPattern);
ZeroMemory(@Fmt, SizeOf(Fmt));
Fmt.cbSize := SizeOf(Fmt);
Fmt.dwMask := CFM_COLOR or CFM_BACKCOLOR;
Fmt.crTextColor := ColorToRGB(clHighlightText);
Fmt.crBackColor := ColorToRGB(clHighlight);
SendMessage(RichEdit1.Handle, EM_EXSETSEL, 0, LPARAM(@Rng));
SendMessage(RichEdit1.Handle, EM_SETCHARFORMAT, SCF_SELECTION, LPARAM(@Fmt));
SendMessage(RichEdit1.Handle, EM_REPLACESEL, 0, LPARAM(PChar(NewPattern)));
Inc(Pos, Length(NewPattern));
until False;
end;
finally
RichEdit1.Lines.EndUpdate;
SendMessage(RichEdit1.Handle, EM_SETEVENTMASK, 0, EventMask);
end;
end;