【问题标题】:How do I insert new, unformatted, lines at the top of a TRichEdit如何在 TRichEdit 的顶部插入新的、未格式化的行
【发布时间】:2019-10-06 19:34:27
【问题描述】:

我正在使用 TRichEdit 来保存电子邮件客户端的正文。我已经为用户提供了简单的格式化功能(粗体、斜体、下划线、左、中和右段落对齐和项目符号。这适用于使用 Indy 按照 Remy 的代码here 将格式化文本作为 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 的顶部插入没有段落或字体格式的行,同时保留用户可能已应用于手动输入的第一行的任何格式(什么是)?

我现在用来插入我的称呼字符串的代码如下,但我的称呼仍然得到用户应用的格式样式。 (最初我只有三个插入行,但在类似的问题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


【解决方案1】:

您可以为您的RichEdit 定义DefAttributes。然后您可以轻松返回使用此设置,只需

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;

... 还有哪些特殊属性,BoldItalicUnderlineStrikeout 他们可能已经添加以及第一行的项目符号。这些在我的测试表单中添加了按钮。

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

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 格式化,原始文本保持其具有的任何格式。

【讨论】:

  • 恐怕它的行为方式相同。我将行 TheBody.SelAttributes.Name := 'Tempus Sans ITC;` 更改为 TheBody.SelAttributes.Name := 'Stencil '; 以使任何字体更改更加明显。我首先单击了您的 Button1,它将字体设置为 Tahoma 10pt。然后,我在单独的行上手动输入“一”、“二”、“三”(以 Tohoma 10pt 红色显示)并将它们格式化为项目符号。最后我点击了 button2 来运行 AltAddRecipientVarableToBody 代码。这插入了称呼和两个空行。它们是黑色的,但仍然是 Tohoma 10pt 字体并且仍然是项目符号。
  • 感谢您的打扰。这样可行。看来我快到了。我们都将段落编号设置为 nsnone,但我是在选择之前设置的。此外,您将选择样式设置为默认值,而我将其设置为 []。无论如何,您的代码有效,而我的代码无效,因此接受了答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-06-25
  • 1970-01-01
  • 2019-12-05
  • 2023-02-08
  • 2018-08-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多