1

我正在使用 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 中插入新行来解决。

4

1 回答 1

1

您可以您的RichEdit. 然后您可以轻松返回以使用此设置,只需

RE.SelAttributes := RE.DefAttributes;

因此,这是对您的情况的测试。首先定义DefAttributes,例如OnFormCreate()

procedure TForm1.FormCreate(Sender: TObject);
begin
  // Initialize to what you want to return to, or use as default
  RE.DefAttributes.Charset := ANSI_CHARSET;
  RE.DefAttributes.Color := clBlack;
  RE.DefAttributes.Height := -16;
  RE.DefAttributes.Name := 'Segoe UI';
  RE.DefAttributes.Size := 12;
  RE.DefAttributes.Style := [];
end;

请注意,上面不处理子弹,它们是分开处理的。

在以下代码中,我们模拟用户可能编写的内容......

procedure TForm1.Button1Click(Sender: TObject);
begin
  RE.Lines.Add('Final reminder');
  RE.Lines.Add('Please, fill the form below, and send it immediately.');

  RE.SelStart := 0;
  RE.SelLength := Length(RE.Lines[0]);
  RE.SelAttributes.Color := clRed;
  RE.SelAttributes.Name := 'Algerian';
  RE.SelAttributes.Size := 15;

  RE.SelStart :=  Length(RE.Lines[0]);
  RE.SelLength :=  Length(RE.Lines[1]);
  RE.SelAttributes := RE.DefAttributes;
end;

...以及它们可能添加的特殊属性 , ,Bold以及第一行的项目符号。这些在我的测试表单中添加了按钮。ItalicUnderlineStrikeout

在此处输入图像描述

最后如何将三行添加到开头并确保独立格式。

procedure AltAddRecipientVarableToBody( var RE: TRichEdit);
begin
  RE.lines.Insert(0,EMPTY_STRING);  // two blank lines
  RE.lines.Insert(0,EMPTY_STRING);
  RE.lines.Insert(0,'To: ' + RECIPIENT_VARIABLE_SALUTATION);

  // Select
  RE.SelStart := 0;
  RE.SelLength:= length(RE.Lines[0]) + 1
               + length(RE.Lines[1]) + 1
               + length(RE.Lines[2]) + 1;
  // Clear attributes
  RE.SelAttributes := RE.DefAttributes;
  // Clear bullets
  RE.Paragraph.Numbering := nsNone;
end;

每行添加 1 个字符用于换行符。请注意,由于项目符号是段落的属性,因此不能在其中定义DefAttributes,必须单独处理。

在此处输入图像描述

结果,添加的三行格式化为DefAttributes,原始文本保持其具有的任何格式。

于 2019-05-20T14:24:39.033 回答