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

解决方案 »

  1.   

    to FigoZhu(谢慕安) :
      RichEdit.Line(n).Font 是不可以的,恐怕没有这么简单吧。
      

  2.   

    那为什么不用SelStart和SelLength呢?
    用完了,再把 SelLength:=0; 不就可以了。
      

  3.   

    可是这样如何判断一行的长度呢?我找了一下,好像没有这个函数。比如,第三行,我加载的文本不一样,它的selStart和selLength也不一样。
      

  4.   

    这个就是你要的函数,自己多测试测试就知道了。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.
      

  5.   

    看到这个帖的标题说是"不通过SelStart来控制某一行",这个我确实不知道如何做到。期待更好的答案.