在DELPHI的RichTextEdit控件中,怎样实现插入图片的功能。

解决方案 »

  1.   

    啊?
    没听说过
    用WORD吧
      

  2.   

    好想不行吧
    可以在其中create  TImage 吧
      

  3.   

    Richedit中插入图片    
        
    usesRichEdit;// Stream Callback functiontypeTEditStreamCallBack = function(dwCookie: Longint; pbBuff: PByte;cb: Longint; var pcb: Longint): DWORD;stdcall;TEditStream = recorddwCookie: Longint;dwError: Longint;pfnCallback: TEditStreamCallBack;end;// RichEdit TypetypeTMyRichEdit = TRxRichEdit;// EditStreamInCallback callback functionfunction EditStreamInCallback(dwCookie: Longint; pbBuff: PByte;cb: Longint; var pcb: Longint): DWORD; stdcall;// by P. BelowvartheStream: TStream;dataAvail: LongInt;begintheStream := TStream(dwCookie);with theStream dobegindataAvail := Size - Position;Result := 0;if dataAvail <= cb thenbeginpcb := read(pbBuff^, dataAvail);if pcb <> dataAvail thenResult := UINT(E_FAIL);endelsebeginpcb := read(pbBuff^, cb);if pcb <> cb thenResult := UINT(E_FAIL);end;end;end;// Insert Stream into RichEditprocedure PutRTFSelection(RichEdit: TMyRichEdit; SourceStream: TStream);// by P. BelowvarEditStream: TEditStream;beginwith EditStream dobegindwCookie := Longint(SourceStream);dwError := 0;pfnCallback := EditStreamInCallBack;end;RichEdit.Perform(EM_STREAMIN, SF_RTF or SFF_SELECTION, Longint(@EditStream));end;// Convert Bitmap to RTF Codefunction BitmapToRTF(pict: TBitmap): string;// by D3kvarbi, bb, rtf: string;bis, bbs: Cardinal;achar: ShortString;hexpict: string;I: Integer;beginGetDIBSizes(pict.Handle, bis, bbs);SetLength(bi, bis);SetLength(bb, bbs);GetDIB(pict.Handle, pict.Palette, PChar(bi)^, PChar(bb)^);rtf := '{{';SetLength(hexpict, (Length(bb) + Length(bi)) * 2);I := 2;for bis := 1 to Length(bi) dobeginachar := Format('%x', [Integer(bi[bis])]);if Length(achar) = 1 thenachar := '0' + achar;hexpict[I - 1] := achar[1];hexpict[I] := achar[2];Inc(I, 2);end;for bbs := 1 to Length(bb) dobeginachar := Format('%x', [Integer(bb[bbs])]);if Length(achar) = 1 thenachar := '0' + achar;hexpict[I - 1] := achar[1];hexpict[I] := achar[2];Inc(I, 2);end;rtf := rtf + hexpict + ' }}';Result := rtf;end; // Example to insert image from Image1 into RxRichEdit1procedure TForm1.Button1Click(Sender: TObject);varSS: TStringStream;BMP: TBitmap;beginBMP := TBitmap.Create;BMP := Image1.Picture.Bitmap;SS := TStringStream.Create(BitmapToRTF(BMP));tryPutRTFSelection(RxRichEdit1, SS);finallySS.Free;end;end;****************************************下面的代码可以不调用那个InsertObject的对话框而直接插入一张图片:varBmp:TBitmap;beginif not OpenPictureDialog1.Execute then exit;Bmp:=TBitmap.Create;Bmp.LoadFromFile(OpenPictureDialog1.FileName);Clipboard.Assign(BMP);RxRichEdit201.PasteFromClipboard;Bmp.Free;end;**************************************: TechnoFantasy(www.applevb.com)RichEdit中,插入图片代码:procedure proPrintRTFWithBMP(strCaption,strPic,strTitle:string;rtf:TRichEdit);{strText为要打印的文本 strCaption为打印标题 strPic为图像文件目录strTitle为要显示在图像右侧的图像标题}varFRTF:IRichEditOle;FOLE:IOLEObject;formatEtc:tagFORMATETC;FStorage :ISTORAGE;FClientSite:IOLECLIENTSITE;FLockBytes:ILockBytes;ReObject:TReObject;xt:TGuid;FTemp:IUnknown;strTemp:string;bCreateNew:boolean;ABMP:TBitmap;Ajpeg:TJpegImage;i:Longint;begin// rtfTemp:=TRichEdit.Create(frmPrintFrame);try{ with rtfTemp dobeginParent := frmPrintFrame;width:=200;height:=200;visible:=false;Text := strText;end; }//图片文件不存在,直接打印文本并退出if not fileexists(strPic)thenbeginPrintRichEdit(strCaption,rtf);exit;end;abmp:=TBitmap.Create;ajpeg:= TJpegImage.Create;tryif ExtractFileExt(strPic)='.jpg' thenbeginbCreateNew:=true;ajpeg.LoadFromFile(strPic);abmp.Assign(ajpeg);strTemp:=ExtractFilePath(strPic)+'0099www.bmp';abmp.SaveToFile(strTemp);for i:=1 to 30000 doapplication.ProcessMessages;endelsestrTemp:= strPic;finallyabmp.Free;ajpeg.free;abmp:=nil;ajpeg:=nil;end;sendmessage(rtf.handle,EM_GETOLEINTERFACE,0,LongInt(@FRTF));if not assigned(FRTF)thenbeginshowmessage('Error to get Richedit OLE interface');exit;end;//建立一个可以访问全局内存的Byte数组 FLockBytes//返回ILockBytes接口if CreateILockBytesOnHGlobal(0,true,FLockBytes)<>S_OK thenbeginshowmessage('Error to create Global Heap');exit;end;//建立一个混合文档存取对象if StgCreateDocfileOnILockBytes(FLockBytes,STGM_SHARE_EXCLUSIVE orSTGM_CREATE or STGM_READWRITE,0,FStorage)<>S_OK thenbeginshowmessage('Error to create storage');exit;end; formatEtc.cfFormat := 0;FormatEtc.ptd := nil;FormatEtc.dwAspect := DVASPECT_CONTENT;FormatEtc.lindex := -1;FormatEtc.tymed := TYMED_NULL;FRTF.GetClientSite(FClientSite);//从文件中创建一个OLE对象if OleCreateFromFile(GUID_NULL,PWideChar(WideString(strTemp)),IID_IUnknown,0,@formatEtc,FClientSite,FStorage,FOLE)<>S_OK thenbeginshowmessage('Error');exit;end;//现在的FOLE还是一个IUnKnown接口,将其转换为一个 IOleObject接口FTemp:=FOLE;FTemp.QueryInterface(IID_IOleObject, FOle);OleSetContainedObject(FOle, TRUE); //step 2reobject.cbStruct := sizeof(TReObject);FOLE.GetUserClassID(xt);ReObject.clsid := xt;reobject.cp := ULong(REO_CP_SELECTION);reobject.dvaspect := DVASPECT_CONTENT;reobject.dwFlags := ULong(REO_RESIZABLE) or ULong(REO_BELOWBASELINE);reobject.dwUser := 0;reobject.poleobj := FOle;reobject.polesite := FClientSite;reobject.pstg := FStorage;reobject.sizel.cx := 0;reobject.sizel.cy := 0;FRTF.InsertObject(reobject);PrintRichEdit(strCaption,rtf);finallyif bCreateNew thenDeletefile(strTemp);FRTF:=nil;FOLE:=nil;end;end;
      

  4.   

    上面的代码是一个在RTF控件当前位置插入图像并打印的,你运行上面的代码需要首先引用ActiveX, ComObj, RichEdit, Jpeg并且将PrintRichEdit(strCaption,rtf);去掉以下的结构是需要手工加入的:type_ReObject = recordcbStruct: DWORD; { Size of structure }cp: ULONG; { Character position of object }clsid: TCLSID; { Class ID of object }poleobj: IOleObject; { OLE object interface }pstg: IStorage; { Associated storage interface }polesite: IOleClientSite; { Associated client site interface }sizel: TSize; { Size of object (may be 0,0) }dvAspect: Longint; { Display aspect to use }dwFlags: DWORD; { Object status flags }dwUser: DWORD; { Dword for user's use }end;TReObject = _ReObject;typeIRichEditOle = interface(IUnknown)['{00020d00-0000-0000-c000-000000000046}']function GetClientSite(out clientSite: IOleClientSite): HResult; stdcall;function GetObjectCount: HResult; stdcall;function GetLinkCount: HResult; stdcall;function GetObject(iob: Longint; out reobject: TReObject;dwFlags: DWORD): HResult; stdcall;function InsertObject(var reobject: TReObject): HResult; stdcall;function ConvertObject(iob: Longint; rclsidNew: TIID;lpstrUserTypeNew: LPCSTR): HResult; stdcall;function ActivateAs(rclsid: TIID; rclsidAs: TIID): HResult; stdcall;function SetHostNames(lpstrContainerApp: LPCSTR;lpstrContainerObj: LPCSTR): HResult; stdcall;function SetLinkAvailable(iob: Longint; fAvailable: BOOL): HResult; stdcall;function SetDvaspect(iob: Longint; dvaspect: DWORD): HResult; stdcall;function HandsOffStorage(iob: Longint): HResult; stdcall;function SaveCompleted(iob: Longint; const stg: IStorage): HResult; stdcall;function InPlaceDeactivate: HResult; stdcall;function ContextSensitiveHelp(fEnterMode: BOOL): HResult; stdcall;function GetClipboardData(var chrg: TCharRange; reco: DWORD;out dataobj: IDataObject): HResult; stdcall;function ImportDataObject(dataobj: IDataObject; cf: TClipFormat;hMetaPict: HGLOBAL): HResult; stdcall;end;Type TCharRange=recordcpMin:integer;cpMax:integer;End;Type TFormatRange=recordhdc : Integer;hdcTarget:integer;rectRegion:trect;rectPage:trect;chrg : TCharRange;End;************************************以下不通过剪切板而直接在Richedit中插入一张图片:varfrmMain: TfrmMain;implementation{$R *.DFM}{$R Smiley.res}usesRichEdit;typeTEditStreamCallBack = function(dwCookie: Longint; pbBuff: PByte;cb: Longint; var pcb: Longint): DWORD;stdcall;TEditStream = recorddwCookie: Longint;dwError: Longint;pfnCallback: TEditStreamCallBack;end;typeTMyRichEdit = TRxRichEdit;// EditStreamInCallback callback functionfunction EditStreamInCallback(dwCookie: Longint; pbBuff: PByte;cb: Longint; var pcb: Longint): DWORD; stdcall;vartheStream: TStream;dataAvail: LongInt;begintheStream := TStream(dwCookie);with theStream dobegindataAvail := Size - Position;Result := 0;if dataAvail <= cb thenbeginpcb := read(pbBuff^, dataAvail);if pcb <> dataAvail thenResult := UINT(E_FAIL);endelsebeginpcb := read(pbBuff^, cb);if pcb <> cb thenResult := UINT(E_FAIL);end;end;end;// Insert Stream into RichEditprocedure PutRTFSelection(RichEdit: TMyRichEdit; SourceStream: TStream);varEditStream: TEditStream;beginwith EditStream dobegindwCookie := Longint(SourceStream);dwError := 0;pfnCallback := EditStreamInCallBack;end;RichEdit.Perform(EM_STREAMIN, SF_RTF or SFF_SELECTION, Longint(@EditStream));end;// Load a smiley image from resourcefunction GetSmileyCode(ASimily: string): string;vardHandle: THandle;pData, pTemp: PChar;Size: Longint;beginpData := nil;dHandle := FindResource(hInstance, PChar(ASimily), RT_RCDATA);if dHandle <> 0 thenbeginSize := SizeofResource(hInstance, dHandle);dhandle := LoadResource(hInstance, dHandle);if dHandle <> 0 thentrypData := LockResource(dHandle);if pData <> nil thentryif pData[Size - 1] = #0 thenbeginResult := StrPas(pTemp);endelsebeginpTemp := StrAlloc(Size + 1);tryStrMove(pTemp, pData, Size);pTemp[Size] := #0;Result := StrPas(pTemp);finallyStrDispose(pTemp);end;end;finallyUnlockResource(dHandle);end;finallyFreeResource(dHandle);end;end;end;procedure InsertSmiley(ASmiley: string);varms: TMemoryStream;s: string;beginms := TMemoryStream.Create;trys := GetSmileyCode(ASmiley);if s <> '' thenbeginms.Seek(0, soFromEnd);ms.Write(PChar(s)^, Length(s));ms.Position := 0;PutRTFSelection(frmMain.RXRichedit1, ms);end;finallyms.Free;end;end;procedure TfrmMain.SpeedButton1Click(Sender: TObject);beginInsertSmiley('Smiley1');end;procedure TfrmMain.SpeedButton2Click(Sender: TObject);beginInsertSmiley('Smiley2');end;// Replace a :-) or :-( with a corresponding smileyprocedure TfrmMain.RxRichEdit1KeyPress(Sender: TObject; var Key: Char);varsCode, SmileyName: string;procedure RemoveText(RichEdit: TMyRichEdit);beginwith RichEdit dobeginSelStart := SelStart - 2;SelLength := 2;SelText := '';end;end;beginIf (Key = ')') or (Key = '(') thenbeginsCode := Copy(RxRichEdit1.Text, RxRichEdit1.SelStart-1, 2) + Key;SmileyName := '';if sCode = ':-)' then SmileyName := 'Smiley1';if sCode = ':-(' then SmileyName := 'Smiley2';if SmileyName <> '' thenbeginKey := #0;RemoveText(RxRichEdit1);InsertSmiley('Smiley1');end;end;end;
     
       
      

  5.   

    RXLib里的 RxRichEdit可以
      

  6.   

    其实只要=====以下不通过剪切板而直接在Richedit中插入一张图片:====这个以下的就可以了