在RichEdit中,可以通过SelStart来设置选中文本的字体,可不可以不通过选中文本,而是直接在程序内部操作某一行,设置它的字体。比如,实现该功能的函数是,setRowFont(row: Integer, font:TFont),row就是行号,font是字体。谢谢。

解决方案 »

  1.   

    这个就是你要的函数,自己多测试测试就知道了。unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, ComCtrls;type
      TForm1 = class(TForm)
        REFile: TRichEdit;
        Button1: TButton;
        Button2: TButton;
        procedure Button2Click(Sender: TObject);
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        procedure ChangeLineFont(row:integer;FFont:TFont);
      end;var
      Form1: TForm1;implementation{$R *.dfm}
    procedure TForm1.ChangeLineFont(row:integer;FFont:TFont);
    var
       Start,Startback,j:integer;
    begin
      REFile.WordWrap :=false; //注意这里是为了保证回车2个字符的处理正常
       Startback:=REFile.SelStart;
       Start:=0;
       for j:=0 to REFile.Lines.Count-1 do
       begin
          if j=row then
          begin
             REFile.SelStart:=Start;
             REFile.SelLength:=Length(REFile.Lines.Strings[j]);
             REFile.SelAttributes.Size:=FFont.Size;
             Refile.SelAttributes.Name :=FFont.Name;
             Refile.SelAttributes.Color := FFont.Color;
             Refile.SelAttributes.Charset:=FFont.Charset;
             Refile.SelAttributes.Pitch := FFont.Pitch;
             Refile.SelAttributes.Style := FFont.Style;
             Refile.SelAttributes.Height := FFont.Height;
             REFile.SelLength:=0;
             break;
          end;
          Start:=Start+Length(REFile.Lines.Strings[j])+2;
       end;
       REFile.SelStart:=Startback;
      REFile.WordWrap :=true;  //恢复
    end;
    procedure TForm1.Button1Click(Sender: TObject);
    begin
    //你可以加载一个字体大小各不相同的的rtf文件,然后修改行字体.
      REFile.Lines.LoadFromFile('c:\1.rtf');
    end;procedure TForm1.Button2Click(Sender: TObject);
    begin
    ChangeLineFont(1,font); //修改第2行的字体
    ChangeLineFont(2,font); //修改第3行的字体
    end;end.