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

  • 主题发起人 主题发起人 kyq
  • 开始时间 开始时间
K

kyq

Unregistered / Unconfirmed
GUEST, unregistred user!
设Form1上有控件RxRichEdit1和Button1。

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;

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

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

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

FreeAndNil(Picture);
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
//InsertBitmapIntoRxRichEdit('C:/Documents and Settings/Administrator/My Documents/1star.bmp', RxRichEdit1);
InsertBitmapIntoRxRichEdit('C:/Documents and Settings/Administrator/My Documents/Untitled-8.jpg', RxRichEdit1);
RxRichEdit1.SetFocus;
end;
end.
 
学习,好像QQ就是 这么做地
 
以前也做过这方面的控件,可是图形总是被擦掉,学习中....[:)]
 
插入速度好慢
而且图象失真不少!

等着新版本!
 
后退
顶部