procedure TfrmXWBL.FormPaint(Sender: TObject);
var
  aCanvas: Tcanvas;
  X1, X2, Y,t,i: Integer;
  byLineLength: BYTE;begin
aCanvas := TCanvas.Create;
Y := RichEdit1.Height;
   try   aCanvas.Handle := GetDC(RichEdit1.Handle);
   t:=2+richedit1.DefAttributes.Height+6;
   aCanvas.Pen.Color:=clskyblue;
   acanvas.Pen.width:=1;
   for i := 0 to RichEdit1.Lines.Count - 1 do
   begin
   //RichEdit1.
      With aCanvas do
      Begin
         Begin
         MoveTo(RichEdit1.Left+2,t); // Draw line after 80 characters
         LineTo(RichEdit1.Width-1,t);
      End;
   end;
   t:=t+richedit1.DefAttributes.Height+5;
    end
   Finally
      ReleaseDC(RichEdit1.Handle,aCanvas.Handle);
      aCanvas.Free;
   End;
end;
各位高手,我想在richedit中的每行加一条线分隔,但是当richedit中的内容过多时,滚动滚动条就会把原来绘好的线条抹去。有没有办法在滚动滚动条时,原来划的行线保留。谢谢!

解决方案 »

  1.   

    控件发生变动肯定要重绘的,如果不重绘会发生问题,你也不希望控件里面用鼠标选择的文字跟你看到的不一样吧。你要保留这个线,只能在RichEdit的onPaint事件中实现
      

  2.   

    RichEdit的onPaint事件中实现
    加上你上面的代码
      

  3.   

    RichEdit有全WMPaint消息,如果能截获到这个消息,添加上你的重绘代码就行了。
      

  4.   

    interface
    uses
      Messages, Windows,  Graphics,  ComCtrls;type
      TMyRichEdit = class(TRichEdit)
        procedure WMPaint(var Message: TWMPaint); message WM_PAINT;
      end;implementation{ TMyRichEdit }procedure TMyRichEdit.WMPaint(var Message: TWMPaint);
    var
      ACanvas: Tcanvas;
      X1, X2, Y,t,i: Integer;
      byLineLength: BYTE;
    begin
      inherited;
      aCanvas := TCanvas.Create;
      Y := Self.Height;
      try
        ACanvas.Handle := GetDC(Self.Handle);
        t:=2+Self.DefAttributes.Height+6;
        aCanvas.Pen.Color:=clskyblue;
        acanvas.Pen.width:=1;
        for i := 0 to Self.Lines.Count - 1 do
        begin
          With ACanvas do
          Begin
            Begin
              MoveTo(Self.Left+2,t); // Draw line after 80 characters
              LineTo(Self.Width-1,t);
            End;
          end;
          t:=t+Self.DefAttributes.Height+5;
        end
      Finally
        ReleaseDC(Self.Handle,ACanvas.Handle);
        ACanvas.Free;
      End;
    end;然后动态创建这个控件即可