我做了一个DEMO:
procedure TForm1.btnInsertClick(Sender: TObject);
var
Bmp: TBitmap;
FStream: IStream;
FRTF: IRichEditOle;
FStorage: IStorage;
FLockBytes: ILockBytes;
ReObject: TReObject;
Written: Longint;
Content: string;
begin
Bmp := TBitmap.Create;
Bmp.LoadFromResourceName(hInstance, 'angry');
try
//创建并写入流:
SendMessage(ExRichEdit1.Handle, em_GetOleInterFace, 0, LongInt(@FRTF));
if not Assigned(FRTF) then Exit;
if not Succeeded(CreateILockBytesOnHGlobal(0,true,FLockBytes)) then Exit;
if not Succeeded(StgCreateDocfileOnILockBytes(FLockBytes,STGM_SHARE_EXCLUSIVE or
STGM_CREATE or STGM_READWRITE,0,FStorage)) then Exit;
if not Succeeded(FStorage.CreateStream('face', STGM_CREATE or STGM_READWRITE or STGM_SHARE_EXCLUSIVE, 0, 0, FStream)) then exit;
Content := Str[Count];
Inc(Count);
if not Succeeded(FStream.Write(@Content, Length(Content), @Written)) then exit;
ExRichEdit1.InsertBitmap(Bmp); //ExRichEdit控件可以直接通过这种方式插入BMP
//往已插入的图片中插入前面创建的流:
ExRichEdit1.SelLength := -1;
SendMessage(ExRichEdit1.Handle, em_GetOleInterFace, 0, LongInt(@FRTF));
Reobject.cbStruct := Sizeof(TReobject);
if (FRTF.GetObject(Integer(REO_IOB_SELECTION), ReObject, REO_GETOBJ_ALL_INTERFACES) = S_OK)
and (Assigned(ReObject.oleobj)) then
FRTF.SaveCompleted(Integer(REO_IOB_SELECTION), FStorage);
ExRichEdit1.SelStart := Start + 1;
ExRichEdit1.SelLength := 0;
ExRichEdit1.SetFocus;
finally
FRTF := nil;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Count := 0;
Str[0] := '0123';
Str[1] := '1234';
Str[2] := '2345';
Str[3] := '3456';
Str[4] := '4567';
Str[5] := '5678';
Str[6] := '6789';
end;
procedure TForm1.btnReadClick(Sender: TObject);
var
Content, s, ch, Res: string;
i, j, p: Integer;
FRTF: IRichEditOle;
FStorage: IStorage;
FStream: IStream;
Realsize: Longint;
Reobject: TReObject;
begin
Memo2.Clear;
Res := '';
p := 0; //ExRichEdit的指针
s := ExRichEdit1.WideText;
i := 1; //S的指针
repeat //一位位往下读s,如果是'?',有可能是OLE对象,尝试读取流。
Ch := s;
if Ord(Ch[1]) >= 127 then //汉字的第一字节
begin
Ch := Copy(s, i, 2);
Res := Res + Ch;
Inc(i, 2);
end
else if (Ch <> '?') then //OLE对象对应位置显示的是'?'
begin
Res := Res + Ch;
Inc(i);
end else
begin
ExRichEdit1.SelStart := p;
ExRichEdit1.SelLength := 1;
try
SendMessage(ExRichEdit1.Handle, em_GetOleInterFace, 0, LongInt(@FRTF));
Reobject.cbStruct := Sizeof(TReobject);
if Succeeded(FRTF.GetObject(Integer(REO_IOB_SELECTION), ReObject, REO_GETOBJ_ALL_INTERFACES))
and (Assigned(ReObject.oleobj)) then
begin
FStorage := Reobject.stg;
if Succeeded(FStorage.OpenStream('face', nil, STGM_READ or STGM_SHARE_EXCLUSIVE, 0, FStream)) then
begin
if Succeeded(FStream.Read(@Content, Sizeof(string), @Realsize)) then
Res := Res + Content;
end
else Res := Res + '?';
end
else Res := Res + '?';
finally
FRTF := nil;
end;
ExRichEdit1.SelLength := 0;
Inc(i);
end;
Inc(p);
until i > Length(s);
memo2.Text := Res;
ExRichEdit1.Clear;
Count := 0;
end; //执行到这句的时候出现了我上面说的错误,在Str字符串中插入了一段“乱码”
请各位大侠帮忙看看哪里出错了,调也调不出来。