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

[入库:2005年8月18日] [更新:2007年3月25日]

本文简介:选择自 andykang 的 blog

例程如下:

设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 {\pict\dibitmap ' + 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:\temp\untitled.bmp', rxrichedit1);
  insertbitmapintorxrichedit('c:\temp\untitled-8.jpg', rxrichedit1);
  rxrichedit1.setfocus;
end;
end.

本文关键:RichEdit RxRichEdit 插入 图片
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top