procedure TForm1.AddText(RichEdit: TRichEdit; Str: string;
TextColor: TColor = clBlack;
FontName: string = '宋体';
Size: integer = 9;
Style: TFontStyles = []);
var
nStartOffset: integer;
begin
with RichEdit1 do
begin
if Lines.Count = 0 then
nStartOffset:= 0
else
nStartOffset:= Length(Text); //Lines.Insert(0, Str);
Lines.Add( Str );
SelStart:= nStartOffset;
SelLength:= Length( Str );
//Set Attribues
SelAttributes.Color:= TextColor;
SelAttributes.Name:= FontName;
SelAttributes.Size:= Size;
SelAttributes.Style:= Style;
SelLength:= 0;
//滚动到最下方,模拟 CTRL+END 键,总不好使呢!???
PostMessage(RichEdit1.Handle, WM_KEYDOWN, VK_CONTROL, 0);
PostMessage(RichEdit1.Handle, WM_KEYDOWN, VK_NEXT, 0);
PostMessage(RichEdit1.Handle, WM_KEYUP, VK_NEXT, 0);
PostMessage(RichEdit1.Handle, WM_KEYUP, VK_CONTROL, 0);
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
AddText(RichEdit1, '缺省文字');
AddText(RichEdit1, '红色文字', clRed);
AddText(RichEdit1, '绿色,华文行楷', clGreen, '华文行楷');
AddText(RichEdit1, '蓝色,黑体,24', clBlue, '黑体',24);
AddText(RichEdit1, '紫色,华文彩云,32', clFuchsia, '华文彩云',32,
[fsBold, fsItalic, fsUnderline]);
AddText(RichEdit1, '黄色文字', clYellow);
end;