TRichEdit 文字背景顏色

1 篇文章 / 0 new
author
TRichEdit 文字背景顏色
指定文字背景顏色方式
procedure addText(s :string; const fgcolor : integer = -1; const bgcolor : integer = -1);
    procedure RE_ColorLine(ARichEdit: TRichEdit; AfgColor, ABgColor, StartPos, EndPos: Integer);
    var
        Format: CHARFORMAT2;
    begin
        FillChar(Format, SizeOf(Format), 0);
        with Format do
        begin
            cbSize := SizeOf(Format);
            dwMask := CFM_BACKCOLOR;
            crBackColor := ABgColor;
        end;
        with ARichEdit do
        begin
            SelStart := StartPos;
            SelLength := Endpos - Startpos; //Length(Lines[ARow - 1]);
            SelAttributes.Color := AFgColor;
            Perform(EM_SETCHARFORMAT, SCF_SELECTION, Longint(@Format));
            SelLength := 0;
        end;
    end;
var
    l,sp,ep : integer;
begin
    if s <> '' then
    begin
        sp := length(richedit.Lines.text);
        richedit.Lines.Add(s);
        ep := sp+ length(s);
        RE_ColorLine(richedit, fgcolor , bgcolor , sp, ep);
    end;
end;
Free Web Hosting