程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> 在RxRichEdit中插入圖片的完美解決方法(不使用剪貼板)

在RxRichEdit中插入圖片的完美解決方法(不使用剪貼板)

編輯:Delphi

例程如下:

設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 := {tf1 {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.

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved