ITPub博客

首页 > 应用开发 > IT综合 > 在RxRichEdit中插入图片的完美解决方法(不使用剪贴板) (转)

在RxRichEdit中插入图片的完美解决方法(不使用剪贴板) (转)

原创 IT综合 作者:worldblog 时间:2007-12-12 11:13:16 0 删除 编辑
在RxRichEdit中插入图片的完美解决方法(不使用剪贴板) (转)[@more@]

例程如下:

设Form1上控件RxRichEdit1和Button1。

InsertBitmapintoRxRichEdit使用方法请见Button1Click方法。

unit Unit1;

interface

uses
  windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls, RxRichEd;

type
  TForm1 = class(TForm)
  Button1: TButton;
  RxRichEdit1: TRxRichEdit;
  procedure Button1Click(Sender: Tobject);
  private
  { Private declarations }
  public
  { Public declarations }
  end;

var
  Form1: TForm1;

implementation
uses
  JPEG;

{$R *.dfm}

function ConvertBitmapToRTF(const Bitmap: TBitmap): string;
var
  bi, bb: string;
  bis, bbs: Cardinal;
  achar: string[2];
  Buffer: string;
  I: Integer;
type
  Pword = ^Word;
begin
  GetDIBSizes(Bitmap.Handle, bis, bbs);
  SetLength(bi, bis);
  SetLength(bb, bbs);
  GetDIB(Bitmap.Handle, Bitmap.Palette, PChar(bi)^, PChar(bb)^);
  SetLength(Buffer, (Length(bb) + Length(bi)) * 2);
  i := 1;
  for bis := 1 to Length(bi) do
  begin
  achar := IntToHex(Integer(bi[bis]), 2);
  PWord(@Buffer[i])^ := PWord(@achar[1])^;
  inc(i, 2);
  end;
  for bbs := 1 to Length(bb) do
  begin
  achar := IntToHex(Integer(bb[bbs]), 2);
  PWord(@Buffer[i])^ := PWord(@achar[1])^;
  inc(i, 2);
  end;
  Result := '{rtf1 {pictdibitmap ' + Buffer + ' }}';
end;

procedure InsertBitmapIntoRxRichEdit(const Bitmap: TBitmap; const RxRichEdit:
  TRxRichEdit); overload;
begin
  RxRichEdit.SelText := ConvertBitmapToRTF(Bitmap);
  RxRichEdit.SelLength := 0;
  RxRichEdit.SelStart := RxRichEdit.SelStart + 1;
end;

procedure InsertBitmapIntoRxRichEdit(const GraphicFileName: string; const
  RxRichEdit: TRxRichEdit); overload;
var
  Bitmap: TBitmap;

  Graphic: TPicture;
begin
  Graphic := TPicture.Create;
  try
  Graphic.LoadFromFile(GraphicFileName);

  if Graphic.Graphic is TBitmap then
  Bitmap := Graphic.Bitmap
  else
  begin
  Bitmap := TBitmap.Create;
  Bitmap.Assign(Graphic.Graphic);
  end;

  InsertBitmapIntoRxRichEdit(Bitmap, RxRichEdit);
  finally
  if Bitmap <> Graphic.Bitmap then
  FreeAndNil(Bitmap);

  FreeAndNil(Graphic);
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  //InsertBitmapIntoRxRichEdit('C:TempUntitled.bmp', RxRichEdit1);
  InsertBitmapIntoRxRichEdit('C:TempUntitled-8.jpg', RxRichEdit1);
  RxRichEdit1.SetFocus;
end;
end.


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10752043/viewspace-991834/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
  • 博文量
    6241
  • 访问量
    2411180