我正在尝试使用IDataObject的GetData方法从TOleContainer中提取位图。

 OleContainer1.CreateObject('Paint.Picture', false);
 OleContainer1.OleObjectInterface.QueryInterface(IDataObject, DataObject);


EnumFormatEtc与DataObject上的DATADIR_GET返回以下内容:

 cfFormat, ptd, dwAspect, lIndex, tymed

 CF_METAFILEPICT, nil, DVASPECT_CONTENT, -1, TYMED_MFPICT
 CF_DIB, nil, DVASPECT_CONTENT, -1, TYMED_HGLOBAL or TYMED_ISTREAM
 CF_BITMAP, nil, DVASPECT_CONTENT, -1, TYMED_HGLOBAL


但是当我这样做时:

FormatEtc.cfFormat := CF_BITMAP;
FormatEtc.ptd := nil;
FormatEtc.dwAspect := DVASPECT_CONTENT;
FormatEtc.lIndex := -1;
FormatEtc.tymed := TYMED_HGLOBAL;

OleCheck(DataObject.GetData(FormatEtc, StorageMedium));


我收到无效的FORMATETC结构错误。我究竟做错了什么?

最佳答案

我通过使用找到的代码here来完成您尝试执行的相同操作。就我而言,我发现最好执行以下操作,该操作使用提供的链接中的DrawOleOnBmp()

oleMain.UpdateObject;
if oleMain.OleObjectInterface = nil then
  raise Exception.Create('OLE Container is empty.');
DrawOleOnBmp(oleMain.OleObjectInterface, imgMain.Bitmap);
imgMain.Bitmap.SaveToFile('Filename.bmp');


其中oleMainTOleContainer,而imgMainTImage32。两者都在表格上可见...

为了方便起见,以下是@MarkElder编写的链接中的方法:

{
  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: TBitmap32);
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;

    Bmp.FillRect(0, 0, Bmp.Width, Bmp.Height, clWhite);

    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;

10-04 12:07