且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从TOleContainer中提取图元文件?

更新时间:2023-10-03 09:02:28

OLE容器具有可以访问的底层IOLEObject接口。您可以将其传递到 OLEDraw 函数你自己的画布您可以使用Bitmap或Metafile画布,然后以所需的格式保存图像。



OleDraw(OleContainer.OleObjectInterface,DVASPECT_CONTENT,Bmp.Canvas.Handle, R);

  
{
DrawOleOnBmp
--------- -------------------------------------------------- ----------------
取一个OleObject并将其绘制到位图画布上。位图将被调整为
以匹配OLE对象的正常大小。
}
程序DrawOleOnBmp(Ole:IOleObject; Bmp:TBitmap);
var
ViewObject2:IViewObject2;
ViewSize:TPoint;
AdjustedSize:TPoint;

DC:HDC;
R:TRect;
begin

如果成功(Ole.QueryInterface(IViewObject2,ViewObject2))然后
begin
ViewObject2.GetExtent(DVASPECT_CONTENT,-1,nil,ViewSize);

DC:= GetDC(0);
AdjustedSize.X:= MulDiv(ViewSize.X,GetDeviceCaps(DC,LOGPIXELSX),2540);
AdjustedSize.Y:= MulDiv(ViewSize.Y,GetDeviceCaps(DC,LOGPIXELSY),2540);
ReleaseDC(0,DC);

Bmp.Height:= AdjustedSize.Y;
Bmp.Width:= AdjustedSize.X;

SetRect(R,0,0,Bmp.Width,Bmp.Height);

OleDraw(Ole,DVASPECT_CONTENT,Bmp.Canvas.Handle,R);
end
else
begin
raise Exception.Create('无法获取OleObject上的IViewObject2 interfact);
结束

end;


I have a Delphi (BDS 2006) application with TOleContainer control. It has an OLE object inside, MS Equation formula (name 'Equation.3') from MS Office 2003.

How can I extract the vector metafile from the formula image to insert it into web-page or some other document without OLE support?

TOleContainer has only 'Equation.3' objects inside, no other possibilities. I've tried to use .Copy method to make it through clipboard, but it's copied an empty image.

OLE Container has on underlying IOLEObject interface you can access. You can pass that to the OLEDraw function with your own canvas. You could use either a Bitmap or Metafile canvas and then save out the image in the format you need.

OleDraw(OleContainer.OleObjectInterface, DVASPECT_CONTENT, Bmp.Canvas.Handle, R);


{
  DrawOleOnBmp
  ---------------------------------------------------------------------------
  Take a OleObject and draw it to a bitmap canvas.  The bitmap will be sized
  to match the normal size of the OLE Object.
}
procedure DrawOleOnBmp(Ole: IOleObject; Bmp: TBitmap);
var
  ViewObject2: IViewObject2;
  ViewSize: TPoint;
  AdjustedSize: TPoint;

  DC: HDC;
  R: TRect;
begin

  if Succeeded(Ole.QueryInterface(IViewObject2, ViewObject2)) then
  begin
    ViewObject2.GetExtent(DVASPECT_CONTENT, -1, nil, ViewSize);

    DC := GetDC(0);
    AdjustedSize.X := MulDiv(ViewSize.X, GetDeviceCaps(DC, LOGPIXELSX), 2540);
    AdjustedSize.Y := MulDiv(ViewSize.Y, GetDeviceCaps(DC, LOGPIXELSY), 2540);
    ReleaseDC(0, DC);

    Bmp.Height := AdjustedSize.Y;
    Bmp.Width := AdjustedSize.X;

    SetRect(R, 0, 0, Bmp.Width, Bmp.Height);

    OleDraw(Ole, DVASPECT_CONTENT, Bmp.Canvas.Handle, R);
  end
  else
  begin
    raise Exception.Create('Could not get the IViewObject2 interfact on the OleObject');
  end;

end;