这是我很久以前保存的帖子。它是由 TeamB 的 Peter Above 博士发布到旧的 Borland Delphi 新闻组,但它今天仍然适用。它展示了如何使用EM_STREAMIN
andEM_STREAMOUT
消息和相关回调将 RTF 文本放入和复制出TRichEdit
.
Uses RichEdit;
Type
TEditStreamCallBack = function (dwCookie: Longint; pbBuff: PByte;
cb: Longint; var pcb: Longint): DWORD; stdcall;
TEditStream = record
dwCookie: Longint;
dwError: Longint;
pfnCallback: TEditStreamCallBack;
end;
function EditStreamInCallback(dwCookie: Longint; pbBuff: PByte;
cb: Longint; var pcb: Longint): DWORD; Stdcall;
var
theStream: TStream;
dataAvail: LongInt;
begin
theStream := TStream(dwCookie);
with theStream do begin
dataAvail := Size - Position;
Result := 0; {assume everything is ok}
if dataAvail <= cb then begin
pcb := Read(pbBuff^, dataAvail);
if pcb <> dataAvail then //couldn't read req. amount of bytes
result := E_FAIL;
end
else begin
pcb := Read(pbBuff^, cb);
if pcb <> cb then
result := E_FAIL;
end;
end;
end;
Function EditStreamOutCallback(dwCookie: Longint; pbBuff: PByte;
cb: Longint; var pcb: Longint): DWORD; stdcall;
var
theStream: TStream;
begin
theStream := TStream(dwCookie);
with theStream do begin
If cb > 0 Then
pcb := Write(pbBuff^, cb);
Result := 0;
end;
end;
Procedure GetRTFSelection( aRichEdit: TRichEdit; intoStream: TStream );
Var
editstream: TEditStream;
Begin
With editstream Do Begin
dwCookie:= Longint(intoStream);
dwError:= 0;
pfnCallback:= EditStreamOutCallBack;
end;
aRichedit.Perform( EM_STREAMOUT, SF_RTF or SFF_SELECTION, longint(@editstream));
End;
Procedure PutRTFSelection( aRichEdit: TRichEdit; sourceStream: TStream );
Var
editstream: TEditStream;
Begin
With editstream Do Begin
dwCookie:= Longint(sourceStream);
dwError:= 0;
pfnCallback:= EditStreamInCallBack;
end;
aRichedit.Perform( EM_STREAMIN, SF_RTF or SFF_SELECTION, longint(@editstream));
End;