RichView的几个封装函数(插入文本和图片)

来源:互联网 发布:linux mount 根目录 编辑:程序博客网 时间:2024/05/13 21:12

 //移动光标到最后

procedure RichViewMoveCaretToEnd(RichEdit: TRichViewEdit);
var
  ItemNo, Offs: Integer;
begin
  ItemNo := RichEdit.ItemCount-1;
  if ItemNo > 0 then
  begin
    Offs := RichEdit.GetOffsAfterItem(ItemNo);
    RichEdit.SetSelectionBounds(ItemNo, Offs, ItemNo, Offs);
  end;
end;

// 向RichView中增加文本(注意里边检查了Style是否存在)

procedure RichViewAddText(RichEdit: TRichViewEdit; const S: string;
  FontInfo: TFontInfo);
const
  RV_FIND_PROP: TRVFontInfoProperties =
    [rvfiFontName, rvfiSize, rvfiBold, rvfiItalic,
    rvfiUnderline, rvfiStrikeout, rvfiColor];
var
  LStyleNo: Integer;
  LNewFontInfo: TFontInfo;
begin
  LStyleNo := RichEdit.Style.TextStyles.FindSuchStyle(0, FontInfo, RV_FIND_PROP);
  if LStyleNo < 0 then
  begin
    LNewFontInfo := RichEdit.Style.TextStyles.Add;
    LNewFontInfo.Assign(FontInfo);
    LNewFontInfo.Unicode := True;
    LStyleNo := RichEdit.Style.TextStyles.Count - 1;
  end;
  RichEdit.ApplyTextStyle(LStyleNo);
  RichEdit.InsertTextW(S);
end;

// 向RichView中增加文本

procedure RichViewAddText(RichEdit: TRichViewEdit;
  const S: string; const FontName: string; FontSize: Integer;
  FontColor: TColor; FontStyle: TFontStyles);
var
  LFontInfo: TFontInfo;
begin
  LFontInfo := TFontInfo.Create(nil);
  try
    LFontInfo.FontName := FontName;
    LFontInfo.Size := FontSize;
    LFontInfo.Color := FontColor;
    LFontInfo.Style := FontStyle;

    RichViewAddText(RichEdit, S, LFontInfo);
  finally
    LFontInfo.Free;
  end;
end;

// 向RichView中增加图片

procedure RichViewAddPicture(RichEdit: TRichViewEdit; const TagStr: string;
  Stream: TMemoryStream; IsGif: Boolean);
var
  LGraphic: TGraphic;
begin
  if IsGif then
  begin
    LGraphic := TGIFImage.Create;
  end else
  begin
    LGraphic := TBitmap.Create;
  end;

  Stream.Position := 0;
  LGraphic.LoadFromStream(Stream);
  RichEdit.InsertPicture(TagStr, LGraphic, rvvaBaseline);
end;