【发布时间】:2019-10-06 19:34:27
【问题描述】:
我正在使用 TRichEdit 来保存电子邮件客户端的正文。我已经为用户提供了简单的格式化功能(粗体、斜体、下划线、左、中和右段落对齐和项目符号。这适用于使用 Indy 按照 Remy 的代码here 将格式化文本作为 html 发送电子邮件。
我使用
将 TRichEdit 文本提取为 htmlfunction GetHTML(RichEdit:TRichEdit): string;
var
htmlstrings : Tstringlist;
JvRichEditToHtml1 :TJvRichEditToHtml;
begin
htmlstrings := Tstringlist.create;
JvRichEditToHtml1 := TJvRichEditToHtml.create(nil);
try
JvRichEditToHtml1.ConvertToHtmlStrings(RichEdit,htmlstrings);
result := htmlstrings.Text;
finally
htmlstrings.free ;
JvRichEditToHtml1.free;
end;
end;
就在我发送电子邮件之前,我使用代码在 TRichEdit 中插入一个称呼字符串作为新的顶行,然后是两个空行。电子邮件系统使用它来个性化电子邮件,并且效果很好。
问题在于,如果用户在输入正文时格式化了正文的第一行,例如假设他们将前几行设为项目符号列表,那么我在代码下添加的称呼行也会显示为项目符号,当电子邮件到达。
如何使用代码在 TRichEdit 的顶部插入没有段落或字体格式的行,同时保留用户可能已应用于手动输入的第一行的任何格式(什么是)?
我现在用来插入我的称呼字符串的代码如下,但我的称呼仍然得到用户应用的格式样式。 (最初我只有三个插入行,但在类似的问题here 中添加了其他代码跟随想法)。大写的标识符是在别处定义的常量。
procedure AddRecipientVarableToBody( var Body: TRichEdit);
begin
//remove formatting from the (new) first paragraph
Thebody.Paragraph.Numbering := nsnone;
Thebody.Paragraph.Alignment := taLeftJustify;
//add the three new top lines (two blank plus a recipient)
//done backwards as we insert a new line zero each time
TheBody.lines.Insert(0,EMPTY_STRING); // two blank lines
TheBody.lines.Insert(0,EMPTY_STRING);
TheBody.lines.Insert(0,'To: ' + RECIPIENT_VARIABLE_SALUTATION);
//Remove any formatting from first three lines
TheBody.SelStart:=0;
TheBody.SelLength:= length(TheBody.Lines[0]) + length(TheBody.Lines[1]) + length(TheBody.Lines[2]);
TheBody.SelAttributes.Style := [];
end;
附录:
我设法通过延迟称呼插入来获得我想要的结果,直到我设置准备传递给 Indy 的参数并将整个 TRichEdit HTML 附加到一个简单的文本字符串,即 而不是
Params.Add('html=' + GetHTML(body));
我用过
Params.Add('html=' + 'To: ' + RECIPIENT_VARIABLE_SALUTATION + GetHTML(body));
body 是 TRichEdit。
但是,我仍然想知道我的问题是否可以通过直接在 TRichEdit 中插入新行来解决。
【问题讨论】:
-
你是对的,只是颜色正常。我现在删除了我的帖子,但今天晚些时候会查看这个问题。
-
我更新了我的答案并将我能想到的所有属性添加到
DefAttributes以及子弹处理。
标签: delphi formatting trichedit