Mega Code Archive

 
Categories / Delphi / Examples
 

Stream

BITS AND BOBS NECESSARY TO FACILITATE THE PLACING OF THE RTF TAGS OF A RICHEDIT COMPONENT INTO A STREAM... strangely enough, this code puts ONLY the RTF tags into the stream, but NONE of the text at all (weird!). However, if one does wish to put RTF tags + text into a stream (or PChar or some other buffer-type varaible) then then one way of doing this is to save the RichText text to file in RTF format and then read that file into the stream or other variable. For another method of putting text + tags into a stream check out the HTML 'help' file somehwere called something like RichTextFormat... uses {note that the inclusion of RichEdit in the uses list below is only necessary to facilitate the writing of RichEdit text to a stream (see SaveToStream procedure) as without this inclusion, the compiler will not recognise the EM_STREAMOUT API call...} {WHATEVER, including:} RichEdit; type TEditorForm = class(TForm) Editor: TRichEdit; procedure SaveEditorToStream; procedure GetRTFSelection(aRichEdit: TRichEdit;intoStream: TStream); private public end; type TEditStreamCallBack = function(dwCookie: Longint; pbBuff: PByte; cb: Longint; var pcb: Longint): Longint; stdcall; var EditorForm: TEditorForm; implementation procedure TEditorForm.SaveEditorToStream; var aMemStream: TMemoryStream; begin aMemStream := TMemoryStream.Create; try GetRTFSelection(Editor, aMemStream); aMemStream.Position := 0; {DO STUFF WITH THE STREAM...} {can put stream text BACK into the Editor like so:...} Editor.Lines.LoadFromStream(aMemStream); finally aMemStream.Free; end; end; function EditStreamOutCallback(dwCookie: Longint; pbBuff: PByte; cb: Longint; var pcb: Longint): Longint; stdcall; var theStream: TStream; begin theStream := TStream(dwCookie); with theStream do begin if (cb > 0) then begin pcb := Write(pbBuff^, cb); Result := pcb; end else Result := 0; end; end; procedure TEditorForm.GetRTFSelection(aRichEdit: TRichEdit;intoStream: TStream); type TEditStream = record dwCookie: Longint; dwError: Longint; pfnCallback: TEditStreamCallBack; end; var editstream: TEditStream; begin with editstream do Begin dwCookie:= Longint(intoStream); dwError:= 0; pfnCallback:= EditStreamOutCallBack; end; {SendMessage(Editor.Handle, EM_STREAMOUT, SF_RTF or SFF_SELECTION, longint(@editstream));} aRichedit.Perform(EM_STREAMOUT,SF_RTF or SFF_SELECTION,longint(@editstream)); end; end.