飘云阁

 找回密码
 加入我们

QQ登录

只需一步,快速开始

查看: 1971|回复: 0

在RxRichEdit中插入图片的完美解决方法

[复制链接]
  • TA的每日心情
    开心
    2019-9-19 16:05
  • 签到天数: 4 天

    [LV.2]偶尔看看I

    发表于 2007-2-12 10:52:41 | 显示全部楼层 |阅读模式
    例程如下:

    设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)^ := PWord(@achar[1])^;
        inc(i, 2);
      end;
      for bbs := 1 to Length(bb) do
      begin
        achar := IntToHex(Integer(bb[bbs]), 2);
        PWord(@Buffer)^ := 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.
    PYG19周年生日快乐!
    您需要登录后才可以回帖 登录 | 加入我们

    本版积分规则

    快速回复 返回顶部 返回列表