我正在使用 TRichEdit 来保存电子邮件客户端的正文。我为用户提供了简单的格式设置功能(粗体、斜体、下划线、左、中和右段落对齐和项目符号。这适用于使用 Indy在此处按照 Remy 的代码将格式化文本作为 html 电子邮件发送。
我将 TRichEdit 文本提取为 html 使用
function 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 的顶部插入没有段落或字体格式的行,同时保留用户可能已应用于手动输入的第一行(什么是)的任何格式?
我现在用来插入我的称呼字符串的代码如下,但我的称呼仍然获得用户应用的格式样式。(最初我只有三个插入行,但在类似的问题中添加了其他代码)。大写的标识符是在别处定义的常量。
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 中插入新行来解决。