首页  编辑  

OLEContainer从文档载入对象

Tags: /超级猛料/COM、ActiveX,DDE/   Date Created:
如何从文件直接读取到OLEContainer中?例如直接读取一个Word文件并显示到OLEContainer?
procedure TOleContainer . LoadAsDocument ( const FileName : string );
var
  Stream : TFileStream ;
  DataHandle : HGLOBAL ;
  Buffer : Pointer ;
begin
  Stream := TFileStream . Create ( FileName , fmOpenRead );
  try
    DataHandle := GlobalAlloc ( GMEM_MOVEABLE , Stream . Size );
    if DataHandle = 0 then OutOfMemoryError ;
    try
      Buffer := GlobalLock ( DataHandle );
      try
        Stream . Read ( Buffer ^, Stream . Size );
      finally
        GlobalUnlock ( DataHandle );
      end ;
      OleCheck ( CreateILockBytesOnHGlobal ( DataHandle , True , FLockBytes ));
      DataHandle := 0 ;
      OleCheck ( StgOpenStorageOnILockBytes ( FLockBytes , nil , STGM_READWRITE or
        STGM_SHARE_EXCLUSIVE , nil , 0 , FStorage ));
      OleCheck ( OleLoad ( FStorage , IOleObject , Self , FOleObject ));
      FDrawAspect := 1 ;
      InitObject ;
      UpdateView ;
    except
      if DataHandle <> 0 then GlobalFree ( DataHandle );
      DestroyObject ;
      raise ;
    end ;
  finally
    Stream . Free ;
  end ;
end ;